Simpler way to create dictionary of separate variables?

前端 未结 27 1896
名媛妹妹
名媛妹妹 2020-11-22 02:42

I would like to be able to get the name of a variable as a string but I don\'t know if Python has that much introspection capabilities. Something like:

>&         


        
27条回答
  •  醉话见心
    2020-11-22 03:17

    you can use easydict

    >>> from easydict import EasyDict as edict
    >>> d = edict({'foo':3, 'bar':{'x':1, 'y':2}})
    >>> d.foo
    3
    >>> d.bar.x
    1
    >>> d = edict(foo=3)
    >>> d.foo
    3
    

    another example:

    >>> d = EasyDict(log=False)
    >>> d.debug = True
    >>> d.items()
    [('debug', True), ('log', False)]
    

提交回复
热议问题