4

Possible Duplicate:
Python: How to avoid explicit 'self'?

In python class if I need to reference the class member variable, I need to have a self. before it. This is anonying, can I not use that but reference the class member?

Thanks. Bin

Community
  • 1
  • 1
Bin Chen
  • 61,507
  • 53
  • 142
  • 183
  • You can call it whatever you want, e.g. just `s` ;) – Felix Kling Nov 11 '10 at 09:20
  • 1
    You could write a decorator that took a list of class attributes and rewrote all access points to them to load `self` first. This would be rewriting global access as local access which is not that hard but is compounded by the fact that you have to insert new bytecode instead of just swapping out ops and args. I would be very impressed if somebody pulled it off in a robust manner. – aaronasterling Nov 11 '10 at 09:38
  • On second thought, It's not as difficult as I thought. I would do it as a metaclass so that you already have access to the class attributes. I would then require use of `self` for assignments and just rewrite the loads. It wouldn't modify the stack depth so there's really nothing tricky other than rewriting the jumps. (even if it did, it would be tedious but not intellectually challenging to calculate the new one)/ – aaronasterling Nov 11 '10 at 09:55

5 Answers5

6

No.

>>> import this
...
Explicit is better than implicit.
...
Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
3

To reference a class variables, you do not need explicit self. You need it for referencing object (class instance) variables. To reference a class variable, you can simply use that class name, like this:

class C:
    x = 1
    def set(self, x):
        C.x = x

print C.x
a = C()
a.set(2)
print a.x
print C.x

the first print would give you 1, and others 2. Although that is probably not what you want/need. (Class variables are bound to a class, not object. That means they are shared between all instances of that class. Btw, using self.x in the example above would mask class variable.)

randomir
  • 17,989
  • 1
  • 40
  • 55
1

I don't know of a way to access object properties as if they're globals without unpacking it explicitly or something.

If you don't like typing self, you can name it whatever you want, a one letter name for instance.

Mark Snidovich
  • 1,055
  • 7
  • 11
  • @Bin Chen: instead of `def foo(self):`, you'd do `def foo(s):`. But you should *not* do this; `self` is what is expected and you'll confuse people by doing it as anything else. And there's no justifiable reason too, anyway. – Chris Morgan Nov 11 '10 at 09:29
1

Writing self explicitly is actually helpful. This encourages readability - one of Python's strengths. I personally consider it very useful.

A similar argument in C++ is that when you use using namespace std, just to save repetitive prefixing of the std namespace. Though this may save time, it should not be done.

So get used to writing self. And seriously, how long does it take!

user225312
  • 126,773
  • 69
  • 172
  • 181
0

Python makes the reference to self explicit for a reason. The primary goal of the Python project is readability and simplicity. The "one correct way to do it" is to have a self argument and an explicit reference.

Do not question the benevolent one ...

Jim Mitchener
  • 8,835
  • 7
  • 40
  • 56
  • 2
    This "one correct way to do it" attitude (/ annoying/wasteful way to do it..) kept me away from the language until compelled into it (pandas, sklearn, statsmodels, ..). I'd much rather use Ruby if that language had the same libraries: and this is after putting a number of data engineering and machine learning pipelines into production with python over a five year period - i.e it is not a newbie comment. – WestCoastProjects Jul 13 '17 at 13:34