Self In the python parameter list is explicitly not equal to redundancy

Source: Internet
Author: User

Self is very useful in distinguishing global variables/functions from member variables/functions in objects. For example, it provides a scope mechanism. I personally think it is much clearer than Ruby's @ and @. This may be a habit, but it is indeed very similar to this in C ++ and Java.
However, self is always a problem for me. I have said here before-I once dreamed that these can be improved in python3, then it usually leads to a round of hot discussion and ends with what people call "Explicit better than implicit.
I had a conversation with Luciano Ramalho (Chairman of the Brazilian Python Organization) when I was in Brazil. He made me understand that not the ubiquitous self is bothering me, but the self in the parameter list. I think it is also called the non-pythonic ).
How it is used
Below are some simple Python Code Describes how to use a class. Copy code The Code is as follows: def F (): Pass
A = 1
Class C1 (object ):
A = 2
Def M1 (Self ):
Print a # prints '1'
Print self. A # prints '2'
F () # The Global version
Self. m2 () # Must scope other members
Def m2 (Self): Pass
OBJ = C1 ()
OBJ. M1 ()

First, let's look at F () and A. They can all be called in the global scope. Class C1 is defined to inherit from the object, which is a standard process for defining a new class (I think these will become less obvious in python3 ).
Note that the first parameters of M1 () and M2 () are both self. In python, self is not a keyword. However, according to the Convention "self" represents the address of the current object, that is, the address of the object is usually the first parameter.
Defining a in a class scope is one of the ways to create an object scope. You can also assign a value to self. A in the method of A, and allocate the memory space of this domain when you run the statement for the first time. However, it is necessary to distinguish between two versions of. If a is used inside method, a is the global version, and self. a Represents the object domain (you can also assign values to global variables within the class. I will not consider this situation for the time being ).
Similarly, an unqualified call to F () creates a global function by limiting self. m2 () calls a member function (and uses the current object address as the self variable passed to M2 ).
Now let's take a look at a class containing the method with parameters:Copy codeThe Code is as follows: Class C2 (object ):
Def m2 (self, a, B): Pass

To call this method, we create an object instance, and then use the dot expression to call m2 () on the object OBJ ():Copy codeThe Code is as follows: OBJ = c2 ()
OBJ. m2 (1, 2)

In the call process, the OBJ address is implicitly passed in M2 () as the self variable. A serious conflict arises: why is it better to define the method implicitly than explicitly, while there is no implicit problem when calling method?
Of course, I think this may be required by the method call syntax, but this means that the definition of method is very different from that of calling. Here there is neither explicit nor pythonic. When the method with the wrong number of parameters is called, we can see that:
OBJ. m2 (1)
Result error prompt:
Traceback (most recent call last ):
File "classes. py", line 9, in <module>
OBJ. m2 (1)
Typeerror: M2 () Takes exactly 3 arguments (2 given)
Because the implicit parameters of self are passed during the method call, the above error message actually means that the method should be called as follows:
C2.m2 (OBJ, 1, 2)
Even if the preceding statement runs successfully, it is not actually called. You should use the regular method call syntax to pass two parameters:
OBJ. m2 (1, 2)
The error message "M2 () Takes exactly 3 arguments (2 given)" not only makes beginners very confused, but I often get stuck after seeing it. I think this indicates that it is neither pythonic nor a conflict between method definition and call.
Desperate suggestions
Despite all the despair in the long history, what suggestions do I have?
Add self as the keyword in python3.1 (a bit more backward incompatible) (or directly use this to make C ++ and Java Program Staff hours are easier to transition ). All existing self-related rules remain unchanged.
The only change is that you do not have to put self into the method parameter list. This is the only implicit place, and others are explicit-except for method calls that remain unchanged.
It implements the consistency between method definition and call. You can define a method with the same number of parameters as the call. When an error occurs in the number of parameters passed by the method call, the error message notifies the method of the actual number of parameters, instead of an additional one.
Explicit vs. Redundancy
Before I once again hear "explicit is better than implicit", there is still a difference between making something clear and becoming redundant. We already have such a language: it has put you through countless tests, because it seems to be good at first, but there are more and more problems in the future. It is called Java.
To make everything explicit, we can use C or assembly and other languages that can precisely describe and display the running details of the machine.
The Force programmer puts self into the method parameter list and explicitly does not touch edges at all. It only forces redundant operations and does not add programming expressions (it is already known to be a method, why should we add self to the parameter list to remind us ). I think it is rigid and non-pythonic.

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.