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()