Jesvin Jose
Jesvin Jose

Reputation: 23098

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?

Upvotes: 0

Views: 33

Answers (2)

Jonas Schäfer
Jonas Schäfer

Reputation: 20738

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).

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 600059

You'll need to reference BaseNotification.NEW_PROPOSAL directly.

Upvotes: 0

Related Questions