This article refer to http://www.cnblogs.com/linuxcat/archive/2012/01/05/2220997.html
The self in Python is generally used in methods of classes, of course, a separate function can also be added to the self parameter, but if there is no need is not to give yourself trouble, because the call will be passed in an empty parameter (I used to do, ashamed).
Self is necessary when defining a method of a class, although it is not necessary to pass in the appropriate arguments when calling. Of course, self can also be replaced by other words that you like, unless you don't want others to read your own program.
# Coding=utf-8 class Person : def __init__ (self,name): self.name=name def SayHello (self): Print'Hello, my name is:', SELF.NAMEP=person (' Bill') P.sayhello ()
Self refers to the class instance object itself, not the class itself. Personally, the role of self is primarily to mark the role of public member variables, and we know that in C + +, you can define a property of a class by using private or publicly, a class member variable that can be used by all methods in the class. However, Python is a dynamic language that does not have to be defined or declared before using variables, so if I define a variable in a method of a class, this variable is the only one that belongs to this method, and if other methods want to use it? Sorry, I can't use it. and using self can solve this problem, self will tell all the methods: This variable is common to us, can be used freely yo
#Coding=utf-8classPerson :def __init__(self,name): Self.name=name Mame1=name#name1 only belongs to _init_ method, SayHello cannot use defSayHello (self):Print 'Hello, my name is:', SELF.NAMEP=person ('Bill') P.sayhello ()
A personal understanding of the self of Python