【发布时间】:2019-11-20 12:52:55
【问题描述】:
我正在尝试创建一个装饰器,它将验证参数是否存在并检索被装饰的方法的名称。
我能够在函数的第二层访问方法的名称,但不能访问第一层。
比如我有这个装饰器
def p_decorate(name, *a, **k):
print(name + ' is at object: ')
print a #I would like to get the method object here
def fn(*a, **k)
print a #object prints here instead
return fn
return p_decorate
我想装饰这门课
class Person(object):
@p_decorate('John')
def get_fullnameobject(self):
return self.name
我希望它打印出来:
John is at object: (<function get_fullnameobject at 0x000000003745A588>,)
(<function get_fullnameobject at 0x000000003745A588>,)
但输出是:
John is at object: ()
(<function get_fullnameobject at 0x000000003745A588>,)
【问题讨论】:
标签: python methods decorator python-decorators