python - Stopping a class variable from re-initializing when called again? -
so here example code
class hi(object) def __init__(self): self.answer = 'hi' def change(self): self.answer ='bye' print self.answer def printer(self): print self.answer class goodbye(object): def bye(self): hi().change() goodbye().bye() hi().printer()
when run code output
'bye' 'hi'
i want output
'bye' 'bye'
is there way while still initializing self.answer hi or cause re-initialize 'hi' no matter afterwards?
you using instance attributes not class attributes jonrsharpe pointed out.
try this:
class hi(object): answer = 'hi' def __init__(self): pass @classmethod def change(cls): cls.answer ='bye' print cls.answer def printer(self): print self.answer class goodbye(object): def bye(self): hi.change() goodbye().bye() hi().printer()
here answer
class attribute , change()
classmethod
Comments
Post a Comment