Cast list of objects to list of strings

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Jun 2 21:36:56 EDT 2008


En Mon, 02 Jun 2008 18:56:08 -0300, jay graves <jaywgraves at gmail.com>  
escribió:

> On Jun 2, 4:02 pm, Larry Bates <larry.ba... at 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




More information about the Python-list mailing list