I'm new to the ideas of decorators (and still trying to wrap my head around them), but I think I've come across a problem that would be well suited for them. I'd like to have class that is decorated across all of the functions in the math library. More specifically my class has two members, x and flag. When flag is true, I'd like the original math function to be called. When flag is false, I'd like to return None.
As a framework for what I'm asking here is the class:
import math
class num(object):
def __init__(self, x, flag):
self.x = x
self.flag = flag
def __float__(self):
return float(self.x)
As a result, this works fine:
a = num(3, True)
print math.sqrt(a)
However this should (in my perfect world), return None:
b = num(4, False)
print math.sqrt(b)
Any suggestions or tips on how this would be possible to apply over a whole library of functions?