Created
November 24, 2021 16:45
-
-
Save CrimsonScythe/e7b10702e12501a657bd4ba3a0d3539c 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
# passing functions in functions | |
def fun(arg): | |
arg() | |
def fun2(): | |
print('fun2') | |
# decorator function | |
def decorator(fun): | |
def wrapper(*args, **kwargs): | |
nums = args[:-1] | |
for arg in nums: | |
try: | |
int(arg) | |
except ValueError: | |
return {'message': 'Invalid input'} | |
tu=tuple(map(int, nums)) | |
tu = tu + (args[-1],) | |
return fun(*tu, **kwargs) | |
return wrapper | |
@decorator | |
def simpleArith(num1, num2, operator): | |
if operator == '+': | |
return {"result": num1 + num2} | |
elif operator == '-': | |
return {"result": num1 - num2} | |
elif operator == '*': | |
return{"result": num1 * num2} | |
elif operator == '/': | |
return {"result": num1 / num2} | |
@decorator | |
def complexArith(num1, num2, num3, operator): | |
if operator == '+': | |
return {"result": num1 + num2 + num3} | |
elif operator == '-': | |
return {"result": num1 - num2 - num3} | |
elif operator == '*': | |
return {"result": num1 * num2 * num3} | |
elif operator == '/': | |
return {"result": num1 / num2 / num3} | |
print(simpleArith('1', 2, '+')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment