为什么当函数有装饰器时 getattr 不起作用
Why does getattr does not work when a function had a decorator
我有以下代码片段,它有两个方法 func_norule() 和 func_with_rule()。 func_with_rule() 方法用 @rule 装饰,而 func_norule() 没有任何装饰器。
当我使用 getattr 函数时 fn = getattr(self, 'func_norule') returns 函数 fn = getattr (self, 'func_with_rule') returns None.
使用装饰器与不使用装饰器时行为不同的原因是什么?
有解决此问题的方法吗?
class Student():
def __init__(self, name, roll_no):
self.name = name
self.roll_no = roll_no
## Decorator function to decorate all the rules function
def rule(func):
print(func.__name__)
def func_norule(self):
#This method works with getattr
print("func_norule:" + self.name)
@rule
def func_with_rule(self):
#This method returns None with getattr
print("func_with_rule:" + self.name)
def myFunc2(self):
fn = getattr(self, 'func_norule')
fn()
fn = getattr(self, 'func_with_rule')
fn()
student = Student('myName', 8)
student.myFunc2()
那是因为您没有在装饰器中绑定 self
。您可以更改您的装饰器,以便将 self
参数传递给您的装饰方法:
class Student:
# ...
def rule(func):
def _(self, *args, **kwargs):
print("Before calling", func.__name__)
result = func(self, *args, **kwargs)
print("After calling", func.__name__)
return result
return _
# ...
@rule
def func_with_rule(self):
#This method returns None with getattr
print("func_with_rule:" + self.name)
现在,当你这样做时
student = Student('myName', 8)
student.myFunc2()
产出
func_norule:myName
Before calling func_with_rule
func_with_rule:myName
After calling func_with_rule
我有以下代码片段,它有两个方法 func_norule() 和 func_with_rule()。 func_with_rule() 方法用 @rule 装饰,而 func_norule() 没有任何装饰器。
当我使用 getattr 函数时 fn = getattr(self, 'func_norule') returns 函数 fn = getattr (self, 'func_with_rule') returns None.
使用装饰器与不使用装饰器时行为不同的原因是什么? 有解决此问题的方法吗?
class Student():
def __init__(self, name, roll_no):
self.name = name
self.roll_no = roll_no
## Decorator function to decorate all the rules function
def rule(func):
print(func.__name__)
def func_norule(self):
#This method works with getattr
print("func_norule:" + self.name)
@rule
def func_with_rule(self):
#This method returns None with getattr
print("func_with_rule:" + self.name)
def myFunc2(self):
fn = getattr(self, 'func_norule')
fn()
fn = getattr(self, 'func_with_rule')
fn()
student = Student('myName', 8)
student.myFunc2()
那是因为您没有在装饰器中绑定 self
。您可以更改您的装饰器,以便将 self
参数传递给您的装饰方法:
class Student:
# ...
def rule(func):
def _(self, *args, **kwargs):
print("Before calling", func.__name__)
result = func(self, *args, **kwargs)
print("After calling", func.__name__)
return result
return _
# ...
@rule
def func_with_rule(self):
#This method returns None with getattr
print("func_with_rule:" + self.name)
现在,当你这样做时
student = Student('myName', 8)
student.myFunc2()
产出
func_norule:myName
Before calling func_with_rule
func_with_rule:myName
After calling func_with_rule