AttributeError:无法在python中设置属性 [英] AttributeError: can't set attribute in python
问题描述
这是我的代码
N = namedtuple("N", ['ind', 'set', 'v'])
def solve()
items=[]
stack=[]
R = set(range(0,8))
for i in range(0,8):
items.append(N(i,R,8))
stack.append(N(0,R-set(range(0,1)),i))
while(len(stack)>0):
node = stack.pop()
print node
print items[node.ind]
items[node.ind].v = node.v
在最后一行中,我无法根据需要将items[node.ind].v
值设置为node.v
,并且出现错误
In the last line I cant set the items[node.ind].v
value to node.v
as I want, and am getting the error
"AttributeError: can't set attribute"
我不知道出了什么问题,但这一定是基于语法的,因为使用node.v+=1
之类的语句也显示了相同的错误.我是Python的新手,所以请提出一种使上述更改成为可能的方法.
I don't know what's wrong but it must be something based on syntax as using statements like node.v+=1
is also showing same error. I'm new to Python, so please suggest a way to make the above change possible.
推荐答案
items[node.ind] = items[node.ind]._replace(v=node.v)
(注意:由于函数_replace中的前导下划线,因此不建议使用此解决方案.特别是对于namedtuple,某些函数的前导下划线不是为了表示它们应为私有")
(Note: Don't be discouraged to use this solution because of the leading underscore in the function _replace. Specifically for namedtuple some functions have leading underscore which is not for indicating they are meant to be "private")
这篇关于AttributeError:无法在python中设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!