eval - Run Python script saved as variable -


is possible run python script has been saved variable?

x = 'print ("hello, world!")' ??? run(x) ??? 

i able run script without having run:

python -c 'print ("hello, world!")' 

any appreciated.

i believe exec statement you're looking for.

>>> cmd = 'print "hello, world!"' >>> exec cmd hello, world! >>> >>> cmd = """ ... x = 4 ... y = 3 ... z = x*y + 1 ... print 'z = {0}'.format(z) ... """ >>> >>> exec cmd z = 13 

please take appropriate cautions if including user input in string going exec. input malicious statements executed.

see also:


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -