35
我想通過一個字符串對象分配一個類屬性 - 但是如何?如何通過python中的__dict__指定新的類屬性?
例子:
class test(object):
pass
a = test()
test.value = 5
a.value
# -> 5
test.__dict__['value']
# -> 5
# BUT:
attr_name = 'next_value'
test.__dict__[attr_name] = 10
# -> 'dictproxy' object does not support item assignment
這似乎對我來說,從工作在對象內部:self .__ dict __。updat e(結果)其中結果是字典 – radtek 2016-03-15 05:23:13