Re: question about exec() and compile()

lance@markv.com
Mon, 13 Sep 93 10:29:22 PDT

I figured out a hack to allow me to do what I want....
I just have to add 1 line of code as shown below..

> I have a file that looks like this:
> t = 1
>
> def Test():
> print 't = ' + `t`
>
> def test2():
> print 'calling Test()'
> Test()
>
>
> I read in the file and want to execute test2().
>
> So I do the following:
>
> f = open('file','r')
> a = f.read()
> f.close()
> a_c = compile(a,'<string>','exec')
> local_ns = {}
> global_ns = {}
> exec(a_c,global_ns,local_ns)

exec(a_c,local_ns,local_ns)

now I can say:
local_ns['test2']()

and the result will be

calling Test()
t = 1

This is what I wanted to be able to do..
Sorry for wasting net-bandwidth on this..

--

Lance Ellinghouse lance@markv.com

1231 bit key fingerprint = 56 DA 31 0C 17 51 36 6A 4E D4 E0 11 D9 B8 06 0A 1024 bit key fingerprint = 66 2C 75 F2 E9 1C 32 84 3A E3 B0 5E 48 01 4C 37 You can receive my Public Key by `finger lance@mark.com`