How to use imported function to get current globals

2014-06-07 Thread 1989lzhh
Here is the code
m1.py
def f():
print globals()

m2.py
from m1 import f
f()# how to get current module's globals?



-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to use imported function to get current globals

2014-06-07 Thread Ian Kelly
On Sat, Jun 7, 2014 at 11:40 AM, 1989lzhh 1989l...@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.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to use imported function to get current globals

2014-06-07 Thread Chris Angelico
On Sun, Jun 8, 2014 at 3:40 AM, 1989lzhh 1989l...@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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to use imported function to get current globals

2014-06-07 Thread Ned Batchelder

On 6/7/14 1:40 PM, 1989lzhh 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?



Looking at the code you have posted in your two messages so far, it 
seems like you are building something very interesting and ambitious. 
Do you mind talking a bit about what it is?


--
Ned Batchelder, http://nedbatchelder.com

--
https://mail.python.org/mailman/listinfo/python-list


Re: How to use imported function to get current globals

2014-06-07 Thread 1989lzhh


发自我的 iPhone

 在 Jun 8, 2014,4:52,Chris Angelico ros...@gmail.com 写道:
 
 On Sun, Jun 8, 2014 at 3:40 AM, 1989lzhh 1989l...@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.
   Could you give an example ? I do want to get the caller's globals, so I can 
expose something into current module implicitly. Thanks!
  Liu 
zhenhai
 
 Normally, passing dictionaries around is going to be MUCH more useful.
 (And probably not actually globals(), you almost never want to use
 that.)
 
 ChrisA
 -- 
 https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to use imported function to get current globals

2014-06-07 Thread Chris Angelico
On Sun, Jun 8, 2014 at 10:28 AM, 1989lzhh 1989l...@gmail.com wrote:


 发自我的 iPhone

 在 Jun 8, 2014,4:52,Chris Angelico ros...@gmail.com 写道:

 On Sun, Jun 8, 2014 at 3:40 AM, 1989lzhh 1989l...@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.
Could you give an example ? I do want to get the caller's globals, so I 
 can expose something into current module implicitly. Thanks!

Frankly, no. I don't want to encourage implicitly exposing something
like that! Why do you want that, rather than something explicit and
clear?

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to use imported function to get current globals

2014-06-07 Thread 1989lzhh
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 da...@davea.name 写道:
 
 1989lzhh 1989l...@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
-- 
https://mail.python.org/mailman/listinfo/python-list