I've read throughly many posts here on the difference between class methods and instance methods. I understand them conceptually, but now I'm trying to figure out the more subtle nuances. In the example below, if I call User.createUser('xyz'), where does userId get stored? does it go to (*) below, i.e. on the instance? Or would it be as if I inserted userId = None where I have the comment # placeholder and the userId passed in from User.createUser('xyz') then assigns the class variable userId with 'xyz'?
class User():
# placeholder
def __init__(self, userId):
self.userId = userId # (*)
@classmethod
def createUser(cls, userId):
if not isValid(userId): # isValid may or may not be part of the class
return False
else:
return cls(userId)
# ... other methods
cls(userId)instantiates your class, anduserIdgets passed to__init__. So forUser, theuserIdwill be stored in the new instance.cls(userId)works exactly the same as if you didUser(userId), and where that value gets stored from there works exactly the same too…class User:without parenthesis unless if you want this class to inherent from another class then you use parenthesisclass User(User2):