I have been learning Python by following some pygame tutorials.
Therein I found extensive use of the keyword self, and coming from a primarily Java background, I find that I keep forgetting to type self. For example, instead of self.rect.centerx
I would type rect.centerx
, because, to me, rect is already a member variable of the class.
The Java parallel I can think of for this situation is having to prefix all references to member variables with this.
Am I stuck prefixing all member variables with self, or is there a way to declare them that would allow me to avoid having to do so?
Even if what I am suggesting isn't pythonic, I'd still like to know if it is possible.
I have taken a look at these related SO questions, but they don't quite answer what I am after:
In Java terms: Python doesn't have member functions, all class functions are static, and are called with a reference to the actual class instance as first argument when invoked as member function.
This means that when your code has a class MyClass
and you build an instance m = MyClass()
, calling m.do_something()
will be executed as MyClass.do_something(m)
.
Also note that this first argument can technically be called anything you want, but the convention is to use self
, and you should stick to that convention if you want others (including your future self) to be able to easily read your code.
The result is there's never any confusion over what's a member and what's not, even without the full class definition visible. This leads to useful properties, such as: you can't add members which accidentally shadow non-members and thereby break code.
One extreme example: you can write a class without any knowledge of what base classes it might have, and always know whether you are accessing a member or not:
class A(some_function()):
def f(self):
self.member = 42
self.method()
That's the complete code! (some_function returns the type used as a base.)
Another, where the methods of a class are dynamically composed:
class B(object):
pass
print B()
# <__main__.B object at 0xb7e4082c>
def B_init(self):
self.answer = 42
def B_str(self):
return "<The answer is %s.>" % self.answer
# notice these functions require no knowledge of the actual class
# how hard are they to read and realize that "members" are used?
B.__init__ = B_init
B.__str__ = B_str
print B()
# <The answer is 42.>
Remember, both of these examples are extreme and you won't see them every day, nor am I suggesting you should often write code like this, but they do clearly show aspects of self being explicitly required.