Python built-in function 2-classmethod ()

Source: Internet
Author: User
Tags instance method

Help on the class Classmethod in module __builtin__:


Class Classmethod (Object)

| Classmethod (function), method

|

| Convert a function to is a class method.

|

| 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:

| def f (CLS, arg1, Arg2, ...): ...

| f = Classmethod (f)

|

| It can called either on the class (e.g. C.F ()) or in an instance

|  (e.g. 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 the Staticmethod builtin.

|

| Methods defined here:

|

| __get__ (...)

| descr.__get__ (obj[, type]), value

|

| __getattribute__ (...)

| x.__getattribute__ (' name ') <==> x.name

|

| __init__ (...)

| X.__init__ (...) initializes x; See Help (Type (x)) for signature

|

| ----------------------------------------------------------------------

| Data descriptors defined here:

|

| __func__

|

| ----------------------------------------------------------------------

| Data and other attributes defined here:

|

| __new__ = <built-in method __new__ of type object>

| T.__NEW__ (S, ...)-A new object with type S, a subtype of T

Classmethod (function)

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 (object):

@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.


Changed in version 2.4:function decorator syntax added.


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 ()).


>>> class C:

... @classmethod

... def f (self):

... print "This is a class method"

...

>>> C.F ()

This is a class method

>>> C=c ()

>>> C.F ()

This is a class method

>>> class D:

... def f (self):

... print "This was not a class method"

...

>>> D.F ()

Traceback (most recent):

File "<stdin>", line 1, in <module>

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


This article is from the "Big Cloud Technology" blog, please be sure to keep this source http://hdlptz.blog.51cto.com/12553181/1898435

Python built-in function 2-classmethod ()

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.