Javascript style dot notation for dictionary keys unpythonic?

前端 未结 12 1069
刺人心
刺人心 2020-12-23 10:44

I\'ve started to use constructs like these:

class DictObj(object):
    def __init__(self):
        self.d = {}
    def __getattr__(self, m):
        return s         


        
12条回答
  •  没有蜡笔的小新
    2020-12-23 10:48

    There's a symmetry between this and this answer:

    class dotdict(dict):
        __getattr__= dict.__getitem__
        __setattr__= dict.__setitem__
        __delattr__= dict.__delitem__
    

    The same interface, just implemented the other way round...

    class container(object):
        __getitem__ = object.__getattribute__
        __setitem__ = object.__setattr__
        __delitem__ = object.__delattr__
    

提交回复
热议问题