Passing items of a lsit as arguments

Jeff Epler jepler at unpythonic.net
Sat Mar 27 05:04:22 CET 2004


On Fri, Mar 26, 2004 at 09:31:47PM +0000, Tobias Pfeiffer wrote:
> Hi!
> 
> I want to call a function that was defined with *args in the header, e.g. 
> the Tkinter.Canvas.create_polygon function. I got the coordinates in a 
> long list but how can I achieve that every item in this list is passed as 
> a single parameter?
> 
> Example:
> bla = [(1,2), (3,4), (5,6), ...]
> has to become
> create_polygon((1,2), (3,4), (5,6), ..., fill=blue)

You can call the functions with the arguments you want this way:
    create_polygon(*bla + (...,), **{'fill'=blue})

However, Tkinter.Canvas is forgiving in the way it accepts arguments.
For instance, this works just fine:
    >>> c.create_polygon([(10, 10), (10, 30), (30, 30), (30, 10)],  fill="blue")
or even
    >>> c.create_polygon([40, 40, 60, 40, 60, 60, 40, 60], fill="green")
so just write '...create_polygon(bla, ..., fill=blue)'.

Jeff




More information about the Python-list mailing list