Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed8.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'defaults': 0.05; 'none,': 0.05; 'used.': 0.05; 'none)': 0.07; 'given,': 0.09; 'globals': 0.09; 'specifying': 0.09; 'example:': 0.10; 'python': 0.10; 'def': 0.13; '231': 0.16; '42;': 0.16; '9:15': 0.16; 'ah,': 0.16; 'clause,': 0.16; 'defaulting': 0.16; 'eval': 0.16; 'function"': 0.16; 'happily': 0.16; 'namespace.': 0.16; 'wrote:': 0.16; '>>>': 0.20; '2015': 0.20; 'exec': 0.22; 'function,': 0.22; 'parse': 0.22; 'sep': 0.22; 'am,': 0.23; 'sat,': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'chris': 0.26; 'fri,': 0.27; 'message- id:@mail.gmail.com': 0.27; 'function': 0.28; 'rules': 0.31; 'maybe': 0.33; 'received:google.com': 0.35; 'so,': 0.35; 'star': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; '12,': 0.37; 'seem': 0.37; 'things': 0.38; 'mean': 0.38; 'does': 0.39; "didn't": 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'called': 0.40; 'within': 0.64; '>>>>>': 0.66; 'applying': 0.70; 'huh?': 0.84; 'locals': 0.84; 'specialised': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=PNFHdYbCYwvdu4AYNVQZSS6tQXt2gYkclvVF/psgI8Y=; b=cHoSiKmbN42DPWa/NGkj6EJiyUDRPAAIuP6AQDor3+Gr05pj0Ii3CW1zzqraZQMQss iTCfIlNHsWTeBMH04X1fKCArd5PbuCjmSDarlOobUDYQDtyP+gNmrpfnXd00KLp+n1rf tfp4mmwFdjh7t0Rw3HmOKO8xhpeCypsMaYNUctfPIf65IHX+jWeNvqzZgC5HR9OZMyKU UV5y5u6IHATIIV7Yq4bUiFY4ctDtF81J8w+dg29D6Qh/fQxaWv5OjSkAZtEd373bLCMJ sbfTgRtcaNkoj//x3NspHmElE+ZHOshaRq0h0UhLsh2eRZ8wcn/ZpWJ2DpqurUCk59VX 1dhw== X-Received: by 10.129.103.67 with SMTP id b64mr26656905ywc.55.1441985269788; Fri, 11 Sep 2015 08:27:49 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <55f293da$0$1640$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Fri, 11 Sep 2015 09:27:10 -0600 Subject: Re: Python handles globals badly. To: 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1441985273 news.xs4all.nl 23801 [2001:888:2000:d::a6]:33610 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:96347 On Fri, Sep 11, 2015 at 9:15 AM, Chris Angelico wrote: > On Sat, Sep 12, 2015 at 1:03 AM, Ian Kelly wrote: >>> There's also a bunch of specialised and complicated rules for what happens >>> if you make a star import ("from module import *") inside a function, or >>> call eval or exec without specifying a namespace. Both of these things are >>> now illegal in Python 3. >> >> Huh? >> >>>>> exec("x = 42") >>>>> x >> 42 >>>>> exec("x = 43", None, None) >>>>> x >> 43 >> >> That's in Python 3.4.0. Maybe I don't understand what you mean by >> "without specifying a namespace". > > *inside a function* > >>>> def f(): > ... exec("x = 42") > ... print(x) > ... >>>> x = 231 >>>> f() > 231 Ah, I didn't parse the "inside a function" as applying to that clause, but even so, I don't see in what way that is "now illegal". For example: >>> x = 231 >>> def f(): ... exec("print(x); x = 42; print(x)") ... print(x) ... >>> f() 231 42 231 The exec still happily runs; it's just using its own private locals namespace. Tangent: does the help for exec need to be updated? It currently reads: The globals and locals are dictionaries, defaulting to the current globals and locals. If only globals is given, locals defaults to it. Which would seem to indicate that if called from within a function with no globals or locals, the locals from the function would be used.