ANN: pkalk, and problems with "exec foo in g,v"
holger krekel
pyth at devel.trillke.net
Tue Dec 24 15:46:51 EST 2002
Gerson Kurz wrote:
> Ah, Christmas is over, Pythontime again!
>
> Announcing pkalk: a (wx)Python powered kalkulator, that evaluates
> expressions as you type them. Great for approximating results and
> stuff. Freeware and all that:
>
> http://p-nand-q.com/e/pkalk.html
>
> Sorry, no linux version yet, I'm stuck with my 2k box this week.
>
> I have this problem though:
>
> ActivePython 2.2.2 Build 224 (ActiveState Corp.) based on
> Python 2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)] on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> from math import *
> >>> a = lambda x:sin(x)
> >>> print a(2)
> 0.909297426826
> >>> g,v={}, {}
> >>> exec "from math import *" in g, v
This imports names into the *local* namespace (v).
"from math import sin" would be enough btw :-)
> >>> exec "a = lambda x:sin(x)" in g, v
It appears that inside lambda 'sin' is assumed to be global.
> >>> exec "sin(2)" in g, v
This works because 'sin' is looked up in the local
namespace.
> >>> exec "a(2)" in g, v
> Traceback (most recent call last):
This doesn't work because inside lambda 'sin' is
looked up with globals where it doesn't exist.
> So, my problem is this: I'm using "exec foo in g,v" for statement
> evaluation. I can import math, and conveniently use the math stuff
> (its a calculator, after all). I cannot, however, define a local
> lambda that uses the "global" scope in g,v. Is that a bug or a
> feature?
'sin' *is* lookuped in the global namespace.
Maybe it's sufficient for your purposes to do
exec "..." in g,g
and don't care about the global/local distinction.
do-the-simplest-thing-that-can-possibly-work'ly y'rs holger
More information about the Python-list
mailing list