Python 将字符串转换为属性-AttributeError:can';t集属性

Python 将字符串转换为属性-AttributeError:can';t集属性,python,attributeerror,setter,converters,namedtuple,Python,Attributeerror,Setter,Converters,Namedtuple,我运行的python代码如下: def dict2struct(d): res = namedtuple("config", d.keys())(*d.values()) return res cfg = {'fieldx': 'Allan', "fieldy": 45, 'fieldt': {'head': False, 'number': 2}} res = dict2struct(cfg) print(res) res.fieldx

我运行的python代码如下:

def dict2struct(d):
    res = namedtuple("config", d.keys())(*d.values())
    return res

cfg = {'fieldx': 'Allan', "fieldy": 45, 'fieldt': {'head': False, 'number': 2}}
res = dict2struct(cfg)
print(res)
res.fieldx = "Jack"
并得到了错误信息:

config(fieldx='Allan', fieldy=45, fieldt={'head': False, 'number': 2})
Traceback (most recent call last):
  File "*****\Testings\test_x.py", line 97, in <module>
    res.fieldx = "Jack"
AttributeError: can't set attribute
config(fieldx='Allan',fieldy=45,fieldt={'head':False,'number':2})
回溯(最近一次呼叫最后一次):
文件“*****\Testings\test_x.py”,第97行,在
res.fieldx=“杰克”
AttributeError:无法设置属性
我已经阅读了下面的问题,并理解这是因为
namedtuples
是不可变的,并且属性设置器受到限制。

但是,我需要将许多字符串从配置字典的键转换为结构或类的属性。这些字符串值不是固定的,因为它们很多


有没有办法解决我的问题的
AttributeError

难道你不能编辑原始字典,然后再次调用dict2struct()函数吗?那么你是在寻找一个类似字典的对象,它允许你通过
x.key=value
设置键/值对吗?很久以前我就写了一本,从那以后我一直在用。我只是在谷歌上搜索,没有发现任何类似的东西不是作为一个庞大的收藏的一部分。我很惊讶。我想知道是否有人知道这些野生动物中有一种是靠自己的力量获得的。也许我应该出版我的:)@Steve,我的输入是一本带字符串键的字典。输出应该是一个结构/类,以便像res.fieldx、res.fieldy等那样使用。你能分享一下你的转换代码吗?啊。找到了。这就是你想要的吗这听起来像是一个错误。你想用它解决什么问题?