Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 827
Reid Avatar asked Feb 06 '10 02:02

Reid


3 Answers

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

like image 134
Alex Martelli Avatar answered Oct 19 '22 12:10

Alex Martelli


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

like image 11
Ponkadoodle Avatar answered Oct 19 '22 13:10

Ponkadoodle


It also works for lists:

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

works just as well

like image 2
inspectorG4dget Avatar answered Oct 19 '22 12:10

inspectorG4dget