Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 6th, 2004, 04:26 AM
Tkinter_Bell Tkinter_Bell is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Isle of dogs
Posts: 68 Tkinter_Bell User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
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.

Reply With Quote
  #2  
Old November 6th, 2004, 12:00 PM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to 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
__________________
*** Experimental Python Markup CGI V2 ***

Reply With Quote
  #3  
Old November 6th, 2004, 01:44 PM
Tkinter_Bell Tkinter_Bell is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Isle of dogs
Posts: 68 Tkinter_Bell User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Quote:
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Python eval operation


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT