我使用EasyDict
和要指派一个dict
类型的钥匙int
吧
from easydict import EasyDict as edict
cfg = edict()
cfg.tt = {'0': 'aeroplane'} # this is ok
cfg.tt = {0: 'aeroplane'} # this raises error, but this is what I want to use!
我想分配dict
我想要的东西怎么办,谢谢
这是因为要将的EasyDict
任何值转换dict
为EasyDict
。并根据键创建属性。 int
值不能是属性,因此这是行不通的。您可以安装PermissiveDict
,它的作用与上一次相同,EasyDict
但不会尝试将值转换为它自己的类型。
pip install permissive-dict
你的例子:
from permissive_dict import PermissiveDict
cfg = PermissiveDict()
cfg.tt = {'0': 'aeroplane'} # this is ok
cfg.tt = {0: 'aeroplane'} # this does not raise errors, but this is what I want to use!
cfg.tt[0]) == cfg.TT[0] == cfg.tT[0] == cfg.Tt[0] == 'aeroplane'