#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Isle of dogs
    Posts
    68
    Rep Power
    11

    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.
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    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.

    grim
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Isle of dogs
    Posts
    68
    Rep Power
    11
    Originally Posted by Grim Archon
    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.

    grim
    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.

    Thanks for you help dude

IMN logo majestic logo threadwatch logo seochat tools logo