Hi Py3k,

I just started using Python 3000 (for my own projects, nothing
production-y, and mostly for the class decorators and function
annotations) but ever since I noticed PEP 3106, the "spooky" behavior
of dict views has been bothering me...I wouldn't say it has been
keeping me up late at night, but I can't seem to shake this nagging
sensation that it is only a matter of time before I return
dict.items(), .keys(), or .values() from a function while not all of
my wits are about me and end up with some impossible-to-trace
algorithmic error because I wrote mydict.items() instead of
list(mydict.items()). This is what I'm talking about:

>>> mydict = {1:2}
>>> myitems = mydict.items()
>>> [x for x in myitems]
[(1, 2)]
>>> mydict[3] = 'foo'
>>> [x for x in myitems]
[(1, 2), (3, 'foo')]

What really bugs me about this state of affairs is that I consider the
python 2 dict.items() to be safe and free of surprises, but I no
longer feel the same way about it in 3; this is really about the fact
that when you want to get the items, keys, or values of a dict, the
simplest thing is no longer the safest thing. (I don't want to belabor
this point too much since it isn't really my place to judge, but to
me, dict views feel like they are a "special case that breaks the
rules.")

In python 2 we had dict.iteritems(), and I never had any negative
feelings about it because I always considered it "more complicated"
than dict.items(), and I almost never used it. Every time I see
dict.iteritems() I think, "OK, this is a lightweight iterator, it
better get used up before the dict changes." If dict.items() had
stayed the same function but we lost dict.iteritems() and gained
dict.itemsview(), I wouldn't have written this message.

At any rate, my apologies for the long message, but I've been sitting
on this for a while, and I just felt the need to get this out.

Thanks for reading,
David Pokorny
_______________________________________________
Python-3000 mailing list
Python-3000@python.org
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe: 
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com

Reply via email to