Here is my code
以下是我的代码
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
In the last line I cant set the items[node.ind].v
value to node.v
as I want, and am getting the error
在最后一行中,我无法将Items[node.ind].v值设置为我想要的node.v,并收到错误
"AttributeError: can't set attribute"
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.
我不知道出了什么问题,但一定是基于语法,因为使用node.v+=1这样的语句也会显示相同的错误。我是新手,所以请建议一种方法来实现上面的更改。
更多回答
thanks for the answers but in case of comparison like items[i].v <8
how do I do it, I just tried using a temporary variable to store its value and then use this for comparison like temp = items[i].v
then temp<8
.
谢谢你的回答,但是在比较的情况下,比如items[i].v <8我怎么做,我只是尝试使用一个临时变量来存储它的值,然后使用这个来比较,比如temp = items[i].v然后temp<8。
There is no need to take special measures when accessing a namedtuple
's attributes.
在访问命名元组的属性时,不需要采取特殊措施。
there is no problem with accessing but i cant reassign it and i understand that and its same reason why i cant use it for comparison like i mentioned items[i].v]<8
. I need to know any better alternative like the one in answer using ._replace()
访问没有问题,但我不能重新分配它,我理解这一点,这也是我不能使用它进行比较的原因,就像我提到的项目[I].v]<8。我需要知道任何更好的替代方案,就像答案中使用._Replace()
Using it in an expression does not require replacing it.
在表达式中使用它不需要替换它。
yea it worked, dunno then why I was getting an error at that statement before :C
是的,它起作用了,那么不知道为什么我之前的那句话是错误的:C
For those searching this error, another thing that can trigger AtributeError: can't set attribute
is if you try to set a decorated @property
that has no setter method. Not the problem in the OP's question, but I'm putting it here to help any searching for the error message directly. (if you don't like it, go edit the question's title :)
对于搜索此错误的人来说,另一件可能触发AtributeError:Can‘t Set Attribute的事情是,如果您试图设置一个没有setter方法的修饰的@Property。这不是操作员问题中的问题,但我把它放在这里是为了帮助任何人直接搜索错误消息。(如果你不喜欢,去编辑问题的标题:)
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()
items[node.ind] = items[node.ind]._replace(v=node.v)
(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")
(注意:不要因为函数REPLACE中的前导下划线而放弃使用此解决方案。特别是对于命名元组,一些函数带有前导下划线,这并不是为了表明它们应该是“私有的”)。
namedtuple
s are immutable, just like standard tuples. You have two choices:
命名元组是不可变的,就像标准元组一样。您有两个选择:
- Use a different data structure, e.g. a class (or just a dictionary); or
- Instead of updating the structure, replace it.
The former would look like:
前者看起来像是:
class N(object):
def __init__(self, ind, set, v):
self.ind = ind
self.set = set
self.v = v
And the latter:
和后者:
item = items[node.ind]
items[node.ind] = N(item.ind, item.set, node.v)
If you want the latter, Ignacio's answer does the same thing more neatly using baked-in functionality.
如果您想要后者,Ignacio的答案使用内置功能更巧妙地实现了同样的功能。
This error can be triggered if you try to redefine a member variable that is already defined in the class you inherited.
如果尝试重新定义已在继承的类中定义的成员变量,则可能会触发此错误。
from pytorch_lightning import LightningModule
class Seq2SeqModel(LightningModule):
def __init__(self, tokenizer, bart, hparams):
super().__init__()
self.tokenizer = tokenizer
self.bart: BartForConditionalGeneration = bart
self.hparams = hparams # This triggers the error
# Changing above line to below removes the error
# self.hp = hparams
As I was new to PyTorch
and PyTorch Lightning
, I did not know the LightningModule
already had a member variable named self.hparams
. As I tried to overwrite it in my code, it caused AttributeError: can't set attribute
.
由于我是PyTorch和PyTorch Lightning的新手,我不知道LightningModule已经有了一个名为self.hparams的成员变量。当我试图在我的代码中覆盖它时,它导致了AttributeError:can't set attribute。
Just simply renaming my variable from self.hparams
to something else removed the error.
只需将变量从self.hpars重命名为其他名称即可消除错误。
Not the problem in the OP's question, but I'm putting it here to help any searching for the error message directly
这不是操作员的问题,但我把它放在这里是为了帮助任何直接搜索错误消息的人
I came across this when I incorrectly mixed dataclass and NamedTuple. Posting this here to potentially save someone from tearing out their hair.
当我错误地混淆了DataClass和NamedTuple时,我遇到了这个问题。在这里张贴这篇文章可能是为了让某人不至于扯掉头发。
@dataclasses.dataclass
class Foo(typing.NamedTuple):
bar: str
In addition to this answer that Azmisov provided, adding a setter would solve the problem:
除了Azmisov提供的这个答案之外,添加一个setter就可以解决这个问题:
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
@attr.setter
def attr(self, value):
self._attr = value
Test()
For me it was a copy/paste error.
The def method for the setter didn't exist with the correct name.
For example:
对我来说,这是一个复制/粘贴错误。Setter的def方法不存在名称正确。例如:
def __init__(self,a):
self.a = a
...
@property
def a(self):
return self._a
@a.setter
def b(self, value): # Should have used a(self,value):
self._a = value
更多回答
Small note: For this to work in Python 2 you need to make Test
a "new-style" class , i.e. explicitly derive from object
(the first line must read class Test(object):
. See also stackoverflow.com/a/45062077/1753435
小提示:要在Python 2中实现这一点,你需要将Test设置为一个“新式”类,即显式地从object派生(第一行必须读作class Test(object):)。另见stackoverflow.com/a/45062077/1753435
I'm having this issue but with a list
property and cannot figure out how to clear the list, any ideas? @Azmisov
我有这个问题,但有一个列表属性,不知道如何清除列表,有什么想法吗?@Azmisov
@Ahmad I'd need more details, you should create a new question
@Ahmad我需要更多细节,你应该创建一个新问题
Why?⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
为什么?⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
@IgnacioVazquez-Abrams more explanation is required in this answer. (linking to the docs is not sufficient.)
@IgnacioVazquez-Abrams这个答案需要更多的解释。(仅链接到文档是不够的。)
TLDR; This is a valid solution. The leading underscore in the function _replace was not meant to indicate internal usage.
TLDR;这是一个有效的解决方案。函数REPLACE中的前导下划线并不表示内部使用。
yes after 10 mins i did what you told in the latter, and now I understand why this works but what I was doing before. Thnx
是的,10分钟后,我做了你在后者告诉我的,现在我明白了为什么这是工作,但我之前在做什么。Thnx
namedtuples are immutable - hits it on the nail. thanks
命名元组是不变的-一针见血。谢谢
"Use a different data structure, e.g. a class" -- possibly a @dataclass?
“使用不同的数据结构,例如一个类”--可能是@DataClass?
yes, finally the right answer, thank you!
是的,终于有了正确的答案,谢谢!
我是一名优秀的程序员,十分优秀!