Python function-Classmethod ()

Source: Internet
Author: User
Classmethod (function)

English Description:

Classmethod is used to specify a method of a class as a class method, without this parameter specifying the class method as an instance method, using the following method:

Class C:    @classmethod    def f (CLS, arg1, Arg2, ...): ...

The class method can be called either directly from the Class (C.F ()) or as an instance call (C (). f ()).

Version: added in python2.2, new features added in python2.4. Still available in Python3.


English Description:

Return A class method for function.

A class method receives the class as implicit first argument, just like an instance method receives the instance. To declare a class method with this idiom:

Class C:    @classmethod    def f (CLS, arg1, Arg2, ...): ...

The @classmethod form is a function decorator–see the description of function definitions on function definitions for de Tails.


It can be called either on the class (such as C.F ()) or in an instance (such as C (). f ()). The instance is ignored except for its class. If A class method is called for a derived class, the derived class object is passed as the implied first argument.


Class methods is different than C + + or Java static methods. If you want those, see Staticmethod ().


For more information on class methods, consult the documentation on the standard type hierarchy in the standard type Hiera Rchy.


New in version 2.2.


Changed in version 2.4:function decorator syntax added.


code example:

>>> class C:     ...     @classmethod ... def f (self): ...             Print "This is a class method" ...>>> c.f () The Is a class method>>> C = C () >>> C.F () the is a C Lass method>>> class D: ...     def f (self): ...             Print "This isn't a class method" ...>>> D.F () Traceback (most recent call last):  File "
 
  
   
  ", Lin E 1, in 
  
   
    
   Typeerror:unbound method F () must is called with D instance as first argument (got nothing instead ) >>> d = d () >>> D.F () This is not a class method
  
   
 
  
  • 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.