How to use imported function to get current globals

Chris Angelico rosuav at gmail.com
Sat Jun 7 16:52:01 EDT 2014


On Sun, Jun 8, 2014 at 3:40 AM, 1989lzhh <1989lzhh at 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?

As Ian said, you almost certainly do not want to do this. But if you
have a solid use-case that involves finding the caller's globals, you
can do it (in CPython - no idea about other Pythons) with the
backtrace.

Normally, passing dictionaries around is going to be MUCH more useful.
(And probably not actually globals(), you almost never want to use
that.)

ChrisA



More information about the Python-list mailing list