472,328 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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

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.

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():
lis = xmms2('list')
playtime_str = lis.pop()
(total, playtime, time_str) = playtime_str.split()
(h,m,s) = time_str.split(':')
lis.pop()
return dict( playlist = lis, playtime = (int(h), int(m), int(s)) )

if __name__ == '__main__':
list()

Sep 15 '06 #1
5 4510

me**********@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.

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():
lis = xmms2('list')
playtime_str = lis.pop()
(total, playtime, time_str) = playtime_str.split()
(h,m,s) = time_str.split(':')
lis.pop()
return dict( playlist = lis, playtime = (int(h), int(m), int(s)) )

if __name__ == '__main__':
list()

|>def xmms2(*args):
.... lis = ["xmms2"] + list(args)
.... return lis
....
|>xmms2('list')
['xmms2', 'list']
|>>
|>xmms2('list', 'bananas', 'apples')
['xmms2', 'list', 'bananas', 'apples']
It must be something else?

Also, "nullary"?

Sep 15 '06 #2
me**********@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
Sep 15 '06 #3

me**********@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

Sep 15 '06 #4

Georg Brandl wrote:
me**********@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.
MASSIVELY overlooked it. Gosh. How embarrassing.

Sep 15 '06 #5

John Machin wrote:
me**********@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()"???
No, I'm talking about listing 2 here:
http://www-128.ibm.com/developerwork...on4/index.html

which states:
"If you look closely at the description for the list class in Listing
2, you'll see that two different constructors are provided. One takes
no arguments, and the other takes a sequence class."

and I figured that since the lower bound on arguments to *args could
zero, that Python was
default to the nullary constructor even before the function got its
args.

paranoia will do these things to you.

>
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 just mean that this:
http://docs.python.org/tut/node6.htm...00000000000000

does not use list() at all.
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

Great suggestion. I had not heard of those. Thanks.

Sep 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: geskerrett | last post by:
In the '80's, Microsoft had a proprietary binary structure to handle floating point numbers, In a previous thread, Bengt Richter posted some...
31
by: metiu uitem | last post by:
Say you have a flat list: How do you efficiently get , , ] I was thinking of something along the lines of: for (key,number) in list: print...
38
by: Steven Bethard | last post by:
> >>> aList = > >>> it = iter(aList) > >>> zip(it, it) > > That behavior is currently an accident....
3
by: Mirco Wahab | last post by:
Hi, I have a 2D array, maybe irregular, like arr = , , ] if tried to pull an index list
0
by: Roland Puntaier | last post by:
Searching the web I have found: from numpy import * def simple_integral(func,a,b,dx = 0.001): return sum(map(lambda x:dx*x,...
8
by: Santiago Romero | last post by:
Hi :) First of all, I must apologize for my poor english :) I'm starting with python and pygame and for testing (and learning) purposes I...
0
by: Gabriel Ibanez | last post by:
Hi all .. I'm trying to using the map function to convert a tuple to a list, without success. I would like to have a lonely line that performs...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.