I tried this below example and need some clarification..In both the cases, I'm able to access the class variable and instance in test function.
So, assume if I have to define a literal that needs to be used across all function, which would be the better way to define..self variable or class variable?
code.py
class testclass:
classvar = 'its classvariable LITERAL'
def __init__(self,x,y):
self.z = x
self.classvar = 'its initvariable LITERAL'
self.test()
def test(self):
print('class var',testclass.classvar)
print('instance var',self.classvar)
if __name__ == '__main__':
x = testclass(2,3)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…