码迷,mamicode.com
首页 > 编程语言 > 详细

python内置函数2-classmethod()

时间:2017-02-17 00:59:35      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:function   either   receives   python   called   

Help on class classmethod in module __builtin__:


class classmethod(object)

 |  classmethod(function) -> method

 |  

 |  Convert a function to be 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, use this idiom:

 |  

 |    class C:

 |        def f(cls, arg1, arg2, ...): ...

 |        f = classmethod(f)

 |  

 |  It can be called either on the class (e.g. C.f()) or on 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 are 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, use this idiom:


class C(object):

    @classmethod

    def f(cls, arg1, arg2, ...):

        ...

The @classmethod form is a function decorator – see the description of function definitions in Function definitions for details.


It can be called either on the class (such as C.f()) or on 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 are different than C++ or Java static methods. If you want those, see staticmethod() in this section.


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


Changed in version 2.4: Function decorator syntax added.


中文说明:

classmethod是用来指定一个类的方法为类方法,没有此参数指定的类的方法为实例方法,使用方法如下:

class C: 

    @classmethod

    def f(cls, arg1, arg2, ...): ... 

类方法既可以直接类调用(C.f()),也可以进行实例调用(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 is not a class method"

... 

>>> D.f()

Traceback (most recent call last):

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

TypeError: unbound method f() must be called with D instance as first argument (got nothing instead)

>>> d=D()

>>> d.f()

This is not a class method


本文出自 “大云技术” 博客,请务必保留此出处http://hdlptz.blog.51cto.com/12553181/1898435

python内置函数2-classmethod()

标签:function   either   receives   python   called   

原文地址:http://hdlptz.blog.51cto.com/12553181/1898435

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!