如何有效且系统地重载Python类方法
假设您有一个包含大量(数百个!)方法的 Python (>=2.6) 类。现在有人想要子类化它,但意识到大多数基类方法只需要一些简单的“调整”。此外,只有少数几种不同的方法可以调整这些方法。有些涉及输入转换,有些涉及输出转换,有些两者都涉及。
更具体地说,我正在寻找一个(简单的)继承解决方案,我只需提供基类和方法列表(列表)来应用特定的转换,而不需要将此样板代码添加到每个重载方法中。
谢谢, 吃
跟进。
根据 gabo10yf 的回答,我想出了一个解决方案:
class B(object):
def f1(self, val):
print '1: ', val
def f2(self, val):
print '2: ', val
def decorator(f):
def g(self, *args, **kwargs):
print 'entering'
result= f(self, *args, **kwargs)
return g
class A(B):
pass
_overridden= ['f1', 'f2']
def override(cls):
for name in _overridden:
setattr(cls, name, decorator(getattr(cls, name).im_func))
override(A)
if __name__== '__main__':
b= B()
b.f1(1)
b.f2(2)
a= A()
a.f1(1)
a.f2(2)
它似乎确实有效。不过感觉太简单了,想必其中还隐藏着一些隐晦的问题吧?无论如何,谢谢你们让我弄清楚了这一点!
Assume that you have a Python (>=2.6) class with plenty (hundreds!) of methods. Now someone wants to subclass that but realized that most of the base class methods needs only some simple 'tuning'. Also there are only handful of different ways to tune those methods. Some involving input transformations, some output transformations, some both.
To be more specific I'm looking for a (simple) solution for the inheritance where I just could provide the base class and a list (of lists) of methods to apply the specific transformations, without adding this boilerplate code to each overloaded method.
Thanks,
eat
Follow up.
Based on gabo10yf answer, I came up a solution as:
class B(object):
def f1(self, val):
print '1: ', val
def f2(self, val):
print '2: ', val
def decorator(f):
def g(self, *args, **kwargs):
print 'entering'
result= f(self, *args, **kwargs)
return g
class A(B):
pass
_overridden= ['f1', 'f2']
def override(cls):
for name in _overridden:
setattr(cls, name, decorator(getattr(cls, name).im_func))
override(A)
if __name__== '__main__':
b= B()
b.f1(1)
b.f2(2)
a= A()
a.f1(1)
a.f2(2)
It really seems to work. However it feels almost too simple, it surely must still contain some murky issues? Anyway thanks to you all letting me figure out this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我希望这有帮助:
为了处理类和静态方法,可能需要进行一些更改。
I hope this helps:
One may have to do some changes in order to handle class and static methods.
我建议使用某种形式的面向方面编程。使用 Logilab 库,可以轻松地对类的所有方法进行包装。
I suggest to use some form of Aspected-Oriented Programming. With the Logilab library, it is easy to put wrappers around all methods of a class.
在继承之前我会考虑装饰器。
我还会考虑重构该类 - 数百种方法向我暗示了“上帝反模式”。
I would consider decorators before I'd go for inheritance.
I'd also consider refactoring that class - hundreds of methods suggests "god anti-pattern" to me.
我将重构该类,因为该类的设计已被破坏。
此外,您可以通过猴子修补所述类的实例轻松实现给定结果。
I will then refactor the class, because the class design is broken.
Also, you can easily achieve the given result by monkey patching an instance of said class.