This article mainly introduces the python instance method, static method, class method, class variable and instance variable analysis. if you need it, refer to the following note: Python2.7 is used.
I. instance method
An instance method is a method that can be used by a class instance. As follows:
The code is as follows:
Class Foo:
Def _ init _ (self, name ):
Self. name = name
Def hi (self ):
Print self. name
If _ name _ = '_ main __':
Foo01 = Foo ('letid ')
Foo01.hi ()
Print type (Foo)
Print type (foo01)
Print id (foo01)
Print id (Foo)
The running result is:
The code is as follows:
Letian
40124704
31323448 [code]
As you can see, Foo's type is classobj (class object, the class defined in python is also an object), and foo01's type is instance ). However, hi () is an instance method, so foo01.hi () will output 'letid '. The first parameter of the instance method defaults to self, representing the instance. Self is not a keyword, but a way of writing conventions. Init () is the default instance method called when an instance is generated. Change the definition of Foo to the following format:
[Code] class Foo:
Def _ init _ (this, name ):
This. name = name
Def hi (here ):
Print here. name
The operation is still correct. The built-in function id is used to view the object identifier. The following is its doc content:
The code is as follows:
>>> Print id. _ doc __
Id (object)-> integer
Return the identity of an object. This is guaranteed to be unique
Simultaneously existing objects. (Hint: it's the object's memory address .)
II. static methods
A static method is a common function that is located in the namespace defined by the class and does not operate on any instance type. Use the decorator @ staticmethod to define the static method. Both class object and instance can call static methods:
The code is as follows:
Class Foo:
Def _ init _ (self, name ):
Self. name = name
Def hi (self ):
Print self. name
@ Staticmethod
Def add (a, B ):
Print a + B
If _ name _ = '_ main __':
Foo01 = Foo ('letid ')
Foo01.hi ()
Foo01.add (1, 2)
Foo. add (1, 2)
The running result is as follows:
The code is as follows:
Letian
3
3
Note that many programming languages do not allow instances to call static methods.
III. class methods
The class method is to operate the class itself as an object. The class method is defined by @ classmethod. The first parameter is the class, and the Convention is written as cls. Class object and instance can call the class method:
The code is as follows:
Class Foo:
Name = 'letid'
@ Classmethod
Def hi (cls, x ):
Print cls. name * x
If _ name _ = '_ main __':
Foo01 = Foo ()
Foo01.hi (2)
Foo. hi (3)
The running result is as follows:
The code is as follows:
Letian
Letian
Note that many other programming languages do not allow instances to call class methods.
IV. super
Super is used to execute functions in the parent class. for example:
The code is as follows:
Class Foo (object ):
Def hi (self ):
Print 'Hi, Foo'
Class Foo2 (Foo ):
Def hi (self ):
Super (Foo2, self). hi ()
If _ name _ = '_ main __':
Foo2 = Foo2 ()
Foo2.hi ()
Running result:
The code is as follows:
Hi, Foo
Note that the Foo class must inherit a class (and the inheritance chain starts with the object class); otherwise, an error is returned. If it is changed to the following format:
The code is as follows:
Class Foo:
Def hi (self ):
Print 'Hi, Foo'
Class Foo2 (Foo ):
Def hi (self ):
Super (Foo2, self). hi ()
If _ name _ = '_ main __':
Foo2 = Foo2 ()
Foo2.hi ()
The following error is reported during running:
The code is as follows:
......
TypeError: must be type, not classobj
About super, please see http://docs.python.org/2/library/functions.html? Highlight?super=super and super.doc.
V. class variables and instance variables
After the class is defined, the instance variable starts with self. For example:
The code is as follows:
Class Foo (object ):
Val = 0
Def _ init _ (self ):
Self. val = 1
If _ name _ = '_ main __':
Foo = Foo ()
Print foo. val
Print Foo. val
The running result is:
The code is as follows:
1
0
The instance can also contain the following types of variables:
The code is as follows:
Class Foo (object ):
Val = 0
Def _ init _ (self ):
Pass
If _ name _ = '_ main __':
Foo = Foo ()
Print foo. val
Print Foo. val
The running result is as follows:
The code is as follows:
0
0
In addition, you can use the following methods to configure a category variable:
The code is as follows:
Class Foo (object ):
Val = 3
Def _ init _ (self ):
Print self. _ class _. val
If _ name _ = '_ main __':
Foo = Foo ()
Running result:
The code is as follows:
3
You can also do this:
The code is as follows:
Class Foo (object ):
Val = 3
Def _ init _ (self ):
Pass
@ Classmethod
Def echo (cls ):
Print cls. val
If _ name _ = '_ main __':
Foo. echo ()
Running result:
The code is as follows:
3
6. how to call the constructor of the parent class
Subclass (derived class) does not automatically call the init method of the parent class (base class), for example:
The code is as follows:
Class Foo (object ):
Def _ init _ (self ):
Self. val = 1
Class Foo2 (Foo ):
Def _ init _ (self ):
Print self. val
If _ name _ = '_ main __':
Foo2 = Foo2 ()
An error is reported during running.
The init method of the parent class can be called in either of the following ways:
The code is as follows:
Class Foo (object ):
Def _ init _ (self ):
Self. val = 1
Class Foo2 (Foo ):
Def _ init _ (self ):
Foo. _ init _ (self)
Print self. val
If _ name _ = '_ main __':
Foo2 = Foo2 ()
Second:
The code is as follows:
Class Foo (object ):
Def _ init _ (self ):
Self. val = 1
Class Foo2 (Foo ):
Def _ init _ (self ):
Super (Foo2, self). _ init __()
Print self. val
If _ name _ = '_ main __':
Foo2 = Foo2 ()
The running results of these two methods are:
The code is as follows:
1
However, the two methods are different.