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

查看:110
本文介绍了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

解决方案

这个链接解释的不错,你看下这里
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()

这篇关于Python的classmethod如何决定它能成为一个「类装饰器」?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆