views:

180

answers:

4

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

+2  A: 

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

Wallacoloo
Ah... I didn't even think of that. Thanks.
Reid
A: 

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

ezod
A: 

It also works for lists:

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

works just as well

inspectorG4dget
And its called 'unpacking' in python
jeffjose
+5  A: 

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

Alex Martelli