Created
November 7, 2017 21:49
-
-
Save djm4686/7baa9e5767bb4781f3eb519c19c0a5d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Constant: | |
def __init__(self, value): | |
self.value = value | |
def __call__(self, *args, **kwargs): | |
return self.value | |
class Function: | |
def __init__(self, coefficient, exponent, sub_function=None, side_function=None): | |
self.coefficient = coefficient | |
self.exponent = exponent | |
self.sub_function = sub_function | |
self.side_function = side_function | |
if sub_function is None: | |
self.sub_function = Reflector() | |
if self.side_function is None: | |
self.side_function = Zero() | |
def __call__(self, *args, **kwargs): | |
return self.coefficient * \ | |
(self.sub_function(param=kwargs["param"]) ** self.exponent) + \ | |
self.side_function(param=kwargs["param"]) | |
class Reflector(Function): | |
def __init__(self): | |
Function.__init__(self, 1, 1, self, Zero()) | |
def __call__(self, *args, **kwargs): | |
return kwargs["param"] | |
class Zero(Constant): | |
def __init__(self): | |
Constant.__init__(self, 0) | |
def __call__(self, *args, **kwargs): | |
return 0 | |
if __name__ == "__main__": | |
f = Function(2, 2) | |
print f(param=1) | |
print f(param=2) | |
print f(param=3) | |
print f(param=4) | |
print f(param=5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment