Python 命名元组列表,替换属性
这是一些简化的代码,我不明白为什么它不起作用。
from collections import namedtuple
MyStruct = namedtuple('MyStruct', 'ThreadInstance ThreadName Mnemonic IpAddr IpGW Status Mode')
Node = MyStruct(None, '', '', '', '', -1, 0)
NodeDb = []
for id in range(4):
NodeDb.append(Node)
NodeDb[2]._replace(ThreadName='T2')
NodeDb[2]._replace(Mnemonic='ABCD')
NodeDb[2]._replace(IpAddr='192.0.1.2')
NodeDb[2]._replace(IpGW='192.0.1.3')
NodeDb[2]._replace(Status=0)
NodeDb[2]._replace(Mode=2)
print(NodeDb)
这是输出
'>>>
[MyStruct(ThreadInstance=None, ThreadName='', Mnemonic='', IpAddr='', IpGW='', Status=-1, Mode=0),
MyStruct(ThreadInstance=None, ThreadName='', Mnemonic='', IpAddr='', IpGW='', Status=-1, Mode=0),
MyStruct(ThreadInstance=None, ThreadName='', Mnemonic='', IpAddr='', IpGW='', Status=-1, Mode=0),
MyStruct(ThreadInstance=None, ThreadName='', Mnemonic='', IpAddr='', IpGW='', Status=-1, Mode=0)]'
Here is some simplified code that I don't understand why it does not work.
from collections import namedtuple
MyStruct = namedtuple('MyStruct', 'ThreadInstance ThreadName Mnemonic IpAddr IpGW Status Mode')
Node = MyStruct(None, '', '', '', '', -1, 0)
NodeDb = []
for id in range(4):
NodeDb.append(Node)
NodeDb[2]._replace(ThreadName='T2')
NodeDb[2]._replace(Mnemonic='ABCD')
NodeDb[2]._replace(IpAddr='192.0.1.2')
NodeDb[2]._replace(IpGW='192.0.1.3')
NodeDb[2]._replace(Status=0)
NodeDb[2]._replace(Mode=2)
print(NodeDb)
Here is the output
'>>>
[MyStruct(ThreadInstance=None, ThreadName='', Mnemonic='', IpAddr='', IpGW='', Status=-1, Mode=0),
MyStruct(ThreadInstance=None, ThreadName='', Mnemonic='', IpAddr='', IpGW='', Status=-1, Mode=0),
MyStruct(ThreadInstance=None, ThreadName='', Mnemonic='', IpAddr='', IpGW='', Status=-1, Mode=0),
MyStruct(ThreadInstance=None, ThreadName='', Mnemonic='', IpAddr='', IpGW='', Status=-1, Mode=0)]'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
_replace
并没有像你想象的那样做。来自 文档:您正在调用
_replace
,但从未存储新的节点它返回。它返回一个新对象而不是“就地”更改对象的原因是,
namedtuples
根据定义是不可变的,即它们在创建后无法更改。请注意,您的
for
循环创建了对同一Node
的四个引用的列表。在这种情况下,这并不是真正的问题,因为您创建的对象都是相同的,并且namedtuple
是不可变的,但通常要注意这一点。总结来说:
_replace
does not do what you think it does. From the docs:You're calling
_replace
but you never store the newNode
it returns. The reason it returns a new object instead of altering the object 'in-place' is thatnamedtuples
are by definition immutable, i.e. they can't be altered after you created them.Be aware your
for
loop creates a list of four references to the sameNode
. In this case this isn't really a problem since the objects you are creating are all the same andnamedtuple
is immutable, but in general be aware of this.So in summary:
请记住,该列表中的每个项目的工作方式都类似于 C 语言中的指针。 Python 变量始终通过引用传递和使用。也就是说,就其编写方式而言,这是正确的:
我认为您想要一个单独对象的列表,在这种情况下,您应该将
Node = MyStruct(...)
移动到 for 循环内。Keep in mind each item in that list works like a pointer in C parlance. Python variables are always passed and used by reference. That is, the way it's written, this is true:
I think you want to have a list of separate objects, in which case, you should move
Node = MyStruct(...)
inside your for loop.