A new to Python question

M.E.Farmer mefjr75 at hotmail.com
Sat May 14 16:25:55 EDT 2005


I said exactly what I meant, the parentheses around the values creates
a tuple that you have no reference to! It also has a side effect of
binding the names inside the tuple to a value and placing them in the
local namespace( implicit tuple unpacking ). 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.
>>> def f(q,w,e,r):
...     return q,w,e,r
...
>>> # diff names
>>> a,b,c,d= f(1,2,3,4)# explicit tuple unpacking
>>> dir()
['__builtins__', '__doc__', '__name__', 'a', 'b', 'c', 'd', 'f',
'shell']
>>> del a,b,c,d
>>> # where is the tuple
>>> (a,b,c,d)= f(1,2,3,4)# implicit tuple unpacking !?
>>> dir()
['__builtins__', '__doc__', '__name__', 'a', 'b', 'c', 'd', 'f',
'shell']
>>> del a,b,c,d
>>> # Where is the tuple (a,s,d,f)? There isn't one.
>>> # assign to a single name ( don't do tuple unpacking )
>>> tup=f(1,2,3,4)
>>> dir()
['__builtins__', '__doc__', '__name__', 'f', 'shell', 'tup']
Now there is.
Steve since you are closer to expert than novice you understand the
difference.
I feel this can be confusing to newbies, maybe you disagree.
M.E.Farmer




More information about the Python-list mailing list