A new to Python question

Steven Bethard steven.bethard at gmail.com
Sat May 14 16:43:52 EDT 2005


M.E.Farmer wrote:
> 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.

It actually is the same, and I don't think implicit or explicit is the 
difference you should be citing here.  The parentheses are fully 
optional -- they don't change the semantics at all:

py> t = (4, 5)
py> a = t
py> a is t
True
py> a = (b, c) = t
py> a is t
True
py> a = b, c = t
py> a is t
True

In all cases, "a" still gets assigned the tuple (4, 5).  Whether or not 
you put the parentheses around "b, c" is fully a matter of style.

I don't know the implementation enough to know whether or not a tuple is 
actually created when "b" and "c" are bound to their values, but I'd be 
willing to bet that whatever happens to "(b, c)" is exactly the same as 
what happens to "b, c".

STeVe



More information about the Python-list mailing list