The self python

Good article: https://blog.csdn.net/CLHugh/article/details/75000104

Important points:

Two underscores, in Python, if the instance variable name starts with, it becomes a private variable (private), can be accessed only internal, not external access

You will see examples of a variable name to begin with an underscore, such as _name, such as instance variables outside is accessible, however, in accordance with the provisions of the convention, when you see this variable, meaning that, "Although I can It is accessed, but please treat me as a private variable, not free to access. "

 

Examples of representatives of the class of self, rather than a class. 

class the Test:
     DEF PPR (Self):
         Print (Self)
         Print (Self. the __class__ ) 

T = the Test () 
t.ppr () 
execution result:
 < __main__ .test AT 0x000000000284E080 Object> 
< class  ' in __main the Test __. ' > 

from above example can clearly be seen, self represent instances of classes. And Self. __Class__ points to class . 
Note: the self into this, the result is the same, but the best use Python convention of self. 

Within the Python interpreter, when we call t.ppr (), in fact, Python interpreted as Test.ppr (t), which is replaced with examples of the self class.

When inherited, passed in which instance, it is that the incoming instance, rather than to define an instance of the class of self.

Guess you like

Origin www.cnblogs.com/Stephen-Qin/p/11079270.html