Python高阶序列赋值?(Python higher

编程入门 行业动态 更新时间:2024-10-25 18:34:01
Python高阶序列赋值?(Python higher-order sequence assignment?)

有没有办法在python中将名称组合在一起,以便重复分配给它们?

虽然我们可以这样做:

a,b,c = (1,2,3)

我希望能够做到这样的事情:

names = a,b,c *names = (3,2,1) # this syntax doesn't work a,b,c == (3,2,1) #=> True

是否有内置语法? 如果没有,我认为可能有一个对象重载其赋值运算符。 在这种情况下,是否存在现有实现,并且此概念是否会出现任何意外故障模式?

关键是不要将名称用作数据,而是能够将实际名称用作每个引用其各自项目的变量,并且能够将列表用作列表,并避免代码如下:

a = 1 b = 2 c = 3 sequence = (a,b,c)

Is there a way to group names together in python, to repeatedly assign to them en masse?

While we can do:

a,b,c = (1,2,3)

I would like to be able to do something like:

names = a,b,c *names = (3,2,1) # this syntax doesn't work a,b,c == (3,2,1) #=> True

Is there a built-in syntax for this? If not, I assume it would be possible with an object that overloads its assignment operator. In that case, is there an existing implementation, and would this concept have any unexpected failure modes?

The point is not to use the names as data, but rather to be able to use the actual names as variables that each refer to their own individual item, and to be able to use the list as a list, and to avoid code like:

a = 1 b = 2 c = 3 sequence = (a,b,c)

最满意答案

这个?

>>> from collections import namedtuple >>> names = namedtuple( 'names', ['a','b','c'] ) >>> thing= names(3,2,1) >>> thing.a 3 >>> thing.b 2 >>> thing.c 1

This?

>>> from collections import namedtuple >>> names = namedtuple( 'names', ['a','b','c'] ) >>> thing= names(3,2,1) >>> thing.a 3 >>> thing.b 2 >>> thing.c 1

更多推荐

本文发布于:2023-08-01 23:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1368011.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:赋值   序列   高阶   Python   higher

发布评论

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

>www.elefans.com

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