实例方法的装饰器可以访问该类吗?


109

我有一些大致如下的内容。基本上,我需要从在其定义中的实例方法上使用的装饰器访问实例方法的类。

def decorator(view):
    # do something that requires view's class
    print view.im_class
    return view

class ModelA(object):
    @decorator
    def a_method(self):
        # do some stuff
        pass

原样的代码给出:

AttributeError:“函数”对象没有属性“ im_class”

我发现了类似的问题/答案-Python装饰器使函数忘记了它属于Python装饰器中的类Get类 -但它们依赖于一种变通方法,该方法通过在运行时抢夺第一个参数来获取实例。就我而言,我将基于从其类收集的信息来调用该方法,因此我不能等待调用进入。

Answers:


68

如果您使用的是Python 2.6或更高版本,则可以使用类装饰器,也许是这样的(警告:未经测试的代码)。

def class_decorator(cls):
   for name, method in cls.__dict__.iteritems():
        if hasattr(method, "use_class"):
            # do something with the method and class
            print name, cls
   return cls

def method_decorator(view):
    # mark the method as something that requires view's class
    view.use_class = True
    return view

@class_decorator
class ModelA(object):
    @method_decorator
    def a_method(self):
        # do some stuff
        pass

方法装饰器通过添加“ use_class”属性将方法标记为感兴趣的方法-函数和方法也是对象,因此可以向其附加其他元数据。

创建类之后,类装饰器将遍历所有方法并对已标记的方法执行所需的任何操作。

如果您希望所有方法都受影响,则可以省去方法装饰器,而只使用类装饰器。


2
谢谢,我认为这是走的路。对于要使用此装饰器的任何类,只需增加一行代码。也许我可以使用自定义元类并在新的 ... 期间执行相同的检查?
卡尔·G

3
尝试将其与staticmethod或classmethod一起使用的任何人都想阅读此PEP:python.org/dev/peps/pep-0232 不确定是否有可能,因为您无法在class / static方法上设置属性,我认为它们会吞噬将任何自定义函数属性应用于函数时。
卡尔·G

正是我想要的,基于我的基于DBM的ORM ...谢谢,老兄。
Coyote12年

您应该inspect.getmro(cls)用来处理类装饰器中的所有基类以支持继承。
schlamar 2013年

1
哦,实际上看起来像是inspect救援stackoverflow.com/a/1911287/202168
Anentropic 2014年

16

从python 3.6开始,您可以使用object.__set_name__一种非常简单的方法来完成此任务。该文档指出__set_name__“在创建拥有类的所有者时调用”。这是一个例子:

class class_decorator:
    def __init__(self, fn):
        self.fn = fn

    def __set_name__(self, owner, name):
        # do something with owner, i.e.
        print(f"decorating {self.fn} and using {owner}")
        self.fn.class_name = owner.__name__

        # then replace ourself with the original method
        setattr(owner, name, self.fn)

注意,它在类创建时被调用:

>>> class A:
...     @class_decorator
...     def hello(self, x=42):
...         return x
...
decorating <function A.hello at 0x7f9bedf66bf8> and using <class '__main__.A'>
>>> A.hello
<function __main__.A.hello(self, x=42)>
>>> A.hello.class_name
'A'
>>> a = A()
>>> a.hello()
42

如果您想更多地了解如何创建类,尤其是何时__set_name__调用类,可以参考“创建类对象”文档


1
将修饰符与参数一起使用时会是什么样子?例如@class_decorator('test', foo='bar')
-luckydonald

2
@luckydonald您可以采用类似于带有参数的普通装饰器的方法来处理它。只要有def decorator(*args, **kwds): class Descriptor: ...; return Descriptor
马特Eding

哇,非常感谢。__set_name__尽管我已经使用Python 3.6+很长时间了,但还是不知道。
kawing-chiu

此方法有一个缺点:静态检查器完全不了解这一点。Mypy认为这hello不是方法,而是type的对象class_decorator
kawing-chiu

@ kawing-chiu如果没有其他作用,则可以使用if TYPE_CHECKING将定义class_decorator为返回正确类型的普通装饰器。
tyrion

15

正如其他人指出的那样,在调用装饰器时尚未创建该类。但是,可以用装饰器参数注释函数对象,然后在元类的__new__方法中重新装饰函数。__dict__至少对我来说,您将需要直接访问该函数的属性,func.foo = 1从而导致AttributeError。


6
setattr应该使用而不是访问__dict__
schlamar

7

正如马克所说:

  1. 任何称为BEFORE类的装饰器都将被构建,因此装饰器未知。
  2. 我们可以标记这些方法,并在以后进行任何必要的后处理。
  3. 我们有两个后处理选项:在类定义的末尾或在应用程序运行之前的某个位置自动进行。我更喜欢使用基类的第一种方法,但是您也可以遵循第二种方法。

此代码显示了使用自动后处理可能如何工作:

def expose(**kw):
    "Note that using **kw you can tag the function with any parameters"
    def wrap(func):
        name = func.func_name
        assert not name.startswith('_'), "Only public methods can be exposed"

        meta = func.__meta__ = kw
        meta['exposed'] = True
        return func

    return wrap

class Exposable(object):
    "Base class to expose instance methods"
    _exposable_ = None  # Not necessary, just for pylint

    class __metaclass__(type):
        def __new__(cls, name, bases, state):
            methods = state['_exposed_'] = dict()

            # inherit bases exposed methods
            for base in bases:
                methods.update(getattr(base, '_exposed_', {}))

            for name, member in state.items():
                meta = getattr(member, '__meta__', None)
                if meta is not None:
                    print "Found", name, meta
                    methods[name] = member
            return type.__new__(cls, name, bases, state)

class Foo(Exposable):
    @expose(any='parameter will go', inside='__meta__ func attribute')
    def foo(self):
        pass

class Bar(Exposable):
    @expose(hide=True, help='the great bar function')
    def bar(self):
        pass

class Buzz(Bar):
    @expose(hello=False, msg='overriding bar function')
    def bar(self):
        pass

class Fizz(Foo):
    @expose(msg='adding a bar function')
    def bar(self):
        pass

print('-' * 20)
print("showing exposed methods")
print("Foo: %s" % Foo._exposed_)
print("Bar: %s" % Bar._exposed_)
print("Buzz: %s" % Buzz._exposed_)
print("Fizz: %s" % Fizz._exposed_)

print('-' * 20)
print('examine bar functions')
print("Bar.bar: %s" % Bar.bar.__meta__)
print("Buzz.bar: %s" % Buzz.bar.__meta__)
print("Fizz.bar: %s" % Fizz.bar.__meta__)

输出结果:

Found foo {'inside': '__meta__ func attribute', 'any': 'parameter will go', 'exposed': True}
Found bar {'hide': True, 'help': 'the great bar function', 'exposed': True}
Found bar {'msg': 'overriding bar function', 'hello': False, 'exposed': True}
Found bar {'msg': 'adding a bar function', 'exposed': True}
--------------------
showing exposed methods
Foo: {'foo': <function foo at 0x7f7da3abb398>}
Bar: {'bar': <function bar at 0x7f7da3abb140>}
Buzz: {'bar': <function bar at 0x7f7da3abb0c8>}
Fizz: {'foo': <function foo at 0x7f7da3abb398>, 'bar': <function bar at 0x7f7da3abb488>}
--------------------
examine bar functions
Bar.bar: {'hide': True, 'help': 'the great bar function', 'exposed': True}
Buzz.bar: {'msg': 'overriding bar function', 'hello': False, 'exposed': True}
Fizz.bar: {'msg': 'adding a bar function', 'exposed': True}

请注意,在此示例中:

  1. 我们可以用任意参数注释任何函数。
  2. 每个类都有自己的公开方法。
  3. 我们也可以继承暴露的方法。
  4. 方法可以被覆盖,因为暴露功能已更新。

希望这可以帮助


4

正如Ants所指出的,您不能从类内部获得对该类的引用。但是,如果您希望区分不同的类(而不是操作实际的类类型对象),则可以为每个类传递一个字符串。您还可以使用类样式装饰器将所需的任何其他参数传递给装饰器。

class Decorator(object):
    def __init__(self,decoratee_enclosing_class):
        self.decoratee_enclosing_class = decoratee_enclosing_class
    def __call__(self,original_func):
        def new_function(*args,**kwargs):
            print 'decorating function in ',self.decoratee_enclosing_class
            original_func(*args,**kwargs)
        return new_function


class Bar(object):
    @Decorator('Bar')
    def foo(self):
        print 'in foo'

class Baz(object):
    @Decorator('Baz')
    def foo(self):
        print 'in foo'

print 'before instantiating Bar()'
b = Bar()
print 'calling b.foo()'
b.foo()

印刷品:

before instantiating Bar()
calling b.foo()
decorating function in  Bar
in foo

另外,请参见Bruce Eckel关于装饰器的页面。


感谢您确认我令人沮丧的结论,认为这是不可能的。我还可以使用完全限定模块/类(“ module.Class”)的字符串,存储字符串直到所有类都完全加载,然后使用import自己检索类。这似乎是完成我的任务的一种痛苦而又不干燥的方式。
卡尔·G

您不需要使用此类装饰器的类:惯用的方法是在装饰器函数内部使用一个额外的嵌套函数级别。但是,如果您确实选择了类,最好不要在类名中使用大写字母,以使装饰本身看起来像“标准”,即@decorator('Bar')与相对@Decorator('Bar')
埃里克·卡普伦

4

什么烧瓶优雅确实是创建一个临时的缓存,它存储的方法,那么它使用别的东西(事实上,瓶将使用注册类register类的方法),以实际包装的方法。

您这次可以使用元类重用此模式,以便可以在导入时包装该方法。

def route(rule, **options):
    """A decorator that is used to define custom routes for methods in
    FlaskView subclasses. The format is exactly the same as Flask's
    `@app.route` decorator.
    """

    def decorator(f):
        # Put the rule cache on the method itself instead of globally
        if not hasattr(f, '_rule_cache') or f._rule_cache is None:
            f._rule_cache = {f.__name__: [(rule, options)]}
        elif not f.__name__ in f._rule_cache:
            f._rule_cache[f.__name__] = [(rule, options)]
        else:
            f._rule_cache[f.__name__].append((rule, options))

        return f

    return decorator

在实际的类上(您可以使用元类来做同样的事情):

@classmethod
def register(cls, app, route_base=None, subdomain=None, route_prefix=None,
             trailing_slash=None):

    for name, value in members:
        proxy = cls.make_proxy_method(name)
        route_name = cls.build_route_name(name)
        try:
            if hasattr(value, "_rule_cache") and name in value._rule_cache:
                for idx, cached_rule in enumerate(value._rule_cache[name]):
                    # wrap the method here

来源:https : //github.com/apiguy/flask-classy/blob/master/flask_classy.py


这是一个有用的模式,但这不能解决方法装饰器能够引用其所应用方法的父类的问题
Anentropic 2014年

我更新了答案,以更明确地说明这在导入时如何对访问类有用(即,使用元类+在方法上缓存装饰器参数)。
charlax 2014年

3

问题在于,当调用装饰器时,该类尚不存在。试试这个:

def loud_decorator(func):
    print("Now decorating %s" % func)
    def decorated(*args, **kwargs):
        print("Now calling %s with %s,%s" % (func, args, kwargs))
        return func(*args, **kwargs)
    return decorated

class Foo(object):
    class __metaclass__(type):
        def __new__(cls, name, bases, dict_):
            print("Creating class %s%s with attributes %s" % (name, bases, dict_))
            return type.__new__(cls, name, bases, dict_)

    @loud_decorator
    def hello(self, msg):
        print("Hello %s" % msg)

Foo().hello()

该程序将输出:

Now decorating <function hello at 0xb74d35dc>
Creating class Foo(<type 'object'>,) with attributes {'__module__': '__main__', '__metaclass__': <class '__main__.__metaclass__'>, 'hello': <function decorated at 0xb74d356c>}
Now calling <function hello at 0xb74d35dc> with (<__main__.Foo object at 0xb74ea1ac>, 'World'),{}
Hello World

如您所见,您将必须找出一种不同的方式来做自己想要的事情。


当定义一个函数时,该函数尚不存在,但可以从其内部递归调用该函数。我猜这是特定于函数的语言功能,不适用于类。
卡尔·G

DGGenuine:仅在完全创建函数后,才调用该函数,并因此访问该函数。在这种情况下,调用装饰器时该类无法完成,因为该类必须等待装饰器的结果,该结果将存储为该类的属性之一。
u0b34a0f6ae

3

这是一个简单的例子:

def mod_bar(cls):
    # returns modified class

    def decorate(fcn):
        # returns decorated function

        def new_fcn(self):
            print self.start_str
            print fcn(self)
            print self.end_str

        return new_fcn

    cls.bar = decorate(cls.bar)
    return cls

@mod_bar
class Test(object):
    def __init__(self):
        self.start_str = "starting dec"
        self.end_str = "ending dec" 

    def bar(self):
        return "bar"

输出为:

>>> import Test
>>> a = Test()
>>> a.bar()
starting dec
bar
ending dec

1

这是一个古老的问题,但遇到了金星。 http://venusian.readthedocs.org/en/latest/

它似乎具有装饰方法的能力,并且可以同时使您访问类和方法。请注意,调用setattr(ob, wrapped.__name__, decorated)不是使用金星的典型方法,并且在一定程度上会破坏目的。

无论哪种方式,下面的示例都是完整的,应该可以运行。

import sys
from functools import wraps
import venusian

def logged(wrapped):
    def callback(scanner, name, ob):
        @wraps(wrapped)
        def decorated(self, *args, **kwargs):
            print 'you called method', wrapped.__name__, 'on class', ob.__name__
            return wrapped(self, *args, **kwargs)
        print 'decorating', '%s.%s' % (ob.__name__, wrapped.__name__)
        setattr(ob, wrapped.__name__, decorated)
    venusian.attach(wrapped, callback)
    return wrapped

class Foo(object):
    @logged
    def bar(self):
        print 'bar'

scanner = venusian.Scanner()
scanner.scan(sys.modules[__name__])

if __name__ == '__main__':
    t = Foo()
    t.bar()

1

装饰器代码运行时,函数不知道它是否是定义点的方法。仅当通过类/实例标识符访问它时,它才可以知道其类/实例。为了克服此限制,您可以按描述符对象进行修饰,以将实际修饰代码延迟到访问/调用时间为止:

class decorated(object):
    def __init__(self, func, type_=None):
        self.func = func
        self.type = type_

    def __get__(self, obj, type_=None):
        func = self.func.__get__(obj, type_)
        print('accessed %s.%s' % (type_.__name__, func.__name__))
        return self.__class__(func, type_)

    def __call__(self, *args, **kwargs):
        name = '%s.%s' % (self.type.__name__, self.func.__name__)
        print('called %s with args=%s kwargs=%s' % (name, args, kwargs))
        return self.func(*args, **kwargs)

这使您可以修饰单个(静态|类)方法:

class Foo(object):
    @decorated
    def foo(self, a, b):
        pass

    @decorated
    @staticmethod
    def bar(a, b):
        pass

    @decorated
    @classmethod
    def baz(cls, a, b):
        pass

class Bar(Foo):
    pass

现在您可以使用装饰器代码进行内省...

>>> Foo.foo
accessed Foo.foo
>>> Foo.bar
accessed Foo.bar
>>> Foo.baz
accessed Foo.baz
>>> Bar.foo
accessed Bar.foo
>>> Bar.bar
accessed Bar.bar
>>> Bar.baz
accessed Bar.baz

...以及更改功能行为:

>>> Foo().foo(1, 2)
accessed Foo.foo
called Foo.foo with args=(1, 2) kwargs={}
>>> Foo.bar(1, b='bcd')
accessed Foo.bar
called Foo.bar with args=(1,) kwargs={'b': 'bcd'}
>>> Bar.baz(a='abc', b='bcd')
accessed Bar.baz
called Bar.baz with args=() kwargs={'a': 'abc', 'b': 'bcd'}

可悲的是,这种方法在功能上等同于威尔·麦卡森同样不适用的答案。根据原始问题的要求,此答案和该答案都将在方法调用时而不是在方法修饰时获得所需的类。在足够早的时间获取该类的唯一合理方法是在类定义时对所有方法进行自省(例如,通过类装饰器或元类)。</sigh>
Cecil Curry

1

正如其他答案所指出的那样,decorator是一种函数式的东西,由于尚未创建该类,因此您无法访问此方法所属的类。但是,完全可以使用装饰器“标记”函数,然后再使用元类技术来处理该方法,因为在此__new__阶段,该类已由其元类创建。

这是一个简单的示例:

我们@field用来将方法标记为一个特殊字段,并在元类中对其进行处理。

def field(fn):
    """Mark the method as an extra field"""
    fn.is_field = True
    return fn

class MetaEndpoint(type):
    def __new__(cls, name, bases, attrs):
        fields = {}
        for k, v in attrs.items():
            if inspect.isfunction(v) and getattr(k, "is_field", False):
                fields[k] = v
        for base in bases:
            if hasattr(base, "_fields"):
                fields.update(base._fields)
        attrs["_fields"] = fields

        return type.__new__(cls, name, bases, attrs)

class EndPoint(metaclass=MetaEndpoint):
    pass


# Usage

class MyEndPoint(EndPoint):
    @field
    def foo(self):
        return "bar"

e = MyEndPoint()
e._fields  # {"foo": ...}

您在此行中有错别字:if inspect.isfunction(v) and getattr(k, "is_field", False):应该getattr(v, "is_field", False)改错了。
EvilTosha

0

您将可以访问装饰器应返回的装饰方法中在其上调用该方法的对象的类。像这样:

def decorator(method):
    # do something that requires view's class
    def decorated(self, *args, **kwargs):
        print 'My class is %s' % self.__class__
        method(self, *args, **kwargs)
    return decorated

使用您的ModelA类,这是做什么的:

>>> obj = ModelA()
>>> obj.a_method()
My class is <class '__main__.ModelA'>

1
谢谢,但这恰恰是我在问题中引用的解决方案,不适用于我。我正在尝试使用装饰器实现观察者模式,并且如果在将方法添加到观察派发器中的某个时刻没有类,我将永远无法在观察派发器的正确​​上下文中调用该方法。首先调用方法获取类并不能帮助我正确地调用该方法。
卡尔·G

哇,抱歉我没读完您的全部问题。
Will McCutchen

0

我只想添加我的示例,因为它包含了从装饰方法访问类时可以想到的所有内容。它使用@tyrion建议的描述符。装饰器可以接受参数并将其传递给描述符。它可以处理类中的方法,也可以处理没有类的函数。

import datetime as dt
import functools

def dec(arg1):
    class Timed(object):
        local_arg = arg1
        def __init__(self, f):
            functools.update_wrapper(self, f)
            self.func = f

        def __set_name__(self, owner, name):
            # doing something fancy with owner and name
            print('owner type', owner.my_type())
            print('my arg', self.local_arg)

        def __call__(self, *args, **kwargs):
            start = dt.datetime.now()
            ret = self.func(*args, **kwargs)
            time = dt.datetime.now() - start
            ret["time"] = time
            return ret
        
        def __get__(self, instance, owner):
            from functools import partial
            return partial(self.__call__, instance)
    return Timed

class Test(object):
    def __init__(self):
        super(Test, self).__init__()

    @classmethod
    def my_type(cls):
        return 'owner'

    @dec(arg1='a')
    def decorated(self, *args, **kwargs):
        print(self)
        print(args)
        print(kwargs)
        return dict()

    def call_deco(self):
        self.decorated("Hello", world="World")

@dec(arg1='a function')
def another(*args, **kwargs):
    print(args)
    print(kwargs)
    return dict()

if __name__ == "__main__":
    t = Test()
    ret = t.call_deco()
    another('Ni hao', world="shi jie")
    
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.