使用dotdict创建数组

2024-04-29 05:44:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在我的应用程序中使用dotdict:

class dotdict(dict):
    """dot.notation access to dictionary attributes"""
    __getattr__ = dict.get
    __setattr__ = dict.__setitem__
    __delattr__ = dict.__delitem__

这项工作:

test = dotdict({})
test.foo = "bar"

但是,这不起作用,因为我得到'NoneType' object does not support item assignment

test = dotdict({})
test.foo[0] = "bar"

我想知道是否有办法更改setattr,以自动创建一个特定大小的数组,这样我就不会出错?多谢各位


Tags: totest应用程序dictionaryaccessfoobardot