[Tutor] eval and exec

Bill Campbell bill at celestial.net
Sat Dec 4 20:46:08 CET 2004


On Sat, Dec 04, 2004, Alan Gauld wrote:
>> I'm having trouble understanding the difference between eval and
>exec.
>
>eval evaluates an *expression* - that is something that returns a
>value.
>
...
>Both are extremely dangerous functions from a security
>and maintenance/reliability pouint of view and should be
>used very rarely.

True enough, but useful upon occassion.  In particular I've had a
question on the back burner for a while.  Suppose I have a
dictionary of database instances, dbtables, keyed on table name,
and I want a general way of creating variables with the name of
the table so I'm not accessing the dictionary.  Would something
like this work:

# dbtables is already built
for table in dbtables.keys():
	exec("%s = dbtables['%s']" % (table, table))

Bill
--
INTERNET:   bill at Celestial.COM  Bill Campbell; Celestial Software LLC
UUCP:               camco!bill  PO Box 820; 6641 E. Mercer Way
FAX:            (206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676
URL: http://www.celestial.com/

My brother sent me a postcard the other day with this big satellite photo
of the entire earth on it. On the back it said: ``Wish you were here''.
		-- Steven Wright


More information about the Tutor mailing list