开发者

Method of Multiple Assignment in Python

开发者 https://www.devze.com 2022-12-19 21:07 出处:网络
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. Coming from ECMAScript, seeing multiple assignments such as a, b,

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. 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. 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. From what I unde开发者_JS百科rstand, 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:

>>> 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. 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


It also works for lists:

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

works just as well


Multiple assignment is implemented as a combination of tuple packing and tuple unpacking, to my knowledge, so it should have the same effect.

0

精彩评论

暂无评论...
验证码 换一张
取 消