Inconsistent behavior of split on empty string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bk_kl@gmx.de

    Inconsistent behavior of split on empty string

    Hi,

    I think the following behavior of the build in function 'split' is inconsistent.
    What do you think?
    [color=blue][color=green][color=darkred]
    >>> "".split()[/color][/color][/color]
    [][color=blue][color=green][color=darkred]
    >>> "".split("; ")[/color][/color][/color]
    ['']

    I'm using python 2.3.3 on Windows 2000.
    (Perl's split only returns all items up to the last non-empty item,
    e.g. <perl>split /;/, "; ;;;"; gets you ['', ' ']. I find this confusing, too).
  • Bengt Richter

    #2
    Re: Inconsistent behavior of split on empty string

    On 13 Feb 2004 00:46:00 -0800, bk_kl@gmx.de (bk_kl@gmx.de) wrote:
    [color=blue]
    >Hi,
    >
    >I think the following behavior of the build in function 'split' is inconsistent.
    >What do you think?
    >[color=green][color=darkred]
    >>>> "".split()[/color][/color]
    >[][color=green][color=darkred]
    >>>> "".split("; ")[/color][/color]
    >['']
    >
    >I'm using python 2.3.3 on Windows 2000.
    >(Perl's split only returns all items up to the last non-empty item,
    > e.g. <perl>split /;/, "; ;;;"; gets you ['', ' ']. I find this confusing, too).[/color]

    ..split() is not an information-preserving split. It is a convenient special case
    whose function should not be confused with that of .split(somethin g).

    The latter would be buggy if you could not successfully do this:
    [color=blue][color=green][color=darkred]
    >>> anystring = ''
    >>> somestring = ';'
    >>> assert anystring == somestring.join (anystring.spli t(somestring))
    >>> anystring == somestring.join (anystring.spli t(somestring))[/color][/color][/color]
    True

    Unfortunately (other than making a likely bug announce itself),
    [color=blue][color=green][color=darkred]
    >>> somestring = ''
    >>> anystring == somestring.join (anystring.spli t(somestring))[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    ValueError: empty separator

    but otherwise I think it works.

    Regards,
    Bengt Richter

    Comment

    Working...