[Python-Dev] Iterable String Redux (aka String ABC)

Mike Klaas mike.klaas at gmail.com
Thu May 29 01:01:34 CEST 2008


On 28-May-08, at 2:33 PM, Bill Janssen wrote:
>
>>> From what's been discussed so far, I don't see any advantage
>> of isinstance(o, String) over hasattr(o, 'encode') or somesuch.
>
> Look, even if there were *no* additional methods, it's worth adding
> the base class, just to differentiate the class from the Sequence, as
> a marker, so that those of us who want to ask "isinstance(o, String)"
> can do so.
>
> Personally, I'd add in all the string methods to that class, in all
> their gory complexity.  Those who need a compliant class should
> subclass the String base class, and override/add what they need.

I'm not sure I agree with you on the solution, but I definitely agree  
that although str/unicode are conceptually sequences of characters, it  
is rarely useful to think of them as iterables of objects, unlike all  
other Sequences.  (Note: I don't dispute that it is occasionally  
useful to treat them as such.)

In my perfect world, strings would be indicable and sliceable, but not  
iterable.  A character iterator could be obtained using a new method,  
such as .chars().

s = 'hello world'
list(s) # exception
set(s) # exception
tuple(s) # exception
for char in s: # exception
[ord(c) for c in s] # exception
s[2] # ok
s[::-1] # ok
for char in s.chars(): # ok
[ord(c) for c in s.chars()] # ok

Though an argument could be made against this, I consider the current  
behaviour of strings one of the few instances where purity beats  
practicality in python.  It is often the cause of errors that fail too  
late in my experience.

-Mike



More information about the Python-Dev mailing list