[Python-3000] Spooky behavior of dict.items() and friends

"Martin v. Löwis" martin at v.loewis.de
Thu Apr 3 00:48:40 CEST 2008


> 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?

Regards,
Martin



More information about the Python-3000 mailing list