Python 命名元组列表,替换属性

发布于 2024-12-29 00:22:22 字数 963 浏览 5 评论 0原文

这是一些简化的代码,我不明白为什么它不起作用。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡水深流 2025-01-05 00:22:22

_replace 并没有像你想象的那样做。来自 文档

somenamedtuple._replace(kwargs)

返回命名元组的新实例,用新值替换指定字段:

>>> p = Point(x=11, y=22)  
>>> p._replace(x=33)  
Point(x=33, y=22)

您正在调用_replace,但从未存储新的节点它返回。它返回一个新对象而不是“就地”更改对象的原因是,namedtuples 根据定义是不可变的,即它们在创建后无法更改。

请注意,您的 for 循环创建了对同一 Node 的四个引用的列表。在这种情况下,这并不是真正的问题,因为您创建的对象都是相同的,并且 namedtuple 是不可变的,但通常要注意这一点。

总结来说:

from collections import namedtuple

MyStruct = namedtuple('MyStruct', 'ThreadInstance ThreadName Mnemonic IpAddr IpGW Status Mode')

NodeDb = []
Node = MyStruct(None, '', '', '', '',  -1, 0)
for id in range(4):
    NodeDb.append(Node)

NodeDb[2] = NodeDb[2]._replace(ThreadName='T2',
                               Mnemonic='ABCD',
                               IpAddr='192.0.1.2',
                               IpGW='192.0.1.3',
                               Status=0,
                               Mode=2)

print(NodeDb)

_replace does not do what you think it does. From the docs:

somenamedtuple._replace(kwargs)

Return a new instance of the named tuple replacing specified fields with new values:

>>> p = Point(x=11, y=22)  
>>> p._replace(x=33)  
Point(x=33, y=22)

You're calling _replace but you never store the new Node it returns. The reason it returns a new object instead of altering the object 'in-place' is that namedtuples 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 same Node. In this case this isn't really a problem since the objects you are creating are all the same and namedtuple is immutable, but in general be aware of this.

So in summary:

from collections import namedtuple

MyStruct = namedtuple('MyStruct', 'ThreadInstance ThreadName Mnemonic IpAddr IpGW Status Mode')

NodeDb = []
Node = MyStruct(None, '', '', '', '',  -1, 0)
for id in range(4):
    NodeDb.append(Node)

NodeDb[2] = NodeDb[2]._replace(ThreadName='T2',
                               Mnemonic='ABCD',
                               IpAddr='192.0.1.2',
                               IpGW='192.0.1.3',
                               Status=0,
                               Mode=2)

print(NodeDb)
万劫不复 2025-01-05 00:22:22

请记住,该列表中的每个项目的工作方式都类似于 C 语言中的指针。 Python 变量始终通过引用传递和使用。也就是说,就其编写方式而言,这是正确的:

assert NodeDb[0] is NodeDb[1] is NodeDb[2] is NodeDb[3] # True

我认为您想要一个单独对象的列表,在这种情况下,您应该将 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:

assert NodeDb[0] is NodeDb[1] is NodeDb[2] is NodeDb[3] # True

I think you want to have a list of separate objects, in which case, you should move Node = MyStruct(...) inside your for loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文