SPAM-LOW: Re: Function/method returning list of chars in string?
tjreedy at udel.edu
Wed Jun 10 15:31:32 EDT 2009
Carl Banks wrote:
>> Sometimes alternate constructors are needed when there is more than one
>> possible way to create an instance from a given input. In the case of
>> str(iterable), one could want either a string representing the iterable
>> itself, just as with non-iterables, or a string representing the
>> concatenated contents of the iterable. Str.join implements the second
>> choice, with an added string parameter to allow a constant string to be
>> interpolated between the joined items.
> But then how do you rationalize str.split(), which is a method of str
> but a constructor of list?
Str.join takes any iterable of strings and constructs a string. Only
str knows how to do that, though it could have a built-in that called a
hypothetical .__join__ method. However, Python started with just one
string type and there does not seem much use for joining an indefinite
number of tuples or lists. (In any case, there is no string equivalent
of list.extend, which can be used for joining multiple lists.)
Str.split only takes a string arg and splits it up. Only str should
know how to split a string. That it returns the pieces as a list is a
historical artifact. I could have returned a tuple. It could have been
changed in 3.0 to return an iterater, like map and others were. If
Python were being designed today with iterators as a basic protocol, and
without the baggage of back compatibility, I am fairly sure .split would
return an iterator.
> Perhaps instead of worrying about symmetry all the time we should just
> accept the inevitability that things will always be asymmetric and
> impure from someone's perspective. Terry's symmetry is Hendrik's
> asymmetry and vice versa.
That was one of my implied points ;-).
Terry Jan Reedy
More information about the Python-list