扩展类的__init__以接受更多关键字参数

问题描述 投票:2回答:1

我想扩展一组更大的类来接受一个额外的关键字参数。这组类都共享一个我无法改变的通用接口。

我试过这样的:

class Base:
    def __init__(self, a=0, b=1, c=2):
        self.a = a
        self.b = b
        self.c = c



def extended_base ( Kls ):

    additional_fields = [ 'a', 'b' ]

    def my_init( self, *args, **kwargs ):
        for field in additional_fields:
            try:
                setattr(self, '{}2'.format(field), kwargs.pop(field))
            except KeyError:
                setattr(self, '{}2'.format(field), None)

        # Does not work as I expect
        # super().__init__(*args, **kwargs)

        # Does not work as expected 
        super(Kls, self).__init__(*args, **kwargs)


    # I would like to define a function that returns a function that does
    # what the following functions do, automatically but for every value
    # in `additional_fields` 
    def get_a2( self ):
        return self.a2 or self.a
    def get_b2( self ):
        return self.b2 or self.b

    return type( 'My{}'.format(Kls.__name__), (Kls, ), {
        '__init__' : my_init,
        'get_a2' : get_a2,
        'get_b2' : get_b2,

    })


test = extended_base(Base)(a=3, a2=9)
test.a
test.a2
test.get_a2()

test.get_b2()

如您所见,我只想扩展基类的一些属性,我希望能够通过简单地指定应该在函数的additional_fields中扩展的属性来实现。

我有两个问题:我不知道如何调用父亲__init__方法,上面显示的两种方式都给出错误第二种是,我不知道如何定义一个匿名(?)函数,例如,这样做如果我将c添加到c列表中,则属性additional_fields也是如此。

python python-3.x
1个回答
0
投票

一种方法是使用元类:

class LibraryBase:  
    # imagine it is placed it third-party library

    def __init__(self, a=0, b=1, c=2):
        self.a = a
        self.b = b
        self.c = c

class Meta(type):
    def __new__(cls, name, bases, attrs):
        for name in attrs['additional_fields']:
            name2 = '{}2'.format(name)
            get_name2 = 'get_{}'.format(name2)

            def getter_name2(name2, name):
                def _get_name2(self):
                    return getattr(self, name2, None) or getattr(self, name)
                return _get_name2

            attrs[get_name2] = getter_name2(name2, name)
        return type.__new__(cls, name, bases, attrs)


class Base(LibraryBase, metaclass=Meta):  
    # we still can inherit from library class with our metaclass

    additional_fields = []

    def __init__(self, a=0, b=1, c=2, **kwargs):
        self.a = a
        self.b = b
        self.c = c
        for key, value in kwargs.items():
            setattr(self, key, value)
        for name in self.additional_fields:
            name2 = '{}2'.format(name)
            setattr(self, name2, kwargs.get(name2))


class ExtendBase(Base):
    additional_fields = ['a', 'b']

class ExtendBase2(Base):
    additional_fields = ['a', 'b', 'c']


test = ExtendBase(a=3, a2=9)

print(test.a)  # 3
print(test.a2)  # 9
print(test.get_a2())  # 9

print(test.b)  # 1
print(test.b2)  # None
print(test.get_b2())  # 1

print(test.c)  # 2
print(test.c2)  # AttributeError
print(test.get_c2())  # AttributeError
© www.soinside.com 2019 - 2024. All rights reserved.