How to catch exceptions elegantly in this situation?

Paul Rubin http
Fri Oct 8 01:39:45 EDT 2004


syed_saqib_ali at yahoo.com (Saqib Ali) writes:
> - 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.
> 
> Any Suggestions??

exprs = [("A", "a+b+c"), ("B", "a-b-c"), ("C", "a/b/c"), etc.]
for a,e in expr:
  try:
    myDict[a] = eval(e)
  except ArithmeticError:
    # whatever



More information about the Python-list mailing list