Skip to content

Instantly share code, notes, and snippets.

@PrudhviVajja
Created November 10, 2020 08:56
Show Gist options
  • Save PrudhviVajja/80b0849b9f29865cad987797256298bd to your computer and use it in GitHub Desktop.
Save PrudhviVajja/80b0849b9f29865cad987797256298bd to your computer and use it in GitHub Desktop.
python-decorators
def func_name_printer(func):
def wrapper(*args):
print("Function that started running is " + func.__name__)
func(*args)
return wrapper
@func_name_printer
def add(*args):
tot_sum = 0
for arg in args:
tot_sum += arg
print("result = " + str(tot_sum))
@func_name_printer
def sub(*args):
tot_sub = args[0]-args[1]
print("result = " + str(tot_sub))
@func_name_printer
def mul(*args):
tot_mul = 1
for arg in args:
tot_mul *= arg
print("result = " + str(tot_mul))
add(1,2)
mul(1,2,3)
sub(400, 150)
# outputs:
# Function that started running is add
# result = 3
# Function that started running is mul
# result = 6
# Function that started running is sub
# result = 250
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment