Documentation of dict views change request
Roy Smith
roy at panix.com
Sun Jan 19 16:40:58 EST 2014
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.
>>> d = {'foo': 1, 'bar':2}
>>> k = d.keys()
>>> k
['foo', 'bar']
>>> k[0] = "some other key"
>>> k
['some other key', 'bar']
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).
Think this one through. How *could* altering what keys() returns
possibly affect the dict? If it did, that means you could do something
like:
some_dict.keys().append("some other key")
what would that mean? You've added a key, but what's the corresponding
value? I will admit, the picture becomes a bit fuzzier if you consider:
some_dict.items().append(("some other key", 42))
which you could at least imagine would affect the underlying dict.
More information about the Python-list
mailing list