Analysis of instance method, static method, class method, class variable and instance variable in Python _python

Source: Internet
Author: User
Tags instance method in python

Note: The Python2.7 is used.

One, instance method

An instance method is a method that an instance of a class can use. As follows:

Copy 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 results of the operation are:
Copy 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 itself is also an object), and the foo01 type is instance (instance). and Hi () is an instance method, so Foo01.hi () will output ' Letian '. The first argument of an instance method defaults to self, which refers to an instance. Self is not a keyword, but an agreement. Init () is an instance method that is invoked by default when the instance is generated. 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 identifier of the object, and the following is its doc content:
Copy Code code as follows:

>>> Print id.__doc__
ID (object)-> integer

Return is the identity of an object. This is guaranteed 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 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 (1,2)
Foo.add (1, 2)


The results of the operation are as follows:

Copy Code code as follows:

Letian
3
3

Note that many programming languages do not allow instances to invoke static methods.

Third, the class method

A class method is a method of manipulating the class itself as an object. The class method uses the @classmethod adorner definition, whose first argument is a class, and the Convention is written as a CLS. class objects and instances can call the class method:

Copy 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 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 perform functions in the parent class, for example:

Copy 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 ()


Run Result:
Copy Code code as follows:

Hi,foo

Note that the Foo class must inherit a class (and the inheritance chain starts with the object class), or an error occurs. If you change to the following form:
Copy 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 are wrong as follows:
Copy 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.


Class variables and instance variables

The class variable is defined after the class is defined, and the instance variable is assumed to start with the self. For example:

Copy 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 results of the operation are:
Copy Code code as follows:

1
0

Instances can also access class variables, as follows:
Copy 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 Code code as follows:

0
0

In addition, class variables can be accessed in the following ways:
Copy Code code as follows:

Class Foo (object):
val = 3
def __init__ (self):
Print Self.__class__.val
if __name__ = = ' __main__ ':
foo = foo ()

Run Result:
Copy Code code as follows:

3

You can also do this:
Copy 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 ()

Run Result:
Copy Code code as follows:

3

Vi. how to invoke the constructor of the parent class

Subclasses (derived classes) do not automatically invoke the Init method of the parent class (the base class), for example:

Copy 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 times wrong.

There are two types of init methods that invoke the parent class, the first of which:

Copy 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 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 ()


Both of these methods run the following results:
Copy Code code as follows:

1

But there is a difference between the two methods.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.