Python static methods, class methods

Source: Internet
Author: User
Tags instance method

Today, let's talk about the special methods that exist in Python classes-static methods, class methods.

I. Definition

static method :

A simple function that meets the following requirements:

1. nested in the class.

2. There is no self parameter.

features :

1. Class calls, instance calls, and static methods do not accept automatic self parameters.

2. The information for all instances is logged, not the behavior provided for the instance.

class method :

A function that conforms to the following characteristics

1. A class call, or instance invocation, passes a parameter that is a class object.

Two. Situations requiring special methods (use)

The program needs to handle the data associated with the class, not the instance. This means that the data information is usually stored on the class itself and does not require any instances to be processed.

For example:

1. Record the number of instances that have a class created.

2. Maintain a list of all instances of a class in the current memory.

The above can also be resolved in the following ways:

Create a simple function outside of the class definition

Example 1: simple function. py

# record the number of instances created by a class def printnuminstance ():  #  Print out the number of instances created    Print Instance created:"#  Call class Data Property  class Spam:    = 0     def __init__ (self):         # each time you create a class, you add one

Output Result:

>>> a = Spam ()>>> b = Spam ()>>> c = Spam ()>>>3& Gt;>> spam.numinstance3

Because the class name is a readable global variable for simple functions, you see that the example above works correctly.

In addition, the function name becomes a global variable, so it applies only to this single module.

The disadvantages of this approach are as follows:

1. Adds an additional name to the scope of the file that can handle only a single class and cannot cope with situations where multiple classes need to be handled.

2. The function has a small direct association with the class, and the function definition may be located outside of the hundreds of lines of code.

3. The function is outside the namespace of the class, and the subclass cannot be redefined to replace or extend it.

Three. Example methods, static methods, class methods

Three methods as shown in Example 2

Example 2. method. py

#-*-coding:utf-8-*-classMethods:"""instance methods, static methods, class methods"""    defImeth (self, x):PrintSelf , xdefSmeth (x):#static methods do not require the self parameter        PrintxdefCmeth (CLS, x):#class method requires a class parameter        PrintCLS, x#to design static methods and class methods, such as the following, call the built-in functions Staticmethod and Clsssmethod.Smeth= Staticmethod (Smeth)#re-assign the method names, which override assignments that were made earlier by DefCmeth = Classmethod (Cmeth)

Three method invocation methods (instance invocation, class invocation)

1. Instance method invocation

An instance method invocation must instantiate a class before it can be called through an instance.

 from Import Methods  #  import module >>> obj = Methods ()  # instantiation >>> Obj.imeth (1 )  # instance calls <method. Methods instance at 0x02d6bd50> 1  # changed to Imeth (obj, 1)>>> methods.imeth (obj, 1) The  # class calls <method. Methods instance at 0x02d6bd50> 1  # changed to Imeth (obj, 1)
When an instance method is called, Python automatically passes the instance to the first (leftmost) parameter of the instance method self (which can also be written as a different variable, but is written by default).

2. Static method invocation

An instance parameter is not required when a static method is called.

>>> Methods.smeth (3)  #  class Call # 3   #  No instances are passed in #  instance call 3  #  instance not passed in

3. Class method invocation

class method call, Python passes the class (not the instance) to the first (leftmost) parameter of the class method CLS (default).

>>> Methods.cmeth (5)  # class calls method. Methods 5  # changed to Cmeth (Methods, 5)>>> Obj.cmeth (5)  # instance Call method. Methods 5  # into Cmeth (Methods, 5)

Python static methods, class 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.