How to overload effectively and systematically Python class methods

时光怂恿深爱的人放手 提交于 2019-12-13 12:18:36

问题


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!


回答1:


I hope this helps:


 # the base class
class B(object):
    def f1(self, val):
        pass
    def f2(self, val):
        pass

def decorator(cls, f): def g(self, *args, **kwargs): # do stuff result = f(self, *args, **kwargs) # do more stuff return g

class A(B): _overridden = ['f1', 'f2'] @classmethod def load(cls): for name in cls._overridden: setattr(name, decorator(getattr(name).im_func))

A.load()

One may have to do some changes in order to handle class and static methods.




回答2:


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.




回答3:


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.




回答4:


Assume that you have a Python (>=2.6) class with plenty (hundreds!) of methods

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.



来源:https://stackoverflow.com/questions/4718520/how-to-overload-effectively-and-systematically-python-class-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!