A new to Python question

Bengt Richter bokr at oz.net
Sat May 14 16:27:48 EDT 2005


On Sat, 14 May 2005 20:38:26 +0200, Bernd Nawothnig <Bernd.Nawothnig at t-online.de> wrote:

>
>> On 2005-05-14, M.E.Farmer wrote:
>
>> (x,y,dotp,sumx,maxv) = abc(x,y,dotp,sumx,maxv)
>> This will only create a tuple in memory that has no name to reference
>> it by!
>
>Maybe. But it does not seem to hurt. And I am not sure the tupel _is_
>really created in that situation.
>
One way to get insight into what a statement does is to disassemble its code, e.g.,

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

In the context of a function or module scope, those loads and stores will change
to optimized byte code ops like LOAD_FAST and STORE_FAST etc, but you can see what's
happening above. I.e., on return from the function call, the returned sequence (we know it's
a tuple in your example, but it could be some other sequence thing) is upacked and its elements
are bound to the individual names, just as individual "assignments" would do.

Regards,
Bengt Richter



More information about the Python-list mailing list