Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51227

Re: Python 3: dict & dict.keys()

Date 2013-07-25 06:47 -0700
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: Python 3: dict & dict.keys()
References <51EF2AD8.3080105@stoneleaf.us> <ksnrr9$k4t$1@ger.gmane.org> <ksp2it$21p$1@ger.gmane.org> <mailman.5061.1374692206.3114.python-list@python.org> <ksqeju$5s7$1@dont-email.me>
Newsgroups comp.lang.python
Message-ID <mailman.5102.1374762804.3114.python-list@python.org> (permalink)

Show all headers | View raw


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~

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Python 3: dict & dict.keys() Ethan Furman <ethan@stoneleaf.us> - 2013-07-24 11:31 -0700
  Re: Python 3: dict & dict.keys() alex23 <wuwei23@gmail.com> - 2013-07-25 16:01 +1000
    Re: Python 3: dict & dict.keys() Ethan Furman <ethan@stoneleaf.us> - 2013-07-25 06:47 -0700
  Re: Python 3: dict & dict.keys() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-25 07:04 +0000
    Re: Python 3: dict & dict.keys() Chris Angelico <rosuav@gmail.com> - 2013-07-25 18:02 +1000
    Re: Python 3: dict & dict.keys() Peter Otten <__peter__@web.de> - 2013-07-25 10:13 +0200
    Re: Python 3: dict & dict.keys() Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-25 09:53 -0600
    Re: Python 3: dict & dict.keys() Peter Otten <__peter__@web.de> - 2013-07-25 18:25 +0200

csiph-web