Self explicit in Python argument list is not equal to redundant _python

Source: Internet
Author: User
Tags in python
Self is useful for distinguishing between global variables/functions and member variables/functions in objects. For example, it provides a scoping mechanism that I personally think is much clearer than Ruby's @ and @@ 清晰 which may be a habit, but it's really similar to this in C + + java.
However, self always has a place that bothers me, as I have said here before-I have fantasized that these things can be improved in Python3, and then it usually triggers a round of hot talk and ends up with what people call "obvious better than hidden."
I had a conversation with Luciano Ramalho (president of the Brazilian Python group) when I was in Brazil. He made me understand that not everywhere self bothers me, but self in the parameter list, which I think is also called a pythonic (un-pythonic).
How it is used
Here are some simple Python code that shows how to use a class.
Copy Code code 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
def m2 (self): Pass
obj = C1 ()
OBJ.M1 ()

First look at f () and a, both of which can be invoked in the global scope. Class C1 is defined as inheriting from object, which is the standard procedure for defining a new class (which I think will become even less noticeable in Python3).
Note that the first argument to M1 () and M2 () is self. In Python, self is not a keyword. However, the Convention "Self" represents the address of the current object, i.e. the address of the object is usually the first parameter.
Defining a on a class scope is one way to create an object scope. You can also assign a value to SELF.A in the method of a, and the memory space of the domain is allocated the first time the statement is run. However, it is necessary to distinguish between the two versions of A. If you use a within method, then this is a global version, and SELF.A is the object domain (you can also assign values to global variables within the class, which I do not consider here).
Similarly, a unqualified call to F () creates a global function, which is called by the member function (the current object address as the self variable passed to M2 ()) by qualifying self.m2 ().
Now let's look at a class with parameter method:
Copy Code code as follows:

Class C2 (object):
def m2 (self, A, b): Pass

To invoke this method, we created an object instance and then called the M2 () on the object obj using the point expression:
Copy Code code as follows:

obj = C2 ()
OBJ.M2 (1,2)

In the invocation, the address of obj is implicitly passed in M2 () as the self variable, where a serious paradox is encountered: why is implicit better than explicit when defining method, and when method is called implicit, there is no problem?
Of course I think this may be required by method to invoke syntax, but this means that the definition and invocation of method are very different, there is neither "explicit" nor pythonic. You can see it when you call method with the wrong number of arguments:
OBJ.M2 (1)
The resulting error message is:
Traceback (most recent call last):
File "classes.py", line 9, in <module>
OBJ.M2 (1)
TYPEERROR:M2 () takes exactly 3 arguments (2 given)
Because of the implicit parameter passing of self during method invocation, the error message above actually says that method should be called:
C2.M2 (obj,1,2)
Even if the above line statement succeeds, it is certainly not the actual invocation. You should use the normal method call syntax, that is, pass two parameters:
OBJ.M2 (1,2)
Error message "m2 () takes exactly 3 arguments (2 given)" Not only makes beginners very confused, every time I see it also often Meng. I think this shows that it is not pythonic, but also directly to the definition and invocation of the contradictions.
Desperate advice.
Despite all the despair in the long history, what advice do I have?
Add self to the keyword in Python3.1 (a bit more backward-incompatible) (or use this directly to make it easier for C + + and Java programmers to transition). All existing rules relating to self are unchanged.
The only change is: You don't have to put self into the method argument list. This is the only implicit place where all the others are explicit-except that the method call is still unchanged.
It implements the consistency of method definitions and invocations. You can define a method that has the same number of parameters as the call. When there is an error in the number of arguments passed by the call method, the error message notifies method that the number of actual arguments should be included, not one more.
Explicit vs. redundancy
There is a difference between making something clear and becoming redundant before I hear it again "better than hidden." We already have a language: it has taken you through countless tests, because it seems to be good at first, but then more and more. It's called Java.
If you want everything to be explicit, we can use C or the assembly and other languages that accurately describe and display the details of the machine's internal operation.
Forcing the programmer to put self in the method argument list is nothing more than explicit, it only enforces redundancy and does not increase the way programming is expressed (already known as a methods, why add self to the argument list to remind us). I think, it is inflexible, is also a 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.