【发布时间】:2015-11-10 13:05:18
【问题描述】:
在 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()")
如果存在,我想调用 pre/post 方法,但没有一直明确说明它们:
foo = Derived()
# if exists: foo.pre_f() -- too verbose, do this automatically!
foo.f()
# if exists: foo.post_f()
【问题讨论】:
标签: python decorator introspection python-decorators