[Python-ideas] strings as iterables - from str.startswith taking any iterator instead of just tuple

Masklinn masklinn at masklinn.net
Sun Jan 5 00:30:07 CET 2014


On 2014-01-04, at 23:59 , Steven D'Aprano <steve at pearwood.info> wrote:
> On Sat, Jan 04, 2014 at 11:22:16AM +0100, spir wrote:
>> On 01/03/2014 07:11 PM, Bruce Leban wrote:
>>> As to the idea of making strings not iterable, that would break my code. I
>>> write a lot of code to manipulate words (to create puzzles) and iterating
>>> over strings is fundamental. In fact, I'd like to have strings as results
>>> of iteration operations on strings:
>>> 
>>>>>>>>> sorted('string')
>>> 'ginrst'
>>>>>>>>> list(itertools.permutations('bar'))
>>> ['bar', 'bra', 'abr', 'arb', 'rba', 'rab']
> 
> That would be nice to have.

More generally, it would be nice if a sequence type could specify how to
derive a new instance of itself (from an iterable for instance).
Constructors don't necessarily work (e.g. str's constructor). Clojure
has such a concept through the IPersistentCollection protocol:
empty(coll) creates a new (empty) instance of coll (clojure's
collections being immutable, it makes sense to create an empty
collection then add stuff into it via into() or conj())



More information about the Python-ideas mailing list