简体   繁体   English

您可以在子类中更新不变的 class 字典吗? (Python)

[英]Can you update an unchanging class dictionary in a subclass? (Python)

I want to be able to do this:我希望能够做到这一点:

class base:
    Dict = {'attribute_name': (0,4)}

class sub(base):
    Dict['attribute_name'] = (5,10)

I know I can do this by just adding a class method that's called in the init method, but that seems like a bad practice as it repeats a redundant function call for every instance.我知道我可以通过添加一个在init方法中调用的 class 方法来做到这一点,但这似乎是一种不好的做法,因为它为每个实例重复了一个冗余的 function 调用。

Dict in this case is a dictionary containing the names of attributes and the physical location of their values in a bin file.在这种情况下,字典是一个字典,其中包含属性名称及其值在 bin 文件中的物理位置。 so I wanted to make a new subclass for each type of file.所以我想为每种类型的文件创建一个新的子类。 Each type of file contains the same information just in different locations, but may have a different method of reading and writing.每种类型的文件都包含相同的信息,只是在不同的位置,但可能有不同的读写方法。 Dict contains 50+ items, so it seems redundant to copy and paste the definition of the entire dictionary into every child class to change 2 or 3 values. Dict 包含 50 多个项目,因此将整个字典的定义复制并粘贴到每个子 class 以更改 2 或 3 个值似乎是多余的。

Do I just need to refactor the whole thing?我只需要重构整个事情吗?

For more context, here's a simplified version of the code.有关更多上下文,这是代码的简化版本。

class base:
    Dict = {'attribute_name': (0,4)}

    def __init__(self,bytestring):
        self.bytearray = bytearray(bytestring)

    def read(self):
        # for each attribute, call its associated set function on its allocated bytes
        for attribute_name, (start,end) in self.Dict.items():
            setattr(self, attribute_name, getattr(self, 'set_'+attribute_name)(self.bytearray[start:end]))

    def write(self):
        # for each attribute, call its associated get function on its allocated bytes
        for attribute_name, (start,end) in self.Dict.items():
            self.bytearray[start:end] = getattr(self, 'get_'+attribute_name)(self.bytearray[start:end])

    def set_attribute_name(self,byte):
        return [int.from_bytes(byte[0:3]),int.from_bytes(byte[3:4],'little')]

    def get_attribute_name(self,byte):
        return int.to_bytes(self.attribute_name[0],3,'little') + int.to_bytes(self.attribute_name[1],1,'little')]

class sub(base):
    Dict['attribute_name'] = (5,10)

    def set_attribute_name(self,byte):
        return [int.from_bytes(byte[0:3]),int.from_bytes(byte[3:5],'little')]

    def get_attribute_name(self,byte):
        return int.to_bytes(self.attribute_name[0],3,'little') + int.to_bytes(self.attribute_name[1],2,'little')]

This allows me to easily modify how existing data is extracted, but also makes grabbing additional data as easy as adding another value to a dictionary and writing a method to handle its extraction.这使我可以轻松地修改现有数据的提取方式,同时也使获取其他数据变得像向字典中添加另一个值并编写处理其提取的方法一样简单。 It also makes copying data between file formats really easy because the attributes all have the same names, so they don't need to know how the other format stores its data.它还使得在文件格式之间复制数据变得非常容易,因为属性都具有相同的名称,因此他们不需要知道其他格式如何存储其数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM