Accessing dict keys like an attribute?

前端 未结 27 2739
南笙
南笙 2020-11-22 04:22

I find it more convenient to access dict keys as obj.foo instead of obj[\'foo\'], so I wrote this snippet:

class AttributeDict(dict         


        
27条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 05:07

    What if you wanted a key which was a method, such as __eq__ or __getattr__?

    And you wouldn't be able to have an entry that didn't start with a letter, so using 0343853 as a key is out.

    And what if you didn't want to use a string?

提交回复
热议问题