Path: csiph.com!goblin1!goblin.stu.neva.ru!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'deletes': 0.09; 'iterate': 0.09; 'nameerror:': 0.09; 'namespace': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'jan': 0.11; 'subject:python': 0.14; '*values*': 0.16; 'dictionary,': 0.16; 'dictionary.': 0.16; 'iterated': 0.16; 'iteration': 0.16; 'namespace.': 0.16; 'pairs,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.16; ';-)': 0.18; 'variable': 0.18; '>>>': 0.20; '(not': 0.20; '2015': 0.20; 'suggested': 0.20; '"",': 0.22; 'am,': 0.23; 'defined': 0.23; 'second': 0.24; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'paul': 0.24; 'header :User-Agent:1': 0.26; "doesn't": 0.26; 'header:X-Complaints-To:1': 0.26; 'looks': 0.29; 'clean.': 0.29; 'print': 0.30; 'maybe': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'traceback': 0.33; 'file': 0.34; 'but': 0.36; 'instead': 0.36; 'needed': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'guys': 0.38; 'no,': 0.38; 'delete': 0.38; 'mean': 0.38; "didn't": 0.39; 'to:addr:python.org': 0.40; 'more': 0.63; 'within': 0.64; 'intent': 0.66; 'statement,': 0.66; 'thursday': 0.66; 'obvious': 0.76; 'dict.': 0.84; 'function)': 0.84; 'received:fios.verizon.net': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Idiosyncratic python Date: Thu, 24 Sep 2015 02:54:16 -0400 References: <560391ea$0$2885$c3e8da3$76491128@news.astraweb.com> <87twqkbavx.fsf@jester.gateway.sonic.net> <560399b0$0$1511$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-59-124-74.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 In-Reply-To: <560399b0$0$1511$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1443077670 news.xs4all.nl 23812 [2001:888:2000:d::a6]:45011 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97064 On 9/24/2015 2:35 AM, Steven D'Aprano wrote: > On Thursday 24 September 2015 16:16, Paul Rubin wrote: > >> Steven D'Aprano 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 "", line 1, in > 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