AttributeError:无法在python中设置属性

编程入门 行业动态 更新时间:2024-10-08 08:31:19
本文介绍了AttributeError:无法在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中设置属性

本文发布于:2023-11-24 00:55:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623379.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   AttributeError   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!