First, your Python code is correct.
It is just a question of how languages ββare developed. Java uses a kind of automatic output of an object reference. Sometimes this can lead to strange behavior for non-java experts:
private int a; public int add(int a, int b){ return a+b;
So why in java there is a this keyword that you can use (but you are not forced to) to resolve this ambiguity.
The python team decided to force the use of the word self (or any other word, but I will explain later) to avoid such a problem. You cannot get rid of it. Although java is still a more verbose language than python, and the self keyword does not affect this assumption.
However, you are not required to use the word "self" as a reference to the current object. You can use any other word that will be the first parameter of your method (but this is very bad practice).
Here you can see two links that deeply explain why "I'm here to stay":
http://www.programiz.com/article/python-self-why
http://neopythonic.blogspot.be/2008/10/why-explicit-self-has-to-stay.html
Alexis Clarembeau
source share