Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #97064
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: Idiosyncratic python |
| Date | 2015-09-24 02:54 -0400 |
| References | <560391ea$0$2885$c3e8da3$76491128@news.astraweb.com> <87twqkbavx.fsf@jester.gateway.sonic.net> <560399b0$0$1511$c3e8da3$5496439d@news.astraweb.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.118.1443077670.28679.python-list@python.org> (permalink) |
On 9/24/2015 2:35 AM, Steven D'Aprano wrote:
> On Thursday 24 September 2015 16:16, Paul Rubin wrote:
>
>> Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes:
>>> for k, v in mydict.items():
>>> del(k)
>>
>> That looks wrong: it's deleting k from what?
>
> The local namespace.
>
> py> k = 23
> py> print k
> 23
> py> del k
> py> print k
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> NameError: name 'k' is not defined
>
>
>>> instead of the more obvious
>>> for v in mydict.values():
>>> ...
>>
>> Maybe you mean
>>
>> while mydict:
>> k, v = mydict.popitem()
>> ...
>
>
> Hah, no, you're the second person to make that mistake! One of the guys I
> work with suggested `mydict = {}` to empty the dict.
>
> `del k` (aside: no need for parens, del is a statement, not a function)
> doesn't delete the key from the dictionary. It just deletes the name k. The
> obvious intent is to iterate over the *values* of the dictionary, but the
> coder didn't know about values, so he iterated over (key,value) pairs, then
> deleted the key local variable (not the key in the dict!) to keep the
> namespace clean.
but, but, each iteration rebinds k, so del k is only needed after the
loop .. unless one is super fanatic about cleanliness within the loop ;-)
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Idiosyncratic python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-09-24 16:02 +1000
Re: Idiosyncratic python Paul Rubin <no.email@nospam.invalid> - 2015-09-23 23:16 -0700
Re: Idiosyncratic python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-09-24 16:35 +1000
Re: Idiosyncratic python Ben Finney <ben+python@benfinney.id.au> - 2015-09-24 16:54 +1000
Re: Idiosyncratic python Steven D'Aprano <steve@pearwood.info> - 2015-09-25 11:08 +1000
Re: Idiosyncratic python Terry Reedy <tjreedy@udel.edu> - 2015-09-24 02:54 -0400
Re: Idiosyncratic python wxjmfauth@gmail.com - 2015-09-24 00:06 -0700
Re: Idiosyncratic python Laurent Pointal <laurent.pointal@free.fr> - 2015-09-24 19:50 +0200
Re: Idiosyncratic python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-09-24 21:05 +0100
Re: Idiosyncratic python jmp <jeanmichel@sequans.com> - 2015-09-24 11:12 +0200
Re: Idiosyncratic python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-09-24 14:09 +0100
Re: Idiosyncratic python jmp <jeanmichel@sequans.com> - 2015-09-24 16:07 +0200
Re: Idiosyncratic python Ian Kelly <ian.g.kelly@gmail.com> - 2015-09-24 08:26 -0600
Re: Idiosyncratic python Chris Angelico <rosuav@gmail.com> - 2015-09-25 02:57 +1000
Re: Idiosyncratic python jmp <jeanmichel@sequans.com> - 2015-09-24 20:04 +0200
Re: Idiosyncratic python Ian Kelly <ian.g.kelly@gmail.com> - 2015-09-24 12:19 -0600
Re: Idiosyncratic python Ned Batchelder <ned@nedbatchelder.com> - 2015-09-24 13:46 -0700
Re: Idiosyncratic python Laura Creighton <lac@openend.se> - 2015-09-24 23:08 +0200
Re: Idiosyncratic python Chris Angelico <rosuav@gmail.com> - 2015-09-25 07:49 +1000
Re: Idiosyncratic python Steven D'Aprano <steve@pearwood.info> - 2015-09-25 10:55 +1000
Re: Idiosyncratic python sohcahtoa82@gmail.com - 2015-09-24 15:32 -0700
Re: Idiosyncratic python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-09-25 00:40 +0100
Re: Idiosyncratic python Akira Li <4kir4.1i@gmail.com> - 2015-09-25 03:04 +0300
Re: Idiosyncratic python Steven D'Aprano <steve@pearwood.info> - 2015-09-25 10:08 +1000
csiph-web