[Python-3000] Spooky behavior of dict.items() and friends
Mike Klaas
mike.klaas at gmail.com
Thu Apr 3 01:06:30 CEST 2008
On 2-Apr-08, at 3:48 PM, Martin v. Löwis wrote:
>> On Wed, Apr 2, 2008 at 5:08 PM, Mike Klaas <mike.klaas at gmail.com>
>> wrote:
>>> ...and the majority of these cases would work fine with views (input
>>> to sorted(), etc).
>>
>> Suppose "the majority" here means 36 of the 46 cases.
>
> What makes you suppose so. In the standard library of Python 2.5, I
> could not find a single case where using views would cause silent
> breakage:
> - the majority of uses is in for loops or list comprehensions.
> - of the remaining uses, the majority is with .sort(), which
> would cause an exception, to be rewritten as sorted(foo.items())
> - of the then-remaining cases, the majority is immediately followed
> by an iteration, with no intermediate changes to the dictionary.
> - in some cases, the view is returned to the caller (i.e. outside
> of the standard library); whether this would break anything would
> depend on the application.
>
> In your code, how many (in absolute numbers) applications of .items()
> would break when .items() becomes a view?
I assume you are asking Jason even though you attributed the quote to
me.
However, a cursory examination of the 46 non-'for' I quoted above
results in a situation much like you describe: either silently working
fine with view, or louding breaking where a list was expected. There
are some that get passed out of functions which may fail silently but
I don't have time to examine them in detail right now.
-Mike
More information about the Python-3000
mailing list