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 MyClassand you build an instancem = MyClass(), callingm.do_something()will be executed asMyClass.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:
That’s the complete code! (some_function returns the type used as a base.)
Another, where the methods of a class are dynamically composed:
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.