Replies: 3 comments
|
@Griffe27 post real code that shows a problem. This works for me: >>> from asteval import Interpreter
>>> aeval = Interpreter()
>>> aeval("""def myfunc(a, b, c=10):
... if c != 0:
... return (a+b)/c
... else:
... return 0
... """)
>>> aeval("print(myfunc(2,12,c=1))")
14.0
>>> aeval("print(myfunc(2,12,c=0))")
0 |
|
Thanks for your answer @newville , sorry I was far from precise! I fully understand that parameters should be evaluated before calling the function, but is there a way for example to catch error when parameter is not used or something like that? If possible I would like to avoid modifying formulas as there are many already existing. If not possible one way would be to create a specific function for "division" case and passing numerator and denominator separatly. Thank you |
|
In the evaluation of That is the correct behavior, matching Python: >>> def ifeq(a,b,t,f):
... return t if a==b else f
...
>>> x = 5
>>> ifeq(x, 0, 100, 10/x)
2.0
>>> x = 0
>>> ifeq(x, 0, 100, 10/x)
Traceback (most recent call last):
File "<python-input-5>", line 1, in <module>
ifeq(x, 0, 100, 10/x)
~~^~
ZeroDivisionError: division by zero
How would one know what 'not used' means when preparing to call the function? But also: There is no problem with the function definition or call. Any problem is completely contained in having |
Uh oh!
There was an error while loading. Please reload this page.
Hi,
Thanks for your package!
I've a IFEQ custom function that takes 3 parameters :
value, value_true, value_false
If value==0 then returns value_true else returns value_false.
It is used to avoid division by zero : it returns 0 if denominator is zero, otherwise the division.
Problem is that value_false is evaluated even if condition is true.
Is there a way to manage that?
Thanks!
All reactions