0
class Note:
     nextseqNum = 0
     def __init__(self):
        self.text        = str
        self.dateCreated = datetime
        self.dateRead    = datetime
        self.description = str
        self.category    = str
        self.priority    = int
        self.hidden      = bool
        self.seqNum      = nextseqNum
        nextseqNum       += 1

For some reason it is throwing me

UnboundLocalError: local variable 'nextseqNum' referenced before assignment

I don't understand why. That is how you make a shared class varaible right?

1 Answer 1

3

The shared class variable needs to be accessed on the class -- It doesn't become a local variable in class methods (which explains the error message):

self.seqNum = Note.nextseqNum
Note.nextseqNum += 1

there are some shortcuts:

self.seqNum = self.nextseqNum  # Not found on self, so looked up on class.
Note.nextseqNum += 1

works because if a name isn't found on an instance, python then looks at the class. If you don't want to name the class explicitly:

self.seqNum = self.__class__.nextseqNum  # for new-style classes, type(self) == self.__class__
self.__class__.nextseqNum += 1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.