Reputation: 153
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?
Upvotes: 0
Views: 70
Reputation: 310247
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
Upvotes: 3