This design of Python is the author from the Modula-3, in the case of later use, it seems that the design is relatively successful. Let's discuss this with Python versus C + +:
Modula-3 is a language designed by the Digital Equipment Company (DEC) System Research Center (SRC) and the Olivetti Research Center (ORC) in the late 80.
Although this language is not widely used in the industry, but the language to the later invented Java, Python, Caml, C # has a lot of influence
1. First, call the time to specify "This", let us more clearly distinguish the invocation is a member variable, a function or a local variable, a function.
When we read self.x or Self.meth (), even if we do not know the definition of class can be 100% to determine that a member variable or method is called here. Let's imagine that in C + +, we can verify that variables are member variables by looking at and excluding all local variables (assuming that the global variable is rare, even if it is "g_", which is easily recognizable).
In Python, because the variable does not require additional definitions, this method fails, and the only way to do this is to look at the definition of class, so this is necessary. Many of the code specifications for C + + and Java stipulate that member functions or variables should have a m_ prefix, and we can see that this coding specification is necessary.
2. Second, because this is a mandatory explicit reference to the current instance, Python does not need to introduce any new syntax to reference or invoke the member variables and functions of other classes, including the parent class.
In C + +, if we want to invoke the function of the parent class in the subclass, we need to use: this operator; in Python we write baseclass.methodname (self, <argument list>). This syntax of Python is especially useful when implementing __init__ () of subclasses, for example, we often need to override the parent class's function in the subclass and call the parent class function itself.
3. Finally, there is a slight performance consideration: In Python's language definition, a local variable is assigned to a function body, and there is no explicitly declared variable at the global value.
Of course, in language design, we have a lot of ways to tell the interpreter, the compiler whether a variable assignment is a local variable or a member variable, but for the performance of the explanatory language, it is best to semantic provisions. The declaration that must be displayed for C + + with variables solves this problem, but Python does not have an extra variable-declaration syntax, which is too much of a bonus if you simply introduce a variable like C + + to the above-mentioned purpose.
So, Self.var this syntax is a very elegant solution. We write the syntax of Self.var in the function, and the interpreter does not have to struggle to find the variable in the global scope. In other words, local variables and member variables exist in two different namespaces, and we need to tell Python which space we are using.
Python Learning Group: 238757010
Why must the call and declaration of a Python class member have "this"?