[Python-bugs-list] [ python-Bugs-448153 ] Dictionary iterators break old code
noreply@sourceforge.net
noreply@sourceforge.net
Mon, 06 Aug 2001 13:59:01 -0700
Bugs item #448153, was opened at 2001-08-05 06:27
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=448153&group_id=5470
>Category: Python Library
>Group: Python 2.2
Status: Open
Resolution: None
>Priority: 5
Submitted By: Martin v. Löwis (loewis)
>Assigned to: Martin v. Löwis (loewis)
Summary: Dictionary iterators break old code
Initial Comment:
With the introduction of dictionary iterators,
iteration over UserDict specializations may silently
break existing code. The attached code is a
simplification of the xml.dom.NamedNodeMap class,
which provides iteration over the values using a
well-defined order, which is implemented in
__getitem__. In Python 2.2a1, __getitem__ is not
called anymore, thus the code breaks.
----------------------------------------------------------------------
>Comment By: Guido van Rossum (gvanrossum)
Date: 2001-08-06 13:59
Message:
Logged In: YES
user_id=6380
Aaaaaaargh!!!!!!!
You mean somebody wrote a subclass of UserDict that attempts
to behave like a sequence by checking the type of the
__getitem__ argument??????????
Yuck!!!!!!!!!!!!!!!
You XML weenies are sickos. :-)
Now that I've got that out of the way :-),
Short of totally dropping dictionary iterators, the only
solution I can see it to remove the __iter__ method of
UserDict. That is backwards compatible, but it removes the
ability to iterate over UserDict instances (except for the
sick ones :-), thus making UserDict less like a dictionary.
But since UserDict is likely to become deprecated anyway in
favor of subclassing from the 'dictionary' built-in type,
this solution seems acceptable -- but only if this trick is
also in use outside the XML code. I would prefer to see the
XML code fixed, since it can now properly define __iter__ to
iterate over whatever it wants to in any sequence it would
like. :-)
----------------------------------------------------------------------
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=448153&group_id=5470