search

Home  >  Q&A  >  body text

There are a large number of methods with similar structures in a Python3 class. Can modifiers be used to avoid mechanical rewriting of a large number of generation methods?

class Classname(object):
    def __init__(self, p1, p2=''):
        self.p1 = p1
        self.p2 = p2

    # @Classname.decorator
    def method_one(self, p_list):
        return function_one(p_list)

    def method_one(self, p_list):
        return function_two(p_list)

    def method_one(self, p_list):
        return function_three(p_list)

A lot of them are like this method_one calls funciton_one, but now the call function_xxx needs to be changed depending on whether the user passes in p2 The method, I hope to change it to something like this
function_xxx I cannot modify the code.

    def method_two(self, p_list):
        if self.p2:
            return function_two(self.p2, p_list)
        else:
            return function_two(p_list)

I have considered using a decorator to handle it, but it seems that using a decorator can only wrap a layer around the function, but cannot intrusively change the calling method. Is there any suitable solution here?
If it is possible to modify the calling method based on self.p3 self.p4 in the future, is there any better solution?

PHPzPHPz2750 days ago889

reply all(1)I'll reply

  • 滿天的星座

    滿天的星座2017-05-24 11:37:25

    You can use metaclasses to magically modify classes, here is an example

    def function_one(*args):
        print(1, args)
    
    def function_two(*args):
        print(2, args)
    
    def make_method(func):
        # 此处填逻辑
        def _method(self, plist):
            func(plist)
            
        return _method
    
    # 元类工厂方法,传入包含 function_xxx 的模块对象
    def meta(mod):
        class Meta(type):
            def __new__(cls, name, bases, attrs):
                fnames = attrs.get('FUNCTIONS', [])
                for n in fnames:
                    func = getattr(mod, 'function_' + n)
                    attrs['method_'+n] = make_method(func)
                return super(Meta, cls).__new__(cls, name, bases, attrs)
        return Meta
    
    import sys
    
    myself = sys.modules[__name__]
    
    class Class(metaclass=meta(myself)): 
        FUNCTIONS = ['one', 'two']
    
    obj = Class()
    obj.method_one('one')
    obj.method_two('two')

    reply
    0
  • Cancelreply