A new to Python question

M.E.Farmer mefjr75 at hotmail.com
Sat May 14 17:59:04 EDT 2005


>It actually is the same, and I don't think implicit or explicit is the
>difference you should be citing here.
Why not it is relevant and true that Python is doing an implicit
unpacking of the values in that unnamed tuple.
Quoting myself """It might be the "same" as
no parens but it isn't very clear. If you want a tuple make it
explicit, if you want individual names make it explicit."""
So as you say, maybe it is is fully a matter of style and apparently I
don't like that style!

>They're both an UNPACK_SEQUENCE byte-code.
I have dissed it myself ;)
My argument, if you can call it that, is that it is not clear that
Python is going to do a tuple unpacking here or not !
You had to dis it to find out and you are not a newbie.
# extra fluff from pyshell
>>> import dis
>>> dis.dis(compile('(x,y,dotp,sumx,maxv) =
abc(x,y,dotp,sumx,maxv)','','exec'))
          0 SET_LINENO               0

          3 SET_LINENO               1
          6 LOAD_NAME                0 (abc)
          9 LOAD_NAME                1 (x)
         12 LOAD_NAME                2 (y)
         15 LOAD_NAME                3 (dotp)
         18 LOAD_NAME                4 (sumx)
         21 LOAD_NAME                5 (maxv)
         24 CALL_FUNCTION            5
         27 UNPACK_SEQUENCE          5
         30 STORE_NAME               1 (x)
         33 STORE_NAME               2 (y)
         36 STORE_NAME               3 (dotp)
         39 STORE_NAME               4 (sumx)
         42 STORE_NAME               5 (maxv)
         45 LOAD_CONST               0 (None)
         48 RETURN_VALUE
>>> dis.dis(compile('x,y,dotp,sumx,maxv =
abc(x,y,dotp,sumx,maxv)','','exec'))
          0 SET_LINENO               0

          3 SET_LINENO               1
          6 LOAD_NAME                0 (abc)
          9 LOAD_NAME                1 (x)
         12 LOAD_NAME                2 (y)
         15 LOAD_NAME                3 (dotp)
         18 LOAD_NAME                4 (sumx)
         21 LOAD_NAME                5 (maxv)
         24 CALL_FUNCTION            5
         27 UNPACK_SEQUENCE          5
         30 STORE_NAME               1 (x)
         33 STORE_NAME               2 (y)
         36 STORE_NAME               3 (dotp)
         39 STORE_NAME               4 (sumx)
         42 STORE_NAME               5 (maxv)
         45 LOAD_CONST               0 (None)
         48 RETURN_VALUE
>>> dis.dis(compile('tup = abc(x,y,dotp,sumx,maxv)','','exec'))
          0 SET_LINENO               0

          3 SET_LINENO               1
          6 LOAD_NAME                0 (abc)
          9 LOAD_NAME                1 (x)
         12 LOAD_NAME                2 (y)
         15 LOAD_NAME                3 (dotp)
         18 LOAD_NAME                4 (sumx)
         21 LOAD_NAME                5 (maxv)
         24 CALL_FUNCTION            5
         27 STORE_NAME               6 (tup)
         30 LOAD_CONST               0 (None)
         33 RETURN_VALUE
>>>
You may notice that the act of assigning the return values to a unnamed
tuple and a name have different semantics and that was the real point.
The problem is why should this be the same:

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

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

Seems weird, non-intuitive that a tuple unpacking and tuple creation
have the same bytecode.
So why is this done? What is the reason, I am sure there are a few ;)

p.s. I am leaving out for a while and will read/followup later if
needed. Thank you all for your time. 
M.E.Farmer




More information about the Python-list mailing list