Why must the call and declaration of a Python class member have "this"?

Source: Internet
Author: User

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

650) this.width=650; "title=" 773e10b4da51f1ce8bd6ee5c541eaa77_b.png "alt=" Wkiom1beco7shfkoaahzv51vqxi309.png "src= "Http://s4.51cto.com/wyfs02/M02/7A/FA/wKiom1bECo7ShFkOAAHZV51vqXI309.png"/>

First, the call requires the designation "this", let's make a more explicit distinction between calling 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.


Second, because this is a forced 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.


Finally, it's a bit of a performance consideration. : In the language definition of Python, a local variable is an assignment in the body of a function, 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.


Copyright belongs to the author, any reprint please contact the author for authorization.

==========================================

Welcome Attention reboot Education python actual Combat Class (March 5 classes)

Course Details click: http://www.51reboot.com/course/actual/

>>> more technical exchanges, please Dabigatran: 238757010

650) this.width=650; "title=" 91730ff8713adb3fb80d79dba4beb3de_b.jpg "alt=" Wkiom1bec4ggethuaaawwkgbnyk500.jpg "src= "Http://s4.51cto.com/wyfs02/M00/7A/FA/wKiom1bEC4GgetHuAAAwWKgBnyk500.jpg"/>

This article is from the "Reboot DevOps Development" blog, please be sure to keep this source http://opsdev.blog.51cto.com/2180875/1742684

Why must the call and declaration of a Python class member have "this"?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.