[Python-3000] Spooky behavior of dict.items() and friends
Mike Klaas
mike.klaas at gmail.com
Thu Apr 3 00:56:33 CEST 2008
On 2-Apr-08, at 3:33 PM, Jason Orendorff 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. Then what
> you're saying is, if I write .items() without thinking, there's about
> a 3% chance it won't work (10 out of 339 cases). Forgive me: the
> fact that you've gotten it down to 3%, e.g. by making items() return a
> view instead of an iterator, doesn't make me terrifically happy.
I apologize: I wasn't trying to make the point that programmers used
to the old behaviour can continue willy-nilly using it without
worrying about the consequences.
Yes, programmers will have to learn the new behaviour; these are among
the subtleties of the new language. I suspect that it will be
mentioned prominently in every "python 3k for 2.X programmers"
tutorial, and 2to3 can handle this translation safely.
I suspect that most cases will not fail quietly, either:
l = d.keys()
l.sort() # exception
l[0] # exception
etc. (there may be other examples, too, like sliceability)
-Mike
More information about the Python-3000
mailing list