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


Groups > comp.lang.python > #72922 > unrolled thread

Re: How to use imported function to get current globals

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2014-06-07 11:57 -0600
Last post2014-06-07 11:57 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#72922 — Re: How to use imported function to get current globals

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-06-07 11:57 -0600
SubjectRe: How to use imported function to get current globals
Message-ID<mailman.10855.1402163915.18130.python-list@python.org>
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.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web