November 6th, 2004, 04:26 AM
Python eval operation
Does anyone know how to use the eval keyword beyond the basics. there are two other peramiters but I dont know what they do or how to use them.
November 6th, 2004, 12:00 PM
Well the docs explain it very well - I suppose if you know what a namespace is.
Global and local dictionaries can be supplied to become the namespace that the eval expression will be executed in. The local namespace is for example the definitions available within the function that is using the eval function. Global corresponds to the enclosing function, class, module and the rest.
If they are omitted then the expression has complete access to any and all variables, functions, classes and module imports that were existing in the namespace before the code is evaluated.
If you specify the global and local dictionaries then you have the ability to redefine the operating environment for the expression - possibly restricting it or changing it completely.
As to why you would do it - security is one reason, not allowing an arbitrary string obtained from wherever to cause havoc when it is evaluated.
You can use the globals() built in function, (there is also a locals() function) - to return a dictionary suitable for modifying.
November 6th, 2004, 01:44 PM
If secuirty is the only reason for those extre paramiters, I dont think I would need it. I wanted to implement control tables into my application and use eval to store functions.
Originally Posted by Grim Archon
Thanks for you help dude