经常使用@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
                            
                                    Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
这个链接解释的不错,你看下这里
http://stackoverflow.com/questions/16774...
Python文档里关于descriptor的说明有
descriptor
所以对于classmethod封装的声明,等同于
你这段源码是哪里看到的?
classmethod应该是built-in用 C 写的吧,你贴的这段更像是 Python 模拟的版本。真的源码:https://hg.python.org/cpython/file/69b41...