Cast list of objects to list of strings

B

bukzor

I have this function:

def write_err(obj):
from sys import stderr
stderr.write(str(obj)+"\n")

and I'd like to rewrite it to take a variable number of objects.
Something like this:

def write_err(*objs):
from sys import stderr
stderr.write(" ".join(objs)+"\n")

but I lose the property that the function works on any object. What's
the simplest way to fix this? In essence, I need to cast a list of
objects to a list of strings. I'd like to do just "str(objs)" but that
(obviously) doesn't quite do what I need.
 
J

jay graves

I think what you want is:
def write_err(*args):
from sys import stderr
stderr.write("\n".join([str(o) for o in args]))

Slight nitpick. If you are using version >= 2.4 you could use a
generator expression instead of a list comprehension to avoid building
and throwing away a list.

"\n".join(str(o) for o in args)

http://www.python.org/dev/peps/pep-0289/

Very unlikely to yield a material time difference in this case but
cleaner IMO.

....
Jay Graves
 
G

Gary Herron

bukzor said:
I have this function:

def write_err(obj):
from sys import stderr
stderr.write(str(obj)+"\n")

and I'd like to rewrite it to take a variable number of objects.
Something like this:

def write_err(*objs):
from sys import stderr
stderr.write(" ".join(objs)+"\n")

but I lose the property that the function works on any object.

No you don't. If you were happy with printing the str(...) of a single
objects, why not just printout the (concatenation) of the str(...) of
each of many objects?

stderr.write(" ".join([str(b) for b in objs])+"\n")

Gary Herron
 
B

bukzor

I think what you want is:
def write_err(*args):
from sys import stderr
stderr.write("\n".join([str(o) for o in args]))

Slight nitpick. If you are using version >= 2.4 you could use a
generator expression instead of a list comprehension to avoid building
and throwing away a list.

"\n".join(str(o) for o in args)

http://www.python.org/dev/peps/pep-0289/

Very unlikely to yield a material time difference in this case but
cleaner IMO.

...
Jay Graves

Thanks! That's exactly what I was looking for.
 
G

Gabriel Genellina

I think what you want is:
def write_err(*args):
from sys import stderr
stderr.write("\n".join([str(o) for o in args]))

Slight nitpick. If you are using version >= 2.4 you could use a
generator expression instead of a list comprehension to avoid building
and throwing away a list.

"\n".join(str(o) for o in args)

http://www.python.org/dev/peps/pep-0289/

Very unlikely to yield a material time difference in this case but
cleaner IMO.

Still nitpicking: using a generator expression in this case has no
advantage. The first thing that str.join does is to create a list out of
its argument (unless it is already a list or a tuple). In fact, a list
comprehension is faster here.
 
J

jay graves

Still nitpicking: using a generator expression in this case has no
advantage. The first thing that str.join does is to create a list out of
its argument (unless it is already a list or a tuple). In fact, a list
comprehension is faster here.

Really! I stand corrected.

I'm not a C programmer but I peeked at the source. I see that it
makes a pass to calculate the total size and then another pass to
catenate the items, which makes sense from a memory allocation
standpoint.

To compare and contrast, I looked up the 'sum' built in and saw that
it used the iterator protocol, (PyObject_GetIter). I assume that the
other similar functions (min,max, etc) would do the same.

Thanks for setting me straight.
....
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top