Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'subject:not': 0.03; 'explicitly': 0.05; 'args': 0.07; 'attribute': 0.07; 'modified': 0.07; 'none:': 0.07; 'subject:Getting': 0.07; 'suppose': 0.07; 'caller': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:module': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; '(sorry': 0.16; 'a.py': 0.16; 'b.py': 0.16; 'builtins': 0.16; 'c.py': 0.16; 'caller.': 0.16; 'defined.': 0.16; 'exist.': 0.16; 'globals': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'sees': 0.16; 'utc,': 0.16; 'work.)': 0.16; 'wrote:': 0.18; 'module': 0.19; 'possible,': 0.19; 'stack': 0.19; 'written,': 0.19; 'import': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'pass': 0.26; 'defined': 0.27; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'possibility': 0.29; "i'm": 0.30; 'posting': 0.31; 'that.': 0.31; 'coded': 0.31; "d'aprano": 0.31; 'inspect': 0.31; 'prints': 0.31; 'steven': 0.31; 'there.': 0.32; 'supposed': 0.32; 'guess': 0.33; 'monday,': 0.33; 'subject:the': 0.34; 'case,': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'i.e.': 0.36; 'doing': 0.36; 'should': 0.36; 'filter': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'how': 0.40; 'received:173': 0.61; 'information': 0.63; 'real': 0.63; 'map': 0.64; 'received:fios.verizon.net': 0.84; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Getting globals of the caller, not the defining module Date: Mon, 11 Nov 2013 20:57:19 -0500 References: <5280beb6$0$29976$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-59-117-133.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.1.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384221452 news.xs4all.nl 15916 [2001:888:2000:d::a6]:55731 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59136 On 11/11/2013 7:02 AM, 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 In Python 3, the attribute is __globals__. In either case, it is only defined on Python coded functions, so one should be prepared for it to not exist. That possibility is real because there *are* builtins like map and filter that take function args and call them. Inspect has been modified in Py 3, but stack is still there. > # module B.py > import A > print(A.spam() is globals()) # prints True > def f(): > return A.spam() > > # module C.py > import B > print(B.f() is vars(B)) # prints True > > I don't really know what I'm doing but I guess it won't work in alternative implementations of Python. > -- Terry Jan Reedy