相关疑难解决方法(0)

如何"完美"覆盖一个字典?

我怎样才能使"完美"成为dict的子类?最终目标是有一个简单的字典,其中键是小写的.

似乎应该有一些微小的原语我可以覆盖以使这项工作,但根据我的所有研究和尝试似乎情况并非如此:

这是我的第一次尝试,get()不起作用,毫无疑问还有许多其他小问题:

class arbitrary_dict(dict):
    """A dictionary that applies an arbitrary key-altering function
       before accessing the keys."""

    def __keytransform__(self, key):
        return key

    # Overridden methods. List from 
    # https://stackoverflow.com/questions/2390827/how-to-properly-subclass-dict

    def __init__(self, *args, **kwargs):
        self.update(*args, **kwargs)

    # Note: I'm using dict directly, since super(dict, self) doesn't work. …
Run Code Online (Sandbox Code Playgroud)

python inheritance dictionary get set

204
推荐指数
5
解决办法
10万
查看次数

标签 统计

dictionary ×1

get ×1

inheritance ×1

python ×1

set ×1