Skip to main content

Posts

Showing posts from August, 2015

eval,exec, execfile, compile and py_compile in python

Importance of eval, exec, execfile, compile; and the py_compile module: eval(str,globals,locals) This function executes an expression string and returns the result. >>> eval('2+3') 5 >>> eval("'udhay'*3") 'udhayudhayudhay' #exec statement executes a string containing arbitrary python code >>> exec("print 'Hello'") Hello >>> exec('2+34') >>> a=[1,2,3,45] >>> exec "for i in a: print i"                                                                                                                       1 2 3 45 >>> #execfile(filename,globals,locals) -function executes the contents of a file ... >>> execfile("fileName.py") In [1]: globals={'x':7,'y':10,'birds':['parrot','pigeon','sparrow']} In [2]: locals={}