Search code examples
pythonoopclass-methodclass-attribute

How can I access a class data member from a method within the same class?


class Class:
    _member = 1

    def method(self):

I want to access _member from within method(), what is the correct way to do so?


Solution

  • You can use self._member, if it isn't an attribute of the object (in self.__dict__) I believe it looks in the classes __dict__ next, which should contain the class attributes.