Documentation of dict views change request

Chris Angelico rosuav at gmail.com
Sun Jan 19 16:56:42 EST 2014


On Mon, Jan 20, 2014 at 8:40 AM, Roy Smith <roy at panix.com> wrote:
> In article <mailman.5728.1390166846.18130.python-list at python.org>,
>  Charles Hixson <charleshixsn at 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
<charleshixsn at 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



More information about the Python-list mailing list