Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64326 > unrolled thread
| Started by | Charles Hixson <charleshixsn@earthlink.net> |
|---|---|
| First post | 2014-01-19 13:26 -0800 |
| Last post | 2014-01-20 08:56 +1100 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
Documentation of dict views change request Charles Hixson <charleshixsn@earthlink.net> - 2014-01-19 13:26 -0800
Re: Documentation of dict views change request Roy Smith <roy@panix.com> - 2014-01-20 03:10 +0530
Re: Documentation of dict views change request Chris Angelico <rosuav@gmail.com> - 2014-01-20 08:56 +1100
| From | Charles Hixson <charleshixsn@earthlink.net> |
|---|---|
| Date | 2014-01-19 13:26 -0800 |
| Subject | Documentation of dict views change request |
| Message-ID | <mailman.5728.1390166846.18130.python-list@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
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2014-01-20 03:10 +0530 |
| Message-ID | <roy-B69DE3.03105820012014@news.panix.com> |
| In reply to | #64326 |
In article <mailman.5728.1390166846.18130.python-list@python.org>,
Charles Hixson <charleshixsn@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.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-01-20 08:56 +1100 |
| Message-ID | <mailman.5730.1390168611.18130.python-list@python.org> |
| In reply to | #64327 |
On Mon, Jan 20, 2014 at 8:40 AM, Roy Smith <roy@panix.com> wrote:
> In article <mailman.5728.1390166846.18130.python-list@python.org>,
> Charles Hixson <charleshixsn@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@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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web