The inverse of .join

Stephen Hansen me+list/python at ixokai.io
Thu Jun 17 15:59:12 EDT 2010


On 6/17/10 12:44 PM, MRAB wrote:
> Neil Cerutti wrote:
>> On 2010-06-17, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>>> On Thu, Jun 17, 2010 at 11:45 AM, Neil Cerutti
>>> <neilc at norwich.edu> wrote:
>>>> What's the best way to do the inverse operation of the .join
>>>> function?
>>> Use the str.split method?
>>
>> split is perfect except for what happens with an empty string.
>>
> I see what you mean.
> 
> This is consistent:
> 
>>>> ','.join([''])
> ''
>>>> ''.split(',')
> ['']
> 
> but this isn't:
> 
>>>> ','.join([])
> ''
>>>> ''.split(',')
> ['']
> 
> An empty string could be the result of .join(['']) or .join([]).
> 
> Should .split grow an addition keyword argument to specify the desired
> behaviour? (Although it's simple enough to define your own function.)

Guido finds keyword-arguments-to-change-behavior to be unPythonic, IIRC.
It generally means 'make a new API'. But, the question is-- is it worth
the mental strain of a new API?

This is such an extreme edge case, having to do:

  if blah:
    result = blah.split(',')
  else:
    result = []

Is really not asking too much, I think.

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 487 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20100617/1eef980c/attachment.sig>


More information about the Python-list mailing list