By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

Cast list of objects to list of strings

P: n/a
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.

Jun 27 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
bukzor wrote:
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.
I think what you want is:

def write_err(*args):
from sys import stderr
stderr.write("\n".join([str(o) for o in args]))
but then I don't really understand why you would want such a function so I could
be way wrong.

-Larry
Jun 27 '08 #2

P: n/a
On Jun 2, 4:02 pm, Larry Bates <larry.ba...@websafe.com`wrote:
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
Jun 27 '08 #3

P: n/a
bukzor wrote:
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

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.

--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #4

P: n/a
On Jun 2, 2:56 pm, jay graves <jaywgra...@gmail.comwrote:
On Jun 2, 4:02 pm, Larry Bates <larry.ba...@websafe.com`wrote:
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.
Jun 27 '08 #5

P: n/a
En Mon, 02 Jun 2008 18:56:08 -0300, jay graves <ja********@gmail.com>
escribió:
On Jun 2, 4:02 pm, Larry Bates <larry.ba...@websafe.com`wrote:
>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.

--
Gabriel Genellina

Jun 27 '08 #6

P: n/a
On Jun 2, 8:36 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
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
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.