On Sat, Feb 26, 2011 at 11:03 AM, Mart Sõmermaa <mrts.pydev@gmail.com> wrote:
IMHO, x.join(a).split(x) should be "idempotent" in regard to a.
foo = ['a', 'b', 'c'] assert '|'.join(foo).split('|') == foo foo = ['a'] assert '|'.join(foo).split('|') == foo foo = [] assert ' '.join(foo).split() == foo
And now the odd exception to the rule:
assert '|'.join(foo).split('|') == foo Traceback (most recent call last): File "<stdin>", line 1, in <module> AssertionError
That forces one to write special case code when using custom separators. Consider:
# clean baz = dict(chunk.split('=') for chunk in baz.split()) # ugly baz = (dict(chunk.split('=') for chunk in baz.split("|")) if baz else {})
Our younger cousin Ruby has no such idiosyncrasies:
It is no idiosyncrazy -- Split returns what it should return - a list with an empty string:
''.split("|") [''] and it would break a lot of code if it didn't. Filtering out lists with empty string does not see a big issue compared to the inconsistencies that would arise from any different behavior for split.
Any list of strings does the roundtrip with a join->split sequence. Lists of any other elements, or empty lists don't. js -><-
foo = [] foo.join('|').split('|') == foo => true
What is the reason for that oddity? Can we amend it?
Best regards, Mart Sõmermaa _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas