Cast list of objects to list of strings

Gary Herron gherron at
Tue Jun 3 00:33:12 CEST 2008

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.
> --

More information about the Python-list mailing list