Python的classmethod如何决定它能成为一个「类装饰器」?

经常使用@classmethod,但是对它的原理不甚明白,函数装饰器原理不是很复杂

1 那么classmethod作为类装饰器,其作用流程是怎样的?

classmethod源码

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.
    """
    def __getattribute__(self, name): # real signature unknown; restored from __doc__
        """ x.__getattribute__('name') <==> x.name """
        pass

    def __get__(self, obj, type=None): # real signature unknown; restored from __doc__
        """ descr.__get__(obj[, type]) -> value """
        pass

    def __init__(self, function): # real signature unknown; restored from __doc__
        pass

    @staticmethod # known case of __new__
    def __new__(S, *more): # real signature unknown; restored from __doc__
        """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
        pass

    __func__ = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
阅读 3.7k
2 个回答

这个链接解释的不错,你看下这里
http://stackoverflow.com/questions/16774...

Python文档里关于descriptor的说明有
descriptor

Any new-style object which defines the methods __get__(), __set__(), or __delete__().
When a class attribute is a descriptor, its special binding behavior is triggered upon attribute lookup. Normally,
using a.b to get, set or delete an attribute looks up the object named b in the class dictionary for a,
but if b is a descriptor, the respective descriptor method gets called. Understanding descriptors is a key to
a deep understanding of Python because they are the basis for many features including functions, methods,
properties, class methods, static methods, and reference to super classes.
For more information about descriptors’ methods, see descriptors.

所以对于classmethod封装的声明,等同于

 class MyTest(object):
    @classmethod
    def sayHello(cls):
        print "sayHello"


MyTest.sayHello()

cm = classmethod(MyTest)
cm.__get__(None, dict).im_func.sayHello()
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题