Analysis of python--instance method, static method, class method, class variable and instance variable

Source: Internet
Author: User
Tags instance method

Overview:

    1. An instance method is a method that an instance of a class can use.
    2. 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.
    3. 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.
    4. Super is used to execute functions in the parent class.
    5. The class variable is defined after the class is defined, and the instance variable is the start of the self.
    6. How to call the constructor of a parent class: a subclass (derived class) does not automatically invoke the Init method of the parent class (the base class).

First, the example method

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

Copy CodeThe code is 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:


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:


>>> 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 CodeThe 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 (' Letian ')
Foo01.hi ()
Foo01.add (ON)
Foo.add (1, 2)


The results of the operation are as follows:

Copy CodeThe code is 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 CodeThe code is 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 CodeThe code is 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 CodeThe 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 ()


Operation Result:

Copy CodeThe 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 it will error. If you change to the following form:

Copy CodeThe 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 Run Times error is as follows:

Copy CodeThe code is 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 CodeThe 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 result of the operation is:

Copy CodeThe code is as follows:
1
0


Instances can also access class variables, as follows:

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

Copy CodeThe code is as follows:
0
0


In addition, class variables can be accessed in the following ways:

Copy CodeThe code is as follows:
Class Foo (object):
val = 3
def __init__ (self):
Print Self.__class__.val
if __name__ = = ' __main__ ':
foo = foo ()


Operation Result:

Copy CodeThe code is as follows:
3


You can also do this:

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


Operation Result:

Copy CodeThe code is 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 CodeThe 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 ()


Run the Times wrong.

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

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


The second type:

Copy CodeThe 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 results of both of these methods are:

Copy CodeThe code is as follows:
1


But there is a difference between the two methods.

Analysis of python--instance method, static method, class method, class variable and instance variable

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.