Path: csiph.com!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subject:Python': 0.05; 'assignment': 0.07; 'cc:addr:python-list': 0.09; 'dict': 0.09; 'sane': 0.09; 'def': 0.13; 'ah,': 0.16; 'cc:name:python': 0.16; 'empty),': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'wrote:': 0.16; '(in': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'logical': 0.22; 'sep': 0.22; 'am,': 0.23; 'sat,': 0.23; 'header:In-Reply-To:1': 0.24; 'sense': 0.26; 'updating': 0.27; 'message- id:@mail.gmail.com': 0.27; '[2]': 0.27; 'behaviour': 0.29; 'documenting': 0.29; 'starts': 0.29; 'certainly': 0.30; '[1]': 0.32; 'returned': 0.32; 'source': 0.33; 'received:google.com': 0.35; 'but': 0.36; 'created': 0.36; 'subject:: ': 0.37; '12,': 0.37; 'more': 0.63; '>>>>>': 0.66; 'worth': 0.67; 'chrisa': 0.84; 'locals': 0.84; 'thing...': 0.84; 'using.': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=9MODFJELWOr7Mqg1dTomAmYCakZe6DcdDafDMBD+OYo=; b=Fx8ZUtqNxG/iGvZJjJkfXhfLJbevGoaHiMIGRSpLzKMkcwnCKGBnzh6NC74R/RGOBb PKBrX2hNbWuPhDI0ldY8QXbJstbf74/dd2GTFwjV4Orku2I+ow8QExH9ylzptbMrqjV9 MLwuzPnLBluCunuwkjA0268MthXDfo7RVAj3xlKASoFUEXIEdFA8TxDqMj7ghUcEDUoD m01b8DEz9eKKwWp1KXqdrg0bd+9NGPXvlFaBJGg3ygCKp6uAxIaT/uBVu2eIOw11INwT WjT484Lwhra9EIb6XFnbTuJ0QY3ninpwj3mpHnMb2gJq+UgyZhvNRJBr9eVNsfAs/xcj Y8kQ== MIME-Version: 1.0 X-Received: by 10.107.41.142 with SMTP id p136mr4739979iop.19.1441986928352; Fri, 11 Sep 2015 08:55:28 -0700 (PDT) In-Reply-To: References: <55f293da$0$1640$c3e8da3$5496439d@news.astraweb.com> Date: Sat, 12 Sep 2015 01:55:28 +1000 Subject: Re: Python handles globals badly. From: Chris Angelico Cc: Python Content-Type: text/plain; charset=UTF-8 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: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1441986932 news.xs4all.nl 23747 [2001:888:2000:d::a6]:43857 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:96352 On Sat, Sep 12, 2015 at 1:49 AM, Ian Kelly wrote: >> And that's the thing... I think. It's using locals(), which starts out >> as a copy of the function's locals (in this example, empty), but >> without assignment affecting anything. Which is more than a little >> weird: >> >>>>> def f(): >> ... x = [1] >> ... exec("print(x); x[0] = 2; print(x); x = [3]; print(x)") >> ... print(x) >> ... >>>>> f() >> [1] >> [2] >> [3] >> [2] > > Ah, that makes sense. It's writing into the dict that is created and > returned by locals(), but not actually updating the frame locals which > are the source of truth. Yeah... but it only makes sense to people who understand the implementation. It's certainly not a logical and sane behaviour that would be worth documenting and using. ChrisA