我上了这个课:
class DieKlass:
#My attempt using __init__
def __init__(self):
aMethod()
def hello(self):
print ("Hello !")
#Some other file
Def aMethod():
print ("me !")
我希望它作为DieKlass的实例“ ob”上的结果出现:
ob.hello()
导致:
me !
Hello !
如何在DieKlass对象的每个方法调用上调用aMethod()而不在DieKlass方法主体中显式调用aMethod?
答案 0 :(得分:1)
def aMethod(func):
def function_wrapper(x):
print("Me")
func(x)
return function_wrapper
class DieKlass:
@aMethod
def hello(self):
print("Hello")
更多信息,请点击此处:https://www.python-course.eu/python3_decorators.php