Python的classmethod如何决定它能成为一个「类装饰器」?
天蓬老师
天蓬老师 2017-04-17 17:34:03
[Python讨论组]

经常使用@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
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(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()
巴扎黑

你这段源码是哪里看到的?classmethod 应该是 built-in 用 C 写的吧,你贴的这段更像是 Python 模拟的版本。

真的源码:https://hg.python.org/cpython/file/69b41...

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号