Python 3: dict & dict.keys()
Ethan Furman
ethan at stoneleaf.us
Thu Jul 25 09:47:08 EDT 2013
On 07/24/2013 11:01 PM, alex23 wrote:
> On 25/07/2013 4:31 AM, Ethan Furman wrote:
>> 2) Hopefully learn something about when a view is useful.
>
> I haven't seeen this mentioned - forgive me if it's a repeat - but views are constant references to whichever set they
> represent.
>
> Python 2.7:
>
>>>> dd = dict(a=1,b=2,c=3)
>>>> keys = dd.keys()
>>>> 'a' in keys
> True
>>>> dd['d'] = 4
>>>> 'd' in keys
> False
>
> Python 3.3:
>>>> dd = dict(a=1,b=2,c=3)
>>>> keys = dd.keys()
>>>> 'a' in keys
> True
>>>> dd['d'] = 4
>>>> 'd' in keys
> True
>
> If part of my code is only interested in what keys or values are present, it doesn't need to be given a reference to the
> full dictionary, just to whichever view it cares about.
In these cases why is a view better than just using the dict? Is it a safety so the you don't accidentally modify the dict?
--
~Ethan~
More information about the Python-list
mailing list