Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64329
| References | <mailman.5728.1390166846.18130.python-list@python.org> <roy-B69DE3.03105820012014@news.panix.com> |
|---|---|
| Date | 2014-01-20 08:56 +1100 |
| Subject | Re: Documentation of dict views change request |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5730.1390168611.18130.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
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
csiph-web