On Mon, Jan 20, 2014 at 8:40 AM, Roy Smith <r...@panix.com> wrote: > In article <mailman.5728.1390166846.18130.python-l...@python.org>, > Charles Hixson <charleshi...@earthlink.net> wrote: > >> Could it please be clearly documented that keys(), values(), and items() >> are not writeable. > > We'll, technically, they are. > > Of course, this only changes the list that keys() returns, it doesn't > affect the dictionary itself (which, I assume, is what you were really > talking about).
In Python 3, they return views, not lists. So they really are read-only. On Mon, Jan 20, 2014 at 8:26 AM, Charles Hixson <charleshi...@earthlink.net> wrote: > P.S.: Is it reasonable to return the items() of a dict in order to pass a > read only copy of the values? No, because it isn't a copy. At least, not in Python 3; if you're using Python 2, then the wording of your subject line is inaccurate, see above. >>> d = {"a":1,"b":2,"c":3} >>> i = d.items() >>> del d["b"] >>> list(i) [('a', 1), ('c', 3)] If you returned i from a function and expected it to be a copy, you'd be in for a nasty shock. Try the .copy() method for a shallow copy, or look up deepcopy if you need a recursive copy. ChrisA -- https://mail.python.org/mailman/listinfo/python-list