Http://www.jb51.net/article/49402.htm
Note: The Python2.7 is used.
First, the example method
An instance method is a method that an instance of a class can use. As follows:
Copy the Code code as follows:
class Foo:
def __init__ (self, name):
self.name = name
def hi (self):
Print Self.name
if __name__ = = ' __main__ ':
foo01 = Foo (' Letian ')
Foo01.hi ()
Print type (Foo)
Print type (FOO01)
Print ID (foo01)
Print ID (Foo)
The result of the operation is:
Copy the Code code as follows:
Letian
<type ' classobj ' >
<type ' instance ' >
40124704
31323448[code]
As you can see, the type of Foo is Classobj (class object, the class defined in Python is also an object), and the type of FOO01 is instance (instance). and Hi () is an instance method, so Foo01.hi () will output ' Letian '. The first parameter of an instance method defaults to self, which refers to an instance. Self is not a keyword, but an agreed-upon notation. Init () is an instance method that is called by default when the instance is built. Change the definition of Foo to the following form:
[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's identifier, and the following is its doc content:
Copy the Code code as follows:
>>> Print id.__doc__
ID (object), Integer
Return the identity of an object. This was guaranteed to be unique among
Simultaneously existing objects. (Hint:it ' s The object's memory address.)
Second, static method
A static method is a normal function that is in the namespace defined by the class and does not operate on any instance type. Use the adorner @staticmethod to define a static method. Both class objects and instances can call static methods:
Copy the Code code 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 (' Letian ')
Foo01.hi ()
Foo01.add (ON)
Foo.add (1, 2)
The results of the operation are as follows:
Copy the Code code as follows:
Letian
3
3
Note that many programming languages do not allow instances to invoke static methods.
Iii. Types of methods
A class method is a way to manipulate the class itself as an object. The class method uses the @classmethod adorner definition, whose first argument is a class, and the contract is written as a CLS. class objects and instances can call class methods:
Copy the Code code as follows:
class Foo:
name = ' Letian '
@classmethod
def hi (CLS, x):
Print Cls.name * x
if __name__ = = ' __main__ ':
foo01 = Foo ()
Foo01.hi (2)
Foo.hi (3)
The results of the operation are as follows:
Copy the Code code as follows:
Letian Letian
Letian Letian Letian
Note that many other programming languages do not allow instances to invoke class methods.
Four, super
Super is used to execute functions in the parent class, for example:
Copy the Code code 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 ()
Operation Result:
Copy the Code code as follows:
Hi,foo
Note that the Foo class must inherit a class (and the inheritance chain starts with the object class), otherwise it will error. If you change to the following form:
Copy the Code code 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 Run Times error is as follows:
Copy the Code code as follows:
......
Typeerror:must is type, not classobj
For super, see Http://docs.python.org/2/library/functions.html?highlight=super#super and Super.doc for details.
V. Class variables and instance variables
The class variable is defined after the class is defined, and the instance variable is the start of the self. For example:
Copy the Code code 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 result of the operation is:
Copy the Code code as follows:
1
0
Instances can also access class variables, as follows:
Copy the Code code as follows:
class Foo (object):
val = 0
def __init__ (self):
Pass
if __name__ = = ' __main__ ':
foo = foo ()
Print Foo.val
Print Foo.val
The results of the operation are as follows:
Copy the Code code as follows:
0
0
In addition, class variables can be accessed in the following ways:
Copy the Code code as follows:
class Foo (object):
val = 3
def __init__ (self):
Print Self.__class__.val
if __name__ = = ' __main__ ':
foo = foo ()
Operation Result:
Copy the Code code as follows:
3
You can also do this:
Copy the Code code as follows:
class Foo (object):
val = 3
def __init__ (self):
Pass
@classmethod
def Echo (CLS):
Print Cls.val
if __name__ = = ' __main__ ':
Foo.echo ()
Operation Result:
Copy the Code code as follows:
3
Vi. How to call the constructor of a parent class
Subclasses (derived classes) do not automatically invoke the Init method of the parent class (the base class), for example:
Copy the Code code 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 ()
Run the Times wrong.
There are two types of init methods that call the parent class, the first of which:
Copy the Code code 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 ()
The second type:
Copy the Code code 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 results of both of these methods are:
Copy the Code code as follows:
1
But there is a difference between the two methods.
Instance methods, static methods, class methods, class variables, and instance variables in Python