Converting a varargs tuple to a list - a definite pitfall for new comers to Python
John Machin
sjmachin at lexicon.net
Fri Sep 15 02:16:35 EDT 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.
Are you using "the nullary constructor list()" to mean "the 0-argument
function list() that appears later in the script", and "the other one
which takes a sequence" to mean the builtin function list()"??? If, so
I guess it's not just newcomers to the English language who would be
struggling to comprehend :-)
> And the Python docs dont give a good example of dealing with
> taking a sequence of args and converting it to a list.
You have produced 2 perfectly good examples yourself. What's your
point?
>
> I must've spent 40 minutes looking through my ora books, googling and
> irc'ing in vane on this.
There is nothing at all special about a sequence of args. It's just a
tuple, as documented and discoverable:
| >>> def func(*args):
| ... print type(args), repr(args)
| ...
| >>> func(1,2,3,4)
| <type 'tuple'> (1, 2, 3, 4)
| >>>
Here's a tip: when you get into a pickle like that, try running
pychecker and/or pylint over your code. Here's what pychecker has to
say:
metaperllist.py:4: Invalid arguments to (list), got 1, expected 0
metaperllist.py:10: (list) shadows builtin
HTH,
John
More information about the Python-list
mailing list