Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can express "general rules" like this as wrapper functions. In your case you might want to write something along the lines of:

def subs_or_none(expr, vdict):
    try:
        return expr.subs(vdict)
    except ValueError:
        return None

and then you can write

    out_array1[i,j] = subs_or_none(eq_y1.rhs(), vdict)
    ...

Your code is still a bit longer, because you have to specify how to handle the exception (but you really need to do that anyway). Your cost does not go up with the number of times you need the behaviour, however.

Be careful that this code would catch all ValueErrors. You'd need to do some more checking to ensure it's just a division-by-zero. In fact, one could consider it a deficiency that pynac signals a ValueError here and not a ZeroDivisionError.