The role and difference of @staticmethod and @classmethod

Source: Internet
Author: User

In general, to use a method of a class, you need to instantiate an object before calling the method.


Instead of using @staticmethod or @classmethod, you can call it without instantiating it, directly from the class name. Method Name ().


Since both @staticmethod and @classmethod can be called directly from the class name. Method Name (), what's the difference between them?

In terms of their use,

-

@staticmethod does not need to represent the self of its own object and the CLS parameters of its own class, just as with a function.

-

@classmethod also does not require the self parameter, but the first parameter needs to be a CLS parameter that represents its own class.

-

@classmethod is a function modifier, which means that next is a class method, which is called an instance method for what we usually see. The first parameter of the class method is CLS, and the first argument of the instance method is self, which represents an instance of the class.

-

A normal object method requires at least one self parameter, which represents the Class object instance

-

Class methods have the class variable CLS incoming, so that some related processing can be done with the CLS. And with subclass inheritance, when the class method is called, the passed-in class variable CLS is a subclass, not a parent class. For a class method, it can be called through a class, like Test.foo ()


If you want to invoke some of the property methods of this class in @staticmethod, you can only direct the class name. The name of the property or class. Method name.


code example:

#!/usr/local/python3/bin/python3class door (object):    a = 1     def __init__ (self,num,status):         self.num =  num        self.status = status     def open (self):        self.var = 0         self.status =  ' Open '     def close (self) :        self.status =  ' Closed '     def  __test (self):        print  (' __test ')       @classmethod     def test (CLS):         Print ("Class test") #不需要self参数, but the first parameter needs to be a CLS parameter that represents its own class.              &nbSp;  print (CLS.A)      @staticmethod     def test2 ():         print ("Static test2") #不需要自身对象参数, directly using class name + class variable invocation                  print (DOOR.A)          door.test () D1 = door (1, "closed") d1.test () #通过类名调用和通过实例调用d2  =  door (2, "closed") Door.test2 () D2.test2 ()


Output Result:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/71/58/wKiom1XLeBPTZxUPAABmL6p9YFE210.jpg "title=" az3fr~ })}r@k_b$c8) xzs{o.png "alt=" Wkiom1xlebptzxupaabml6p9yfe210.jpg "/>

This article from "Breakthrough Comfort zone" blog, reproduced please contact the author!

The role and difference of @staticmethod and @classmethod

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.