0
class Class:
    _member = 1

    def method(self):

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

Vishnu Upadhyay
  • 5,043
  • 1
  • 13
  • 24
Quaker
  • 1,483
  • 3
  • 20
  • 36

4 Answers4

2
class Class:
    _member = 1

    @classmethod
    def method(cls):
        print cls._member

Class.method()

And:

>>> Class().method()
1
>>> 
wwii
  • 23,232
  • 7
  • 37
  • 77
Vishnu Upadhyay
  • 5,043
  • 1
  • 13
  • 24
  • Additionally, using a ```classmethod``` to **change** a class attribute would ensure that the attribute was not *overridden* in the instance. – wwii Oct 07 '14 at 12:05
1

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.

GP89
  • 6,600
  • 4
  • 36
  • 64
  • does it mean that if I use `method()` I should send an instance of `Class` as a parameter as well? – Quaker Oct 07 '14 at 11:34
  • no, just simply call `self._member` from inside any method. I was just pointing out that if you set an attribute on the object of the same name it will shadow it, because it looks for object attributes before attributes of the class – GP89 Oct 07 '14 at 11:41
  • I understood you perfectly, it's just that when I try to use `method()` from the script that creates the instance of `Class` it tells me that `method()` takes two arguments and I provided only one (my real `method()` actually takes one method and not none as in the exapmle) – Quaker Oct 07 '14 at 11:47
  • @Quaker That sounds like a different problem. Are you calling `Class.method` instead of an instance of Class? `c = Class(); c.method()`. I guess either that or you're not passing the argument when calling the method. – GP89 Oct 07 '14 at 11:49
  • I am calling it through the instance and am passing the one really needed argument :\ – Quaker Oct 07 '14 at 12:01
  • @Quaker Maybe post the code in question. difficult to say exactly without seeing it :) – GP89 Oct 07 '14 at 12:02
  • I think I figured it, the method was static and the minute I removed the static from it, it works as expected :) – Quaker Oct 07 '14 at 12:05
  • 1
    @Quaker Ah yea, if it's a `staticmethod` then it doesn't have state (there's no self) so you can lose the `self` parameter. The reason that was happening was the argument you passed was being passed as the first arg `self`, leaving the second param unfilled. – GP89 Oct 07 '14 at 12:08
  • 1
    Just be careful - ```self._member = 'value'``` in an instance method will not change the class attribute, it will create a new instance attribute. – wwii Oct 07 '14 at 12:09
  • Right. You could use `type(self)._member = 'value'` to do that, although I'm not sure it'd often be a good idea. – GP89 Oct 07 '14 at 12:13
  • @GP89 or just create ```classmethod```s to access/change a class attribute - see @Vishnu Upadhyay's answer. – wwii Oct 07 '14 at 16:33
  • Just now when I got home I realized how irrational was trying to access a data member through a static method :| Guess it was damn too early in the morning :) – Quaker Oct 07 '14 at 19:54
0
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
nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52
0
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).