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

spir denis.spir at gmail.com
Sat Jan 4 11:22:16 CET 2014


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']
>
>
> instead I have to write
>
>>>> >>>''.join(sorted('string'))
>>>> >>>[''.join(s) for s in itertools.permutations('bar')]

Maybe we just need a 'cat' or 'concat' [1] method for lists:
    sorted('string').cat()
    (s for s in itertools.permutations('bar')).cat()
(Then, a hard choice: should cat crash when items are not strings, or 
automagically stringify its operands? I wish join would do the latter.)

Denis

[1] I have not understood yet why "concatenation", instead of just "catenation". 
Literaly means chaining (things) together; but I'm still trying to figure out 
how one can chain things apart ;-)
As if strings were called "withstrings" or "stringtogethers", more or less. 
Enlightening welcome.

(Same for "concatenative languages"... of which one is called "cat"!)


More information about the Python-ideas mailing list