[Andrew Koenig]
... Section 4.2.4 of the library reference says that the 'split' method of a regular expression object is defined as
Identical to the split() function, using the compiled pattern.
Supplying words intended to be clear from context, it's saying that the split method of a regexp object is identical to the re.split() function, which is true. In much the same way, list.pop() isn't the same thing as eyeball.pop() <wink>.
This claim does not appear to be correct:
>>> import re >>> re.compile('').split('abcde') ['abcde']
This result differs from the result of using the string split method.
True, but it's the same as
import re re.split('', 'abcde') ['abcde']
which is all the docs are trying to say.
... My first impulse was to argue that (4) is right, and that the behavior should be as follows
>>> 'abcde'.split('') ['a', 'b', 'c', 'd', 'e']
If that's what you want, list('abcde') is a direct way to get it.
... I made the counterargument that one could disambiguate by adding the rule that no element of the result could be equal to the delimiter. Therefore, if s is a string, s.split('') cannot contain any empty strings.
Sure, that's one arbitrary rule <wink>. It doesn't seem to extend to regexps in a reasonable way, though:
re.split('.*', 'abcde') ['', '']
Both split pieces there match the pattern.
However, looking at the behavior of regular expression splitting more closely, I become more confused. Can someone explain the following behavior to me?
>>> re.compile('a|(x?)').split('abracadabra') ['', None, 'br', None, 'c', None, 'd', None, 'br', None, '']