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


Groups > comp.lang.python > #72955

Re: How to use imported function to get current globals

References <602B90F0-E7FE-4887-ADA1-981FD4179EC7@gmail.com> <ln0he7$p1u$1@ger.gmane.org>
From 1989lzhh <1989lzhh@gmail.com>
Subject Re: How to use imported function to get current globals
Date 2014-06-08 13:51 +0800
Newsgroups comp.lang.python
Message-ID <mailman.10874.1402206730.18130.python-list@python.org> (permalink)

Show all headers | View raw


thanks all you guys. I have find the solution which is quite simple by using sys._frame(1).f_locals in function to get the caller's scope
The following is my user case:
I am writing a tool to translate python code to cython code then compiled using decorate.

jit, build=make("mymodule")
#jit function collect python code and signature then translate to cython code
@jit('int(int)',
    locals='''
    int b;
    ''')
def f(a):
    b=1
    return a+1

build()# compile cython code and load compiled module then expose compiled function to current namespace. So this is my purpose to get caller's scope
f()# now f is a compiled function 

    

发自我的 iPhone

> 在 Jun 8, 2014,10:24,Dave Angel <davea@davea.name> 写道:
> 
> 1989lzhh <1989lzhh@gmail.com> Wrote in message:
>> 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 others have said, it's probably a bad idea.  I can think of 3
> reasons to try: teacher said so, writing a debugger, 
> transliterating code from a crude language into python.
> 
> Could you elaborate on what you really want? Which of those two
> modules is your main script? Which code in which module is trying
> to get which module's globals?  And is the connection static or
> dynamic? And do you want a snapshot of them, or to be able to
> modify and track changes? 
> 
> 
> 
> 
> -- 
> DaveA
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Thread

Re: How to use imported function to get current globals 1989lzhh <1989lzhh@gmail.com> - 2014-06-08 13:51 +0800

csiph-web