join()

echuck at mindspring.com echuck at mindspring.com
Fri Oct 6 00:08:37 EDT 2000


In Python 2.0, I was surprised to see join() had become a method of
string. I "naturally" expected it to be a method of list.

For example, in Objective-C this is Array-componentsJoinedByString:

In terms of OOP, I'd like to put forth this:

The idea is "join the list with a string" whose general form is "verb
the foo with a bar". I'm sure we can all agree the verb becomes the
method. Typically what comes after "with" are arguments and the direct
object becomes the object: foo.verb(bar). In this case:

  list.join(string)

Consider "sort the list with a function". We don't say function.sort
(list), but list.sort(function), instead. e.g., directObject.verb
(withThis, withThat).

But I know that's just going to inspire endless debate on programming
languages, english language and everyone's personal prefs (and I
couldn't resist).

However, I propose an optional second argument, which makes the case
stronger:

  list.join(separator, lastSeparator=None)

When lastSeparator is specified, it is used as the very last separator:

  list = ['a', 'b', 'c', 'd']
  list.join(', ', ' and ') => 'a, b, c and d'

  list = ['a', 'b']
  list.join(', ', ' and ') => 'a and b'

You have to admit, that's pretty sweet.  :-)

Thoughts? Is this PEP worthy?

-Chuck
--
http://webware.sourceforge.net


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list