Skip to content

Instantly share code, notes, and snippets.

@CrimsonScythe
Created November 24, 2021 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CrimsonScythe/e7b10702e12501a657bd4ba3a0d3539c to your computer and use it in GitHub Desktop.
Save CrimsonScythe/e7b10702e12501a657bd4ba3a0d3539c to your computer and use it in GitHub Desktop.
# 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