Python OOP (2)-static Method,class method and instance method

Source: Internet
Author: User
Tags class definition function definition instance method

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.

Simply said Staticmethod cannot access class properties, instance properties, equivalent to a relatively independent method, with the class actually has nothing to do with, in other words, in fact, is placed in the scope of a class function.

#!python2#-*-coding:utf-8-*-classA:v1="class Argu"        def __init__(self): self.v1="instance Argu"            deffun1 ():Print "Static Fun"fun1=Staticmethod (fun1) a=A ()#instance Invocationa.v1'instance Argu'#class callsa.v1'class Argu'a.fun1 () a.fun1 ( )

Another way to do this is to use an adorner after python2.4 decorator

#!python2#-*-coding:utf-8-*-classA:v1="class Argu"        def __init__(self): self.v1="instance Argu"@staticmethoddeffun1 ():Print "Static Fun"a=A ()#instance Invocationa.v1'instance Argu'#class callsa.v1'class Argu'a.fun1 () a.fun1 ( )

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.

Note Classmethod can access class properties and cannot access instance properties. The above variables, VAL1, are class variables in the class, and instance variables in the instance, so it is easy to confuse

Suitable for:

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

# number of record instance creation def amountinstance ():     Print " Amount of instances is:%d " %(c1.numinstance)        class  C1:    numinstance=0     def __init__ (self):        c1.numinstance+=1        a=C1 () b=C1 () c=C1 () Amountinstance (3)

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.

classMyclass:val1='Value 1'    def __init__(self): Self.val2='Value 2'    defstaticmd ():Print 'static methods, unable to access val1 and Val2'SMD=Staticmethod (staticmd)defCLASSMD (CLS):Print 'class method, class:'+ STR (CLS) +', Val1:'+ Cls.val1 +', the value of val2 cannot be accessed'cmd=Classmethod (CLASSMD) MC=MyClass () Mc.cmd () class method, class:__main__. Myclass,val1:value 1, the value of val2 cannot be accessed Myclass.cmd () class method, class:__main__. Myclass,val1:value 1, unable to access the value of Val2

Another way to do this is to use an adorner after python2.4 decorator

classMyclass:val1='Value 1'    def __init__(self): Self.val2='Value 2'@staticmethoddefstaticmd ():Print 'static methods, unable to access val1 and Val2'@classmethoddefCLASSMD (CLS):Print 'class method, class:'+ STR (CLS) +', Val1:'+ Cls.val1 +', the value of val2 cannot be accessed'MC=MyClass () MC.CLASSMD () class method, class:__main__. Myclass,val1:value 1, the value of val2 cannot be accessed MYCLASS.CLASSMD () class method, class:__main__. Myclass,val1:value 1, the value of val2 cannot be accessed#The val1 of the instance is not the same as the val1 of the class, and the class method can access the class Val1Mc.val1='Value changed'Mc.classmd () class method, class:__main__. Myclass,val1:value 1, the value of val2 cannot be accessed MYCLASS.CLASSMD () class method, class:__main__. Myclass,val1:value 1, the value of val2 cannot be accessed Myclass.val1='Value changed'Mc.classmd () class method, class:__main__. Myclass,val1:value changed, cannot access the value of Val2 MYCLASS.CLASSMD () class method, class:__main__. Myclass,val1:value changed, unable to access the value of Val2

Finally, the implementation and invocation of instance method, static method and class method are summarized.

#!python2#-*-coding:utf-8-*-classMethods ():defim (SELF,V2): Self.v2=v2Print "Call instance method:%d"%v2 @staticmethoddefSM (v2):Print "Call static method:%d"%v2 @classmethoddefcm (CLS,V2):Print "Call class method:%d"%v2 obj=Methods ()#instance method call#instance method calls must instantiate a class to be invoked through an instanceobj.im (1) Call instance method:1methods.im (obj,1) Call instance method:1#static method Call#No instance arguments are required for static method callsObj.sm (2) Call static method:2Methods.sm (2) Call static method:2#class method call#class method call, Python passes the class (not the instance) to the first (leftmost) parameter of the class method CLS (default)obj.cm (3) CallclassMethod:3methods.cm (3) CallclassMethod:3

Python OOP (2)-static Method,class method and instance method

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.