Accessing dict keys like an attribute?

前端 未结 27 2108
南笙
南笙 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:02

    No need to write your own as setattr() and getattr() already exist.

    The advantage of class objects probably comes into play in class definition and inheritance.

提交回复
热议问题