我偶然发现了一个棘手的python问题。给定(更新):
class A(object):
def run(self):
# This makes possible to determine if 'run' was overridden
if self.run.im_func != A.run.im_func:
print('Running in {0}'.format(self.__class__.__name__))
class B(A):
def run(self):
super(B, self).run()
class C(A):
pass
b = B()
c = C()
b.run()
>>> Running in B
c.run()
>>> # nothing :)
你如何设计@runoverriden装饰器,它将在A.run()中完成条件语句的工作?
更新 此代码的目的是A.run()应该记录run()调用,只有在它被覆盖时才会记录。
谢谢!
答案 0 :(得分:3)
如果我理解你想要的东西:
import functools
def runoverridden(f):
@functools.wraps(f)
def wrapper(self, *args, **kw):
if getattr(self, f.__name__).im_func != wrapper:
return f(self, *args, **kw)
return wrapper
class A(object):
@runoverridden
def run(self):
print('Running in A')
class B(A):
def run(self):
super(B, self).run()
print('Running in B')
class C(A):
pass
b = B()
c = C()
b.run()
c.run()
答案 1 :(得分:0)
听起来你想要ABCs。具体来说,是abstractmethod装饰者。