exec src in {}, {} strangeness

Brano Zarnovican zarnovican at pobox.sk
Thu Mar 24 09:54:29 EST 2005


As Greg pointed..

g = {}
exec open('t.py').read() in g, g

is what you want.

But you can write it also this way:
exec open('t.py').read() in {}

because if you specify only globals, the same
dictionary is also used for locals. (locals() is
used as a default only if you don't specify globals)

OR

explicitly move class Foo to globals

t.py contains:
globals Foo
class Foo: pass
class Bar:
     f = Foo 

(Should work. I haven't tried it, though)

BranoZ




More information about the Python-list mailing list