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


Groups > comp.lang.python > #72922

Re: How to use imported function to get current globals

References <602B90F0-E7FE-4887-ADA1-981FD4179EC7@gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2014-06-07 11:57 -0600
Subject Re: How to use imported function to get current globals
Newsgroups comp.lang.python
Message-ID <mailman.10855.1402163915.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, Jun 7, 2014 at 11:40 AM, 1989lzhh <1989lzhh@gmail.com> wrote:
> Here is the code
> m1.py
> def f():
>     print globals()
>
> m2.py
> from m1 import f
> f()# how to get current module's globals?

Evaluate globals() in the current module and pass the resulting dict
in as a parameter:

# m1.py
def f(globals):
    print globals

# m2.py
from m1 import f
f(globals())

There's a code smell here, though.  If your function really needs to
interact with globals from other modules, then those should probably
not be globals in the first place.  More likely they should be
attributes of objects that can be easily passed around.

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


Thread

Re: How to use imported function to get current globals Ian Kelly <ian.g.kelly@gmail.com> - 2014-06-07 11:57 -0600

csiph-web