python - 属性错误 : can't set attribute in python

标签 python attributes

这是我的代码

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,并且出现错误

"AttributeError: can't set attribute"

我不知道出了什么问题,但它必须是基于语法的东西,因为使用像 node.v+=1 这样的语句也显示相同的错误。我是 Python 新手,所以请提出一种方法来使上述更改成为可能。

最佳答案

对于那些搜索这个错误的人来说,另一个可能触发 AtributeError: can't set attribute 的事情是,如果你尝试设置一个没有 setter 方法的修饰 @property .不是 OP 问题中的问题,但我把它放在这里是为了帮助任何直接搜索错误消息。 (如果你不喜欢它,去编辑问题的标题:)

class Test:
    def __init__(self):
        self._attr = "original value"
        # This will trigger an error...
        self.attr = "new value"
    @property
    def attr(self):
        return self._attr

Test()

关于python - 属性错误 : can't set attribute in python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22562425/

相关文章:

python - 将逻辑曲线拟合到数据

Python pandas groupby 带有 cumsum 和百分比

c++ - 如何向 lambda 函数添​​加属性?

php - CSS 背景图像使用属性值

javascript - 将网站重新设计为 Ember

python - 如何在正则表达式中对 "or"匹配进行分组?

python - 用 cython 和 mingw 编译产生 gcc : error: unrecognized command line option '-mno-cygwin'

python - Twisted 服务器,从 Twitter 流 api 消费并同时提供静态内容

java - 从 session Java 访问数组

windows - 在 Windows 资源管理器中隐藏 Vim 备份 (*~)