Create a decorator in Python, which remembers last output of the function
Question:
I need to create a decorator which remembers last call result of the function which it decorates. For example, I have a function:
def summarize(*args):
result = ""
for i in args:
result += i
print('New result = ', result)
return result
And I need to use decorator (without any classes or smth), to make it like that:
summarize("he", "llo")
>>> "Previous result = 'None'"
>>> "New result = 'hello'"
summarize("good", "bye")
>>> "Previous result = 'hello'"
>>> "New result = 'goodbye'"
I’m new to decorators and now I don’t have any idea how to make it like that. I get needed result only when I try to call the function twice in one run.
Answers:
See below :
def historic_results(func):
# retrive historic outputs from hitoric_outputs.txt file if it exists
with open('historic_outputs.txt', "r") as f:
try:
previous_outputs = f.readlines()
except:
previous_outputs = []
def inner(*args, **kwargs):
if not previous_outputs : print("Previous Result:", None)
if previous_outputs:
for item in previous_outputs: print("Previous Result:", item)
# append the output in the historic outputs txt file
with open('historic_outputs.txt', 'a') as f:
f.write(func(*args, **kwargs) + "n")
print("New Result", func(*args, **kwargs))
return func(*args, **kwargs)
return inner
@historic_results
def summarize(*args):
result = ""
for i in args:
result += i
return result
Test :
summarize("hel", "lo")
summarize("Ami", "ne")
summarize("stupid", "_beaver")
Output:
Previous Result: None
New Result hello
Previous Result: hello
New Result Amine
Previous Result: hello
Previous Result: Amine
New Result stupid_beaver
prev_result = None
def remember_result(func):
def wrapper(*args):
global prev_result
print(f"Prev result = '{prev_result}'")
prev_result = func(*args)
return wrapper
@remember_result
def summarize(*args):
result = ""
for i in args:
result += i
print('New result = ', result)
return result
I need to create a decorator which remembers last call result of the function which it decorates. For example, I have a function:
def summarize(*args):
result = ""
for i in args:
result += i
print('New result = ', result)
return result
And I need to use decorator (without any classes or smth), to make it like that:
summarize("he", "llo")
>>> "Previous result = 'None'"
>>> "New result = 'hello'"
summarize("good", "bye")
>>> "Previous result = 'hello'"
>>> "New result = 'goodbye'"
I’m new to decorators and now I don’t have any idea how to make it like that. I get needed result only when I try to call the function twice in one run.
See below :
def historic_results(func):
# retrive historic outputs from hitoric_outputs.txt file if it exists
with open('historic_outputs.txt', "r") as f:
try:
previous_outputs = f.readlines()
except:
previous_outputs = []
def inner(*args, **kwargs):
if not previous_outputs : print("Previous Result:", None)
if previous_outputs:
for item in previous_outputs: print("Previous Result:", item)
# append the output in the historic outputs txt file
with open('historic_outputs.txt', 'a') as f:
f.write(func(*args, **kwargs) + "n")
print("New Result", func(*args, **kwargs))
return func(*args, **kwargs)
return inner
@historic_results
def summarize(*args):
result = ""
for i in args:
result += i
return result
Test :
summarize("hel", "lo")
summarize("Ami", "ne")
summarize("stupid", "_beaver")
Output:
Previous Result: None
New Result hello
Previous Result: hello
New Result Amine
Previous Result: hello
Previous Result: Amine
New Result stupid_beaver
prev_result = None
def remember_result(func):
def wrapper(*args):
global prev_result
print(f"Prev result = '{prev_result}'")
prev_result = func(*args)
return wrapper
@remember_result
def summarize(*args):
result = ""
for i in args:
result += i
print('New result = ', result)
return result