Accessing python class member inside methods -
this code
class mine: def __init__(self): var = "hello" def mfx(self): var += "a method called" print var me = mine()
when callme.mfx()
gives following error
>>> me.mfx() traceback (most recent call last): file "<pyshell#1>", line 1, in <module> me.mfx() file "d:\more\pythonnnn\text.py", line 5, in mfx var += "a method called" unboundlocalerror: local variable 'var' referenced before assignment >>>
i need var use inside class. don't want self.var . why happening? how can make variable can used everywhere inside class. using python2.7
you should qualify var. (self.var
instead of var
)
class mine: def __init__(self): self.var = "hello" def mfx(self): self.var += "a method called" print self.var me = mine() me.mfx()
Comments
Post a Comment