Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #59477
| From | Rotwang <sg552@hotmail.co.uk> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Getting globals of the caller, not the defining module |
| Date | 2013-11-14 20:56 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <l63de3$ef6$1@dont-email.me> (permalink) |
| References | <5280beb6$0$29976$c3e8da3$5496439d@news.astraweb.com> <aba4de87-cbf6-4c28-8ae3-da2d2a69eeb4@googlegroups.com> |
On 11/11/2013 12:02, sg552@hotmail.co.uk wrote: > (Sorry for posting through GG, I'm at work.) > > On Monday, November 11, 2013 11:25:42 AM UTC, Steven D'Aprano wrote: >> Suppose I have a function that needs access to globals: >> >> # module A.py >> def spam(): >> g = globals() # this gets globals from A >> introspect(g) >> >> As written, spam() only sees its own globals, i.e. those of the module in >> which spam is defined. But I want spam to see the globals of the caller. >> >> # module B >> import A >> A.spam() # I want spam to see globals from B >> >> I can have the caller explicitly pass the globals itself: >> >> def spam(globs=None): >> if globs is None: >> globs = globals() >> introspect(globs) >> >> But since spam is supposed to introspect as much information as possible, >> I don't really want to do that. What (if anything) are my other options? > > How about this? > > # module A.py > import inspect > def spam(): > return inspect.stack()[1][0].f_globals Bump. Did this do what you wanted, or not?
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Getting globals of the caller, not the defining module Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-11 11:25 +0000
Re: Getting globals of the caller, not the defining module Chris Angelico <rosuav@gmail.com> - 2013-11-11 22:34 +1100
Re: Getting globals of the caller, not the defining module sg552@hotmail.co.uk - 2013-11-11 04:02 -0800
Re: Getting globals of the caller, not the defining module Terry Reedy <tjreedy@udel.edu> - 2013-11-11 20:57 -0500
Re: Getting globals of the caller, not the defining module Rotwang <sg552@hotmail.co.uk> - 2013-11-12 08:29 +0000
Re: Getting globals of the caller, not the defining module Rotwang <sg552@hotmail.co.uk> - 2013-11-14 20:56 +0000
Re: Getting globals of the caller, not the defining module Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-15 08:23 +0000
csiph-web