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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.05; 'globals': 0.09; 'namespace': 0.09; 'script,': 0.09; 'stack': 0.13; 'def': 0.13; 'explicitly': 0.15; '1:48': 0.16; 'fancy': 0.16; 'php)': 0.16; 'wrote:': 0.16; '2015': 0.20; 'sep': 0.22; 'sorry,': 0.22; 'passing': 0.23; 'this:': 0.23; 'header:In-Reply- To:1': 0.24; 'script': 0.25; "doesn't": 0.26; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'inspect': 0.29; 'probably': 0.31; 'another': 0.32; 'statement': 0.32; 'received:google.com': 0.35; 'could': 0.35; 'something': 0.35; 'instead': 0.36; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'takes': 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'called': 0.40; 'more': 0.63; 'locals': 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=gQ2ybWX8nfFEYPXHn9SQJBeLO9GwJ2yUQ47lXkeLWB4=; b=TCVCvaAaZ6Xx+l/b/3JZvh++mCVdR0d6Glr7RR3I9s22bBg2xxbWxpzC2xe4UD/B9f ci+1jZwgUyw8BRtK8PFqkiEqLJo3q1ESZV6+LoaY820nKLVvpLP6xrNuUYOr+sFpAQ8H dqGV3tBSdXccyLL7vNsdLy3wXhoP4FGKq/k3s+ms4jcJ2GSVfM5A7eeApf+fexQ6OgIV eBgUN7ZIYeF7Bxe0NCZcakhff+GUWW0LzQqjkJBn2V+9fDZNAKHqBngp6SkMbZzRnNi0 bVJWvJ9kfUQXjFNM1UY3vACXiHj+6Mf6KHLqfDZbeNdurFIGHBtSUJ5zZqNX6WJsl3FM g3rw== X-Received: by 10.129.45.194 with SMTP id t185mr6496288ywt.111.1441396381602; Fri, 04 Sep 2015 12:53:01 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <58e0d1b5-a7ca-4811-9926-fba1b7ede83f@googlegroups.com> From: Ian Kelly Date: Fri, 4 Sep 2015 13:52:22 -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: 20 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1441396389 news.xs4all.nl 23816 [2001:888:2000:d::a6]:39355 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:96030 On Fri, Sep 4, 2015 at 1:48 PM, Ian Kelly wrote: > On Fri, Sep 4, 2015 at 1:11 PM, wrote: >> 6- "include" script statement (extending namespace to another script, like PHP) > > def include(filename): > exec(open(filename).read()) Sorry, that doesn't work because it takes locals from the include function. You probably need something more like this: def include(filename, globals): exec(open(filename).read(), globals) To be called like: include("foo.py", globals()) If you want to get fancy you could probably have include inspect the stack to pull the globals from the parent stack frame instead of explicitly passing them in.