Converting a varargs tuple to a list - a definite pitfall for new comers to Python

Georg Brandl g.brandl-nospam at gmx.net
Fri Sep 15 07:02:28 CEST 2006


metaperl.etc at gmail.com wrote:
> The following program does not work if you uncomment     #lis =
> ["xmms2"] + list(args)
> 
> Evidently Python is opting for the nullary constructor list() as
> opposed to the other one which takes a sequence. But no newcomer would
> know this. And the Python docs dont give a good example of dealing with
> taking a sequence of args and converting it to a list.

Evidently you overlooked having defined a list() in global scope
yourself which takes no arguments.

> I must've spent 40 minutes looking through my ora books, googling and
> irc'ing in vane on this.
> 
> import subprocess
> 
> def xmms2(*args):
>     #lis = ["xmms2"] + list(args)
>     lis = ["xmms2"] + [a for a in args]
>     output = subprocess.Popen(lis,
> stdout=subprocess.PIPE).communicate()[0]
>     return output.splitlines()
> 
> def list():
  ^^^^^^^^^^^

This is called by list(args) above, and the call fails.
If you want to avoid such clashes, don't name your functions list,
dict, int, str etc.

Georg



More information about the Python-list mailing list