简体   繁体   中英

How to access superclass properties here

I have moved NEW_MESSAGE_ON_PROJECT and NEW_MESSAGE_ON_PROPOSAL to BaseNotification , the superclass.

class CustomerNotification(BaseNotification):
    NEW_PROPOSAL = 1000
    # NEW_MESSAGE_ON_PROJECT = 1001
    # NEW_MESSAGE_ON_PROPOSAL = 1002
    CHOICES = ((NEW_PROPOSAL, "New Prpposal"),
               (NEW_MESSAGE_ON_PROJECT, "New Message on Project"),
               (NEW_MESSAGE_ON_PROPOSAL,"New Message on Proposal"))

When I set CHOICES, I get

NameError: name 'NEW_MESSAGE_ON_PROJECT' is not defined

I dont get self context here. So whats the solution?

You have to explicitly use the name of the base class:

class CustomerNotification(BaseNotification):
    NEW_PROPOSAL = 1000
    # NEW_MESSAGE_ON_PROJECT = 1001
    # NEW_MESSAGE_ON_PROPOSAL = 1002
    CHOICES = ((NEW_PROPOSAL, "New Prpposal"),
               (BaseNotification.NEW_MESSAGE_ON_PROJECT, 
                "New Message on Project"),
               (BaseNotification.NEW_MESSAGE_ON_PROPOSAL,
                "New Message on Proposal"))

The reason for this is that you get a clean namespace when starting a class definition, similar (but certainly not equal) to the namespace you get when you start a function.

This namespace does not include anything from the base class. Accessing base class members is handled when accessing members of the finished class (or their objects, for that matter).

您需要直接引用BaseNotification.NEW_PROPOSAL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM