Why does passing tuple as arg WITHOUT scattering work?

Rhodri James rhodri at wildebst.demon.co.uk
Wed Oct 21 00:14:04 CEST 2009


On Tue, 20 Oct 2009 22:42:07 +0100, Alf P. Steinbach <alfps at start.no>  
wrote:

[snip]

> canvas.create_oval( bbox, fill = "PeachPuff" )

[snip]

> It worked nicely, and I thought this code was fairly perfect until I  
> started studying the language reference.
>
> It seems that formally correct code should apply the scatter operator to  
> the tuple, like this:
>
>
> canvas.create_oval( *bbox, fill = "PeachPuff" )
>
>
> And this /also/ works nicely!
>
> I think it's this latter that is correct, and that the former just  
> worked by accident, due to e.g. the way that some C function parses  
> arguments or such?
>
> But I'm unable to figure it out, so, what's correct (both? one?), and  
> assuming it's the latter that's correct, would the first version still  
> work in practice regardless of Python / Tkinter implementation?

No.  Tkinter.py goes to some lengths to make both of these work, unpacking
bbox if it happens to be a tuple (or packing if it wasn't; I admit I
didn't look at Tkinter.py very hard!)

As to which of the approaches is correct, the answer is whichever one
works for a given function.  It's rare to see the second version, partly
because it's rare to have a tuple in hand that you want to unpack like
that if you don't need to.  However, generally speaking a function will
expect a tuple or not; libraries that can cope with either are the
exception rather than the rule.

-- 
Rhodri James *-* Wildebeest Herder to the Masses



More information about the Python-list mailing list