Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## File 1
- def f1(): print "go f1!"
- class C1(object):
- def do_eval(self,x): # maybe this should be do_evil, given what happens
- print "evaling"
- eval(x)
- eval(x,globals(),locals())
- ## interactive
- class C2(object):
- def do_eval(self,x): # maybe this should be do_evil, given what happens
- print "evaling"
- eval(x)
- eval(x,globals(),locals())
- def f2():
- print "go f2!"
- from file1 import C1
- import file1
- C1().do_eval('file1.f1()')
- C1().do_eval('f1()')
- C1().do_eval('f2()')
- file1.C1().do_eval('file1.f1()')
- file1.C1().do_eval('f1()')
- file1.C1().do_eval('f2()')
- C2().do_eval('f2()')
- C2().do_eval('file1.f1()')
- C2().do_eval('f1()')
- >>> class C1(object):
- >>> def eval(self, x):
- >>> x()
- >>>
- >>> def f2(): print "go f2"
- >>> c = C1()
- >>> c.eval(f2)
- go f2
- ## File 1
- def f1(): print "go f1!"
- class C1(object):
- def do_eval(self, x, e_globals = globals(), e_locals = locals()):
- eval(x, e_globals, e_locals)
- >>> def f2():
- >>> print "go f2!"
- >>> from file1 import * # 1
- >>> C1().do_eval("f2()") # 2
- NameError: name 'f2' is not defined
- >>> C1().do_eval("f2()", globals(), locals()) #3
- go f2!
- >>> C1().do_eval("f1()", globals(), locals()) #4
- go f1!
Add Comment
Please, Sign In to add comment