Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #96030

Re: Python handles globals badly.

References <c3363$547e74fe$5419aafe$24179@news.ziggo.nl> <58e0d1b5-a7ca-4811-9926-fba1b7ede83f@googlegroups.com> <CALwzid=MTtd6r=0WfX=ohU_tuEgjQjF8qr+_mrX=amjByOiA2g@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2015-09-04 13:52 -0600
Subject Re: Python handles globals badly.
Newsgroups comp.lang.python
Message-ID <mailman.147.1441396389.8327.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Sep 4, 2015 at 1:48 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Fri, Sep 4, 2015 at 1:11 PM,  <tdev@freenet.de> 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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Python handles globals badly. tdev@freenet.de - 2015-09-04 12:11 -0700
  Re: Python handles globals badly. Ian Kelly <ian.g.kelly@gmail.com> - 2015-09-04 13:48 -0600
  Re: Python handles globals badly. Ian Kelly <ian.g.kelly@gmail.com> - 2015-09-04 13:52 -0600
  Re: Python handles globals badly. Chris Angelico <rosuav@gmail.com> - 2015-09-05 10:27 +1000
  Re: Python handles globals badly. Michael Torrie <torriem@gmail.com> - 2015-09-04 19:42 -0600
  Re: Python handles globals badly. Chris Angelico <rosuav@gmail.com> - 2015-09-05 11:54 +1000
    Program in or into (was Python handles globals badly) Rustom Mody <rustompmody@gmail.com> - 2015-09-04 20:18 -0700
      Re: Program in or into (was Python handles globals badly) Chris Angelico <rosuav@gmail.com> - 2015-09-05 13:31 +1000
      Re: Program in or into (was Python handles globals badly) Steven D'Aprano <steve@pearwood.info> - 2015-09-06 12:35 +1000
        Re: Program in or into (was Python handles globals badly) MRAB <python@mrabarnett.plus.com> - 2015-09-06 03:54 +0100
        Re: Program in or into (was Python handles globals badly) Rustom Mody <rustompmody@gmail.com> - 2015-09-05 21:35 -0700
        Re: Program in or into (was Python handles globals badly) random832@fastmail.us - 2015-09-06 01:26 -0400
          Re: Program in or into (was Python handles globals badly) wxjmfauth@gmail.com - 2015-09-06 00:42 -0700
        Re: Program in or into (was Python handles globals badly) Ian Kelly <ian.g.kelly@gmail.com> - 2015-09-06 18:19 -0600

csiph-web