[Python-Dev] Re: PEP 292 - Simpler String Substitutions

Michael Chermside mcherm at mcherm.com
Mon Aug 23 19:04:55 CEST 2004


Tim Peters writes:
> The same applies to string.join(iterable), for that matter.

Clark Evans writes:
> This code-snippet is littered everwhere in my applications:
>
>     string.join([str(x) for x in iterable])
>
> Its tedious and makes code hard to read.  Do we need a PEP to fix this?

I'm convinced! Of course any code like the following would break:

   class Item:
       # ...
       def __eq__(self, other):
           "All Items are equal!"
           return isinstance(other, Item)

   def process_item(item):
       """Processes an item, returning a string."""
       # ...

   def process_items(items):
       """This processes each Item in the list and
       returns a string containing the entire thing."""
       while True:
           try:
               return ','.join(items)
           except TypeError:
               pos = items.index(Item()) # find first Item
               items[pos] = process_item( items[pos] )
               continue

<Big Wink>

Seriously, I can't think of a sane use for <String>.join() raising
a TypeError, so I'd say run it by Guido and if it doesn't offend
his design intuition, let's fix it!

-- Michael Chermside



More information about the Python-Dev mailing list