I need to create decorator for a function so that if a particular function is called twice in a row with the same parameters, it will not run, and return None instead.
The function being decorated can have any number of parameters, but no keyword arguments.
For example:
@dont_run_twice
def myPrint(*args):
print(*args)
myPrint("Hello")
myPrint("Hello") #won't do anything (only return None)
myPrint("Hello") #still does nothing.
myPrint("Goodbye") #will work
myPrint("Hello") #will work
See if this simple approach works for you.
prev_arg = ()
def dont_run_twice(myPrint):
def wrapper(*args):
global prev_arg
if (args) == prev_arg:
return None
else:
prev_arg = (args)
return myPrint(*args)
return wrapper
@dont_run_twice
def myPrint(*args):
print(*args)