Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!peer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!news.glorb.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: <1989lzhh@gmail.com> X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python.': 0.02; 'static': 0.04; 'modify': 0.07; 'guys.': 0.09; 'try:': 0.09; 'subject:How': 0.10; 'translate': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; 'crude': 0.16; 'debugger,': 0.16; "module's": 0.16; 'namespace.': 0.16; 'script?': 0.16; 'language': 0.16; 'module': 0.19; 'trying': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'load': 0.23; 'cc:2**0': 0.24; 'compiled': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'said,': 0.30; 'skip:@ 10': 0.30; 'url:mailman': 0.30; 'code': 0.31; 'probably': 0.32; 'quite': 0.32; 'url:python': 0.33; 'could': 0.34; 'connection': 0.35; 'tool': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'url:listinfo': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'so,': 0.37; 'two': 0.37; 'message-id:@gmail.com': 0.38; 'track': 0.38; 'bad': 0.39; 'url:mail': 0.40; 'how': 0.40; 'simple': 0.61; 'header:Message- Id:1': 0.63; 'iphone': 0.65; 'here': 0.66; 'charset:gb2312': 0.80; 'subject:get': 0.81; '8bit%:24': 0.84; 'expose': 0.84; 'snapshot': 0.84; 'angel': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=references:mime-version:in-reply-to:content-type :content-transfer-encoding:message-id:cc:from:subject:date:to; bh=sPmYDWlUElbgXAC3yTwASgyC0IEZ3AqECXWEpwnqpyE=; b=aqYwVswMmIGxubeVAF8wSR9P8KyXKTk6RysBaVQDFJv80Ytkd2FUjTHS+srWG/rzCS Al8mWS0fncsVQSmSSh7wxIFJ4pdEvd9OM/mtjxt8k9bDzIaL9DvHOLZX2QOYAyxLEYcf o9x30Rbus7j+Z0mlK4ycES8wiKadFlTnic0vrnnIZoZ/8q64B/YHUWmmYuBhjUizauP4 nehSIcb3Gp/iAbRnxvXMsk+IXumsEqJc+cWryKnHd3xDE6BV3kYjHHdvsHB4tfguwxd7 MeGg2kX+DPOnd59T9igoc/L9/hySF599FoWpvnemA/jKvyRYf/SWF1Nht5SZXmWiefDP vWdA== X-Received: by 10.42.81.72 with SMTP id y8mr18344973ick.71.1402206720908; Sat, 07 Jun 2014 22:52:00 -0700 (PDT) References: <602B90F0-E7FE-4887-ADA1-981FD4179EC7@gmail.com> Mime-Version: 1.0 (1.0) In-Reply-To: Content-Type: text/plain; charset=gb2312 Content-Transfer-Encoding: quoted-printable X-Mailer: iPhone Mail (11D201) From: 1989lzhh <1989lzhh@gmail.com> Subject: Re: How to use imported function to get current globals Date: Sun, 8 Jun 2014 13:51:53 +0800 To: Dave Angel Cc: "python-list@python.org" 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: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1402206730 news.xs4all.nl 2874 [2001:888:2000:d::a6]:44136 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 5225 X-Received-Body-CRC: 1786145971 Xref: csiph.com comp.lang.python:72955 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 us= ing decorate. jit, build=3Dmake("mymodule") #jit function collect python code and signature then translate to cython cod= e @jit('int(int)', locals=3D''' int b; ''') def f(a): b=3D1 return a+1 build()# compile cython code and load compiled module then expose compiled f= unction to current namespace. So this is my purpose to get caller's scope f()# now f is a compiled function=20 =20 =B7=A2=D7=D4=CE=D2=B5=C4 iPhone > =D4=DA Jun 8, 2014=A3=AC10:24=A3=ACDave Angel =D0=B4=B5= =C0=A3=BA >=20 > 1989lzhh <1989lzhh@gmail.com> Wrote in message: >> Here is the code >> m1.py >> def f(): >> print globals() >>=20 >> m2.py >> from m1 import f >> f()# how to get current module's globals? >=20 > As others have said, it's probably a bad idea. I can think of 3 > reasons to try: teacher said so, writing a debugger,=20 > transliterating code from a crude language into python. >=20 > 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?=20 >=20 >=20 >=20 >=20 > --=20 > DaveA >=20 > --=20 > https://mail.python.org/mailman/listinfo/python-list