class Class:
_member = 1
def method(self):
I want to access _member
from within method()
, what is the correct way to do so?
class Class:
_member = 1
def method(self):
I want to access _member
from within method()
, what is the correct way to do so?
class Class:
_member = 1
def method(self):
print "value is ",self._member
create an instance of the class and call the method
c = Class()
c.method()
output:
value is 1
class Class:
_member = 1
@classmethod
def method(cls):
print cls._member
Class.method()
And:
>>> Class().method()
1
>>>
class Class:
_member = 1
def method(self):
print(Class._member)
Class().method()
Would give the output:
1
That one is a Class attribute, by the way. You could call the method as a bound method. You have the option of staticmethod (no first parameter required), classmethod (first one parameter is a class) and normal method (like this one).
You can use
self._member
, if it isn't an attribute of the object (inself.__dict__
) I believe it looks in the classes__dict__
next, which should contain the class attributes.