如何通过类继承传递参数?

时间:2017-12-11 22:10:51

标签: python class inheritance arguments slots

我有12个看起来像这样的类,唯一的区别是DefaultAsset全局对象。我有12个全局DefaultAssetOrderedDict,例如DefaultAssetsOrderedDictNewAssetsOrderedDictOldAssetsOrderedDict等。我没有让它们成为每个类的一部分,因为它们很大并且我怀疑这样做会导致多个事件的实例是静态的,如果我错了就纠正我(我有很多内存问题,从OrderDict数据行切换到类数据行解决它)

class DefaultAsset(object):
    __slots__ = list(DefaultAssetOrderedDict.keys())

    def __init__(self, **kwargs):
        for arg, default in DefaultAssetOrderedDict.items():
            setattr(self, arg, re.sub(r'[^\x00-\x7F]', '', kwargs.get(arg, default)))
            #print (str(arg) + " : "+ str(re.sub(r'[^\x00-\x7F]', '', kwargs.get(arg, default))))

    def items(self):
        for slot in self.__slots__:
            yield slot, getattr(self, slot)

    def values(self):
        for slot in self.__slots__:
            yield getattr(self, slot)

所以,我想知道如何重写上面的类,以便它是一个名为Rows的父类,以便我可以这样做:

class DefaultAssets (Row, DefaultAssetOrderedDict):
    #does the same thing but per the OrderedDict in second argument

或者也许:

DefaultAssets =  Rows(DefaultAssetOrderedDict)
NewAssets =  Rows(NewAssetOrderedDict)

1 个答案:

答案 0 :(得分:1)

如果我已正确理解您的主要目标,我觉得您可以使用元类将12 OrderedDict个实例转换为单独的类,并通过消除(或至少最小化)重复来节省内存代码和数据。这是一种方法:

from collections import OrderedDict

class MetaDefaultAsset(type):

    def __new__(cls, name, bases, namespace, **kwargs):
        clsobj = type.__new__(cls, name, bases, namespace) # create class object

        # Use "defaults" keyword argument to create __slots__ and default
        # attributes and their values.
        if 'defaults' in kwargs:
            setattr(clsobj, '__slots__', kwargs['defaults'].keys())
            for key, default_value in kwargs['defaults'].items():
                setattr(clsobj, key, default_value)

        # Define some methods to be added to class object created.
        def items(self):
            yield from ((slot, getattr(self, slot)) for slot in self.__slots__)

        def values(self):
            yield (getattr(self, slot) for slot in self.__slots__)

        # Add the above methods to the class object.
        for name, method in {'items': items, 'values': values}.items():
            setattr(clsobj, name, method)

        return clsobj


DEFAULT_ASSET_ORDERED_DICT = OrderedDict(
    [('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])

class DefaultAsset(metaclass=MetaDefaultAsset,
                   defaults=DEFAULT_ASSET_ORDERED_DICT): pass

NEW_ASSETS_ORDERED_DICT = OrderedDict(
    [('computer', 1), ('monitor', 2), ('keyboard', 3), ('mouse', 4)])

class DefaultNewAsset(metaclass=MetaDefaultAsset,
                      defaults=NEW_ASSETS_ORDERED_DICT): pass


da = DefaultAsset()
print(list(da.items()))
dna = DefaultNewAsset()
print(list(dna.items()))

输出:

[('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)]
[('computer', 1), ('monitor', 2), ('keyboard', 3), ('mouse', 4)]