简体   繁体   English

Python中的多重赋值方法

[英]Method of Multiple Assignment in Python

I'm trying to prepare for a future in computer science, so I started with ECMAScript and I am now trying to learn more about Python. 我正在努力为计算机科学的未来做准备,所以我从ECMAScript开始,现在我正在努力学习更多有关Python的知识。 Coming from ECMAScript, seeing multiple assignments such as a, b, c = 1, 2, 3 leaves me bewildered for a moment, until I realize that there are multiple assignments going on. 来自ECMAScript,看到多个任务,如a, b, c = 1, 2, 3让我感到困惑,直到我意识到有多个任务正在进行。 To make things a bit clearer, I'd really like to do (a, b, c) = (1, 2, 3) but I am not sure if this will be a measurable performance hit. 为了让事情更清楚一点,我真的很想做(a, b, c) = (1, 2, 3)但我不确定这是否会成为可衡量的性能影响。 From what I understand, tuples are essentially how multiple assignments work regardless, but there are a great many oddities in the world, so I try not to assume anything. 根据我的理解,元组本质上是多个任务无论如何都有效,但世界上有很多奇怪的东西,所以我尽量不去做任何事情。

Thanks in advance 提前致谢

It's extremely easy to check, with the dis module: 使用dis模块检查非常容易:

>>> import dis
>>> dis.dis(compile('a,b,c=1,2,3','','exec'))
  1           0 LOAD_CONST               4 ((1, 2, 3))
              3 UNPACK_SEQUENCE          3
              6 STORE_NAME               0 (a)
              9 STORE_NAME               1 (b)
             12 STORE_NAME               2 (c)
             15 LOAD_CONST               3 (None)
             18 RETURN_VALUE        
>>> dis.dis(compile('(a,b,c)=(1,2,3)','','exec'))
  1           0 LOAD_CONST               4 ((1, 2, 3))
              3 UNPACK_SEQUENCE          3
              6 STORE_NAME               0 (a)
              9 STORE_NAME               1 (b)
             12 STORE_NAME               2 (c)
             15 LOAD_CONST               3 (None)
             18 RETURN_VALUE        
>>> 

See? 看到? Those totally redundant parentheses make absolutely no difference to the bytecode that's generated and executed -- just like, say, a+b and (a+b) will generate and execute exactly the same bytecode as each other. 那些完全冗余的括号与生成和执行的字节码完全没有区别 - 就像a+b(a+b)将生成并执行完全相同的字节码。 So, if you like to add redundant parentheses, knock yourself out -- people reading your code may not like them, but ones who are just executing it will never even notice. 所以,如果你想添加多余的括号,那就把自己搞得一团糟 - 阅读代码的人可能不喜欢它们,但是那些只是执行它的人永远都不会注意到。 Only, why stop at just two pairs of redundant parentheses? 只是,为什么只停留两对冗余括号? See, 看到,

>>> dis.dis(compile('(((a,b,c)))=(((1,2,3)))','','exec'))
  1           0 LOAD_CONST               4 ((1, 2, 3))
              3 UNPACK_SEQUENCE          3
              6 STORE_NAME               0 (a)
              9 STORE_NAME               1 (b)
             12 STORE_NAME               2 (c)
             15 LOAD_CONST               3 (None)
             18 RETURN_VALUE        
>>> 

six pairs of redundant parentheses (or any number, really) still produce exactly the same code. 六对冗余括号(或任何数字,真的)仍然产生完全相同的代码。 Once you leave the obvious minimum number of redundant parentheses (none at all: they're redundant, after all;-), exactly where do you stop?-) And why there, when it's "free" to add yet one more pair... or two... or three...?-) 一旦你留下明显的最小数量的冗余括号(根本没有:它们是多余的,毕竟;-),你到底在哪里停止? - )为什么有,当它“自由”添加一对时。 ..或两个......或三个......? - )

It should not have any effect on performance. 它不应该对性能产生任何影响。 The parenthesis do not make it a tuple, the comma's do. 括号不会使它成为元组,逗号就是这样。 So (1,2,3) is exactly the same as 1,2,3 所以(1,2,3)与1,2,3完全相同

It also works for lists: 它也适用于列表:

a, b, c = [1, 2, 3]

works just as well 同样有效

根据我的知识,多重赋值是作为元组打包和元组解包的组合实现的,因此它应该具有相同的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM