Created
May 13, 2020 09:13
-
-
Save StephenFordham/07550fdd967bc049aa8ec659c36f4633 to your computer and use it in GitHub Desktop.
method_decorators_example2
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
def integer_check(method): | |
def inner(ref, expo=None): | |
if expo == None: | |
if not isinstance(ref._val1, int) or not isinstance(ref._val2, int): | |
raise TypeError('Please enter numerical numbers for val1 and val2') | |
else: | |
return method(ref) | |
if expo: | |
if not isinstance(ref._val1, int) or not isinstance(ref._val2, int)\ | |
or not isinstance(expo, int): | |
raise TypeError('Please enter numerical numbers for val1, val2 and expo') | |
else: | |
return method(ref, expo) | |
return inner | |
class NumericalOps(object): | |
def __init__(self, val1, val2): | |
self._val1 = val1 | |
self._val2 = val2 | |
@integer_check | |
def multiply_together(self): | |
return self._val1 * self._val2 | |
@integer_check | |
def power(self, exponent): | |
return self.multiply_together() ** exponent | |
y = NumericalOps(1, 2) | |
print(y.multiply_together()) | |
print(y.power(3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment