The reason for Python to self
There is an obvious difference between the methods of a Python class and the normal one, in which the method of the class must have an extra first argument (self), but you do not have to assign a value to this parameter when calling this method (it is better than the hidden is raised). This particular parameter of the Python class method refers to the object itself, and according to Python's convention, it is represented by self. (Of course, we can use any other name instead, just the specification and standard where we recommend that we use self in unison)
Why does Python assign a value to self and you don't have to assign a value to self?
Example: Create a class MyClass, instantiate MyClass get MyObject this object, and then call this object method Myobject.method (ARG1,ARG2) , this process, Python will automatically switch to Myclass.mehod (MYOBJECT,ARG1,ARG2)
This is the principle of the self of Python. Even if the method of your class does not require any arguments, it is necessary to define a self parameter for the method, although we do not need to assign a value to this parameter while instantiating the call.
Instance:
class Python:
def Selfdemo (self):
print ' python,why self? '
p = Python ()
P.selfdemo ()
Output:python,why self?
Take the P.selfdemo () with a parameter such as: P.selfdemo (p) to get the same output result
If I get rid of self,
class Python:
def selfdemo ():
print ' python,why self? '
p = Python ()
P.selfdemo ()
This will be an error:Typeerror:selfdemo () takes no arguments (1 given)
Extended
Self is not a keyword in python. Self represents the address of the current object. Self can avoid global variables caused by unqualified calls.
Did you not know after Python3 that I was hidden? The way I feel in the Python class is to take a self, which is a bit stiff.
The first thing to be clear is that self is only available in the methods of the class, and independent functions or methods do not have to have the. Self is necessary when defining a method of a class, although it is not necessary to pass in the appropriate arguments when calling.
Self name is not necessary, in Python, self is not a keyword, you can be defined as a or B or other names can be, but the agreement idiomatic, do not engage in alternative, we will not understand. The following example changes self to myname without errors:
1 class Person:2 def _init_ (myname,name): 3 myname.name=name4 def sayhello (myname): 5 print ' My name is: ', Myname.name6 p=person (' Bill ') 7 print P
Self refers to the class instance object itself (note: not the class itself).
1 class Person:2 def _init_ (self,name): 3 self.name=name4 def sayhello (self): 5 print ' My name is: ', Self.name6 p=person (' Bill ') 7 print P
self
1 class Person:2 def _init_ (self,name): 3 self.name=name4 def sayhello (self): 5 print ' My name is: ', Self.name6 p=person (' Bill ') 7 P1 = person (' Apple ') 8 print P
This article is from the "Can't Grow" blog, be sure to keep this source http://tfbaby.blog.51cto.com/3125450/1902950
The reason for Python to self