Cast list of objects to list of strings

Larry Bates larry.bates at websafe.com`
Mon Jun 2 17:02:34 EDT 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. 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



More information about the Python-list mailing list