Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Documentation of dict views change request

6 views
Skip to first unread message

Charles Hixson

unread,
Jan 19, 2014, 4:26:03 PM1/19/14
to pytho...@python.org
Could it please be clearly documented that keys(), values(), and items()
are not writeable. I agree that this is how they should be, but it
would be still better if they were clearly documented as such. The
labeling of them as dynamic, while true, was a bit confusing here.
(I.e., it was talking about changing their value through other means of
access rather than directly through the returned values.)

P.S.: Is it reasonable to return the items() of a dict in order to pass
a read only copy of the values?

--
Charles Hixson

Roy Smith

unread,
Jan 19, 2014, 4:40:58 PM1/19/14
to
In article <mailman.5728.1390166...@python.org>,
Charles Hixson <charle...@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.

Mark Lawrence

unread,
Jan 19, 2014, 4:41:25 PM1/19/14
to pytho...@python.org
On 19/01/2014 21:26, Charles Hixson wrote:
> Could it please be clearly documented that keys(), values(), and items()
> are not writeable. I agree that this is how they should be, but it
> would be still better if they were clearly documented as such. The
> labeling of them as dynamic, while true, was a bit confusing here.
> (I.e., it was talking about changing their value through other means of
> access rather than directly through the returned values.)
>
> P.S.: Is it reasonable to return the items() of a dict in order to pass
> a read only copy of the values?
>

Just raise an issue here http://bugs.python.org/

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

Chris Angelico

unread,
Jan 19, 2014, 4:56:42 PM1/19/14
to pytho...@python.org
On Mon, Jan 20, 2014 at 8:40 AM, Roy Smith <r...@panix.com> wrote:
> In article <mailman.5728.1390166...@python.org>,
> Charles Hixson <charle...@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
<charle...@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

Terry Reedy

unread,
Jan 19, 2014, 6:22:07 PM1/19/14
to pytho...@python.org
On 1/19/2014 4:41 PM, Mark Lawrence wrote:
> On 19/01/2014 21:26, Charles Hixson wrote:
>> Could it please be clearly documented that keys(), values(), and items()
>> are not writeable. I agree that this is how they should be, but it
>> would be still better if they were clearly documented as such. The
>> labeling of them as dynamic, while true, was a bit confusing here.
>> (I.e., it was talking about changing their value through other means of
>> access rather than directly through the returned values.)
>>
>> P.S.: Is it reasonable to return the items() of a dict in order to pass
>> a read only copy of the values?
>>
>
> Just raise an issue here http://bugs.python.org/

The relevant section is

4.10.1. Dictionary view objects¶

The objects returned by dict.keys(), dict.values() and dict.items() are
view objects.

The 'view object' are implicitly 'read-only' because no syntax is given
to write to them, but ahead and propose adding 'read-only' to make the
above explicitly say "read-only view objects".

--
Terry Jan Reedy


0 new messages