1
在Python 3我有一個基類,從一個類派生:做一個裝飾呼叫的方法,具體的前置和後置方法的類
class Base:
# Tell the Base class to look and for pre/post
# functions the same name, and call them
def f(self):
print("f()")
def no_prepost(self): # should work, too
print("nothing")
class Derived(Base):
def pre_f(self):
print("pre_f()")
def post_f(self):
print("post_f()")
我想如果調用前置/後置的方法,他們存在,但沒有明確指出它們:
foo = Derived()
# if exists: foo.pre_f() -- too verbose, do this automatically!
foo.f()
# if exists: foo.post_f()