How to catch exceptions elegantly in this situation?

Robert Brewer fumanchu at amor.org
Fri Oct 8 01:26:59 EDT 2004


Saqib Ali wrote:
> - Alternatively I tought of writing a function 'myFunc' and passing in
> the args as follows: (dict=myDict, key="D", expression="a / b / c"). I
> figured within 'myFunc' I would do:  myDict[key] = eval(expression)
> .......... However that wouldn't work either because the eval would
> fail since the variables will be out of context.

1. You might notice that eval() accepts optional globals and locals
arguments, which you can pass to any such 'myFunc' function.

2. If myFunc is defined within the same module, you will notice that it
has access to a, b, and c, as long as you don't rebind those names
within myFunc.

>>> a,b,c = 1,2,3
>>> def myFunc():
... 	print a * b * c
... 	
>>> myFunc()
6



Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list