My Summary of Python self is:
1. For member variables:
During the initialization process, if the subclass enters the initialization function of the parent class, the self. variable (in the parent class) is a member variable of the subclass. Even if this variable is not defined in the subclass, you can call the variable in the member of the subclass after initialization. Put the following code directly:
From classctob import *
Class extentd (extentc ):
Def _ init _ (Self ):
Super (extentd, self). _ init __()
Self. _ class _. myglobal = 'Global from d'
# Self. Name = 'I am d'
Def onstart (Self ):
Print self. Name
Print self. _ class _. myglobal
Print self. Test
If _ name __= = '_ main __':
D = extentd ()
D. Start ()
Bytes ---------------------------------------------------------------------------------------------------------
From classbtoa import *
Class extentc (extentb ):
Def _ init _ (Self ):
Super (extentc, self). _ init __()
Self. _ class _. myglobal = 'Global From C'
Self. Name = 'I am C'
Self. test = "I'm defined in C! "
Def onstart (Self ):
Print self. Name
Print self. _ class _. myglobal
Def start (Self ):
Super (extentc, self). Start ()
If _ name __= = 'main ':
Pass
Bytes ------------------------------------------------------------------------------------
Class basea (object ):
'''
Basea is base class
'''
Myglobal = none
Def _ init _ (Self ):
Self. Name = 'I am basea'
Self. _ class _. myglobal = 'Global from basea'
Print self. _ class _. myglobal
Def onstart (Self ):
Print self. Name
Print self. _ global
Class extentb (basea ):
Def _ init _ (Self ):
Super (extentb, self). _ init __()
Print self. Name
Print self. _ class _. myglobal
Self. _ class _. _ global = 'Global from B'
Self. Name = 'I am B'
Def onstart (Self ):
Print self. Name
Print self. _ global
Def start (Self ):
Self. onstart ()
If _ name __= = 'main ':
Pass
-------------------------------------------------------------------------------
The inheritance relationship of the above Code is: basea is a base class, extentb (basea), extentc (extentb), extentd (extentc)
In extentc, the variable self. Test is defined. This variable is not in extentd, but extentd outputs the value of this variable using print.
2. For member functions:
Use self. func to call a member function. In dive into python, there is such a description of self: "In_ Init __
Method,Self
Point to the newly created object. In other class methods, it points to the class instance called by the method"
In the above Code, D. start () will call extentc. start () (because extentd does not define start (), it can only be found in the parent class), and this function points to the START () of the parent class (), the parent class is self. onstart (), extentb will be called at this time. onstart? No! Is to call extentd. onstart (). That is to say, the onstart () function of the underlying subclass is called after a large circle. This is not suitable for C ++ programmers. This is also the reason why Python's self cannot be fully used as this in C ++.
From the above two points, we can see how selfish the self in python is. It uses every variable as its own (if not ), every time a function is called, it is first retrieved from its own function and no parent class is called (this is somewhat like reloading the parent class function)