简体   繁体   English

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

[英]Can a decorator of an instance method access the class?

I have something roughly like the following.我有一些大致如下。 Basically I need to access the class of an instance method from a decorator used upon the instance method in its definition.基本上,我需要从实例方法的定义中使用的装饰器访问实例方法的类。

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

The code as-is gives:代码原样给出:

AttributeError: 'function' object has no attribute 'im_class' AttributeError: 'function' 对象没有属性 'im_class'

I found similar question/answers - Python decorator makes function forget that it belongs to a class and Get class in Python decorator - but these rely upon a workaround that grabs the instance at run-time by snatching the first parameter.我发现了类似的问题/答案 - Python 装饰器使函数忘记它属于一个类,在 Python 装饰器中获取类- 但这些依赖于通过抢夺第一个参数在运行时获取实例的解决方法。 In my case, I will be calling the method based upon the information gleaned from its class, so I can't wait for a call to come in.就我而言,我将根据从其类中收集的信息来调用该方法,因此我迫不及待地等待来电。

If you are using Python 2.6 or later you could use a class decorator, perhaps something like this (warning: untested code).如果您使用的是 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

The method decorator marks the method as one that is of interest by adding a "use_class" attribute - functions and methods are also objects, so you can attach additional metadata to them.方法装饰器通过添加“use_class”属性将方法标记为感兴趣的方法 - 函数和方法也是对象,因此您可以向它们附加额外的元数据。

After the class has been created the class decorator then goes through all the methods and does whatever is needed on the methods that have been marked.创建类后,类装饰器将遍历所有方法并对已标记的方法执行所需的任何操作。

If you want all the methods to be affected then you could leave out the method decorator and just use the class decorator.如果您希望所有方法都受到影响,那么您可以省略方法装饰器而只使用类装饰器。

Since python 3.6 you can use object.__set_name__ to accomplish this in a very simple way.从 python 3.6 开始,您可以使用object.__set_name__以非常简单的方式完成此操作。 The doc states that __set_name__ is "called at the time the owning class owner is created".该文档指出__set_name__是“在创建拥有类所有者时调用的”。 Here is an example:下面是一个例子:

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)

Notice that it gets called at class creation time:请注意,它在类创建时被调用:

>>> 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

If you want to know more about how classes are created and in particular exactly when __set_name__ is called, you can refer to the documentation on "Creating the class object" .如果您想了解有关如何创建类的更多信息,尤其是在__set_name__确切时间,您可以参考有关“创建类对象”文档

As others have pointed out, the class hasn't been created at the time the decorator is called.正如其他人指出的那样,在调用装饰器时尚未创建该类。 However , it's possible to annotate the function object with the decorator parameters, then re-decorate the function in the metaclass's __new__ method.但是,可以使用装饰器参数注释函数对象,然后在元类的__new__方法中重新装饰函数。 You'll need to access the function's __dict__ attribute directly, as at least for me, func.foo = 1 resulted in an AttributeError.您需要直接访问函数的__dict__属性,至少对我而言, func.foo = 1导致 AttributeError 。

As Mark suggests:正如马克所建议的:

  1. Any decorator is called BEFORE class is built, so is unknown to the decorator.任何装饰器都在构建类之前被调用,因此装饰器不知道。
  2. We can tag these methods and make any necessary post-process later.我们可以标记这些方法并在以后进行任何必要的后期处理。
  3. We have two options for post-processing: automatically at the end of the class definition or somewhere before the application will run.我们有两种后处理选项:自动在类定义的末尾或在应用程序运行之前的某个地方。 I prefer the 1st option using a base class, but you can follow the 2nd approach as well.我更喜欢使用基类的第一个选项,但您也可以遵循第二个方法。

This code shows how this may works using automatic post-processing:此代码显示了使用自动后处理如何工作:

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__)

The output yields:输出产生:

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}

Note that in this example:请注意,在此示例中:

  1. We can annotate any function with any arbitrary parameters.我们可以用任意参数注释任何函数。
  2. Each class has its own exposed methods.每个类都有自己公开的方法。
  3. We can inherit exposed methods as well.我们也可以继承暴露的方法。
  4. methods can be overriding as exposing feature is updated.随着公开功能的更新,方法可以被覆盖。

Hope this helps希望这可以帮助

As Ants indicated, you can't get a reference to the class from within the class.正如 Ants 所指出的,您无法从类中获取对类的引用。 However, if you're interested in distinguishing between different classes ( not manipulating the actual class type object), you can pass a string for each class.但是,如果您有兴趣区分不同的类(而不是操作实际的类类型对象),则可以为每个类传递一个字符串。 You can also pass whatever other parameters you like to the decorator using class-style decorators.您还可以使用类样式装饰器将您喜欢的任何其他参数传递给装饰器。

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()

Prints:印刷:

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

Also, see Bruce Eckel's page on decorators.另外, 请参阅 Bruce Eckel 关于装饰器的页面。

What flask-classy does is create a temporary cache that it stores on the method, then it uses something else (the fact that Flask will register the classes using a register class method) to actually wraps the method. flask-classy所做的是创建一个临时缓存,它存储在方法上,然后它使用其他东西(Flask 将使用register类方法register类的事实)来实际包装方法。

You can reuse this pattern, this time using a metaclass so that you can wrap the method at import time.您可以重用此模式,这次使用元类,以便您可以在导入时包装方法。

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

On the actual class (you could do the same using a metaclass):在实际的类上(你可以使用元类做同样的事情):

@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

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

Here's a simple example:这是一个简单的例子:

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"

The output is:输出是:

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

The problem is that when the decorator is called the class doesn't exist yet.问题是当装饰器被调用时,这个类还不存在。 Try this:尝试这个:

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()

This program will output:该程序将输出:

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

As you see, you are going to have to figure out a different way to do what you want.如您所见,您将不得不想出一种不同的方式来做您想做的事。

As other answers have pointed out, decorator is an function-ish thing, you can not access the class which this method belongs to since the class has not been created yet.正如其他答案所指出的那样,装饰器是一个函数式的东西,由于尚未创建该类,因此您无法访问该方法所属的类。 However, it's totally ok to use a decorator to "mark" the function and then use metaclass techniques to deal with the method later, because at the __new__ stage, the class has been created by its metaclass.但是,使用装饰器来“标记”函数,然后使用元类技术来处理该方法是完全可以的,因为在__new__阶段,该类已经由其元类创建。

Here is a simple example:这是一个简单的例子:

We use @field to mark the method as a special field and deal with it in metaclass.我们使用@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": ...}

This is an old question but came across venusian.这是一个古老的问题,但遇到了金星。 http://venusian.readthedocs.org/en/latest/ http://venusian.readthedocs.org/en/latest/

It seems to have the ability to decorate methods and give you access to both the class and the method while doing so.它似乎有能力装饰方法,并在这样做时让您访问类和方法。 Note tht calling setattr(ob, wrapped.__name__, decorated) is not the typical way of using venusian and somewhat defeats the purpose.请注意,调用setattr(ob, wrapped.__name__, decorated)不是使用 venusian 的典型方法,并且在某种程度上违背了目的。

Either way... the example below is complete and should run.无论哪种方式......下面的示例是完整的并且应该运行。

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()

Function doesn't know whether it's a method at definition point, when the decorator code runs.当装饰器代码运行时,函数不知道它是否是定义点的方法。 Only when it's accessed via class/instance identifier it may know its class/instance.只有当它通过类/实例标识符访问时,它才可能知道它的类/实例。 To overcome this limitation, you may decorate by descriptor object to delay actual decorating code until access/call time:为了克服这个限制,您可以通过描述符对象进行装饰以延迟实际的装饰代码,直到访问/调用时间:

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)

This allows you to decorate individual (static|class) methods:这允许您装饰单个(静态|类)方法:

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

Now you can use decorator code for introspection...现在您可以使用装饰器代码进行自省...

>>> 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

...and for changing function behavior: ...以及改变函数行为:

>>> 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'}

You will have access to the class of the object on which the method is being called in the decorated method that your decorator should return.您将可以访问在装饰器应返回的装饰方法中调用该方法的对象的类。 Like so:像这样:

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

Using your ModelA class, here is what this does:使用您的 ModelA 类,这是这样做的:

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

I just want to add my example since it has all the things I could think of for accessing the class from the decorated method.我只想添加我的示例,因为它具有我可以想到的从装饰​​方法访问类的所有内容。 It uses a descriptor as @tyrion suggests.它使用@tyrion 建议的描述符。 The decorator can take arguments and passes them to the descriptor.装饰器可以接受参数并将它们传递给描述符。 It can deal with both a method in a class or a function without a class.它既可以处理类中的方法,也可以处理没有类的函数。

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")
    

@asterio gonzalez @asterio 冈萨雷斯

I prefer your method, however it has to be changed a little for Python 3 to comply with the new metaclass processing (also, some print statements were missing parentheses):我更喜欢你的方法,但是必须对 Python 3 进行一些更改以符合新的元类处理(此外,一些打印语句缺少括号):

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Aug  9 15:27:30 2021

@author: yves
"""

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

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

    return wrap

class ExposableMetaclass(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 Exposable(metaclass=ExposableMetaclass):
    "Base class to expose instance methods"
    _exposable_ = None  # Not necessary, just for pylint

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_)

Met my needs!满足我的需求!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM