Is there a function python that performs all types of math functions on two variables?
CodePudding user response:
Here's one that handles addition, subtraction, multiplication, division, exponents and modulo:
def all_operations(x, y, z):
if x y == z:
return True
if x - y == z:
return True
if x * y == z:
return True
if x / y == z:
return True
if x % y == z:
return True
return False
CodePudding user response:
Something like this maybe?
x, y, z = 6, 7, 42
for f in dir(x):
try:
if getattr(x, f)(y) == z:
print(f)
except:
pass
That example finds two operations:
__mul__
__rmul__