[IronPython] Evaluating python from within the context of a class

Michael Foord fuzzyman at voidspace.org.uk
Fri Jun 27 18:46:01 CEST 2008


Michael Stephens wrote:
> I want to be able to execute arbitrary expressions but have their 
> context be within my class.  The end result is that RT test should be 
> printed and not global test
>
> def test():
>  print "global test"
>
> class RT:
>  def test(_self_):
>   print 'RT test'
>  def Evaluate(_self_,code):
>   eval(code)
>

exec code in self.__dict__

Might work.

Michael

>
> test()
> a.test()
> a=RT()
> a.Evaluate('test()')
>
> Michael Stephens
>
> Electrical Engineering Graduate Student
> University of Wyoming
> falcon at uwyo.edu <mailto:falcon at uwyo.edu> or 89iroc at gmail.com 
> <mailto:89iroc at gmail.com>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>   


-- 
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/
http://www.trypython.org/
http://www.ironpython.info/
http://www.theotherdelia.co.uk/
http://www.resolverhacks.net/




More information about the Ironpython-users mailing list