[Python-3000] Making strings non-iterable
Brian Harring
ferringb at gmail.com
Tue Apr 18 01:08:05 CEST 2006
On Mon, Apr 17, 2006 at 05:40:19PM -0500, Ian Bicking wrote:
> Raymond Hettinger wrote:
> > -1 on making strings non-iterable. The cure is worse than the disease.
<snip>
> > I'm also -1 on almost all proposals of this kind. IMHO, the first cut
> > of Py3000 should be directed at subtracting the cruft and consolidating
> > what we have. It should be a better Python, not just a different
> > Python. The surest route to that goal is to build on what has been
> > shown to work and experiment with random alternatives that may or may
> > not ultimately prove their worth.
>
> I don't see how this is so dramatically different. I proposed it
> specifically to remove something that at least I considered a language
> wart: an area prone to errors that provides an interface inconsistent
> with other parts of the language.
The issue I'm seeing is that the wart you're pointing at is a general
issue not limited to strings- everyone sooner or later has flattening
code that hits the "recursively iterate over this container, except
for instances of these classes". General algo problem for nested
sequences.
I'd rather see a builtin that makes that case easier then to go and
remove iteration on strings- something akin to
def iter_flatten(lists, dont_iterate=[str]):
stack=[iter(lists)]
while stack:
for x in stack[-1]:
if hasattr(x, "__iter__") and isinstance(x, dont_iterate):
stack.append(iter(x))
break
yield x
else:
stack.pop(-1)
That (assuming I didn't typo it) addresses the issue in a general way,
without removing (useful) __iter__ from instances that occasionally we
don't want to fully expand.
An easier way to invoke a func of the sort from above gets the best of
both worlds- still have iteration over strings, and general solution
to common problem.
Downside is that it forces an extra func call for the following if
it's unknown if the instance is a string or a normal sequence-
for x in iter_flatten(unknown_type_instance):
do_something_with_it(x)
~harring
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-3000/attachments/20060417/ad89a65e/attachment.pgp
More information about the Python-3000
mailing list