I want to define function that can be used globally
I made this decorator.
def toprint(func):
def wrapper(*args, **kwargs):
global print
temp = print
def print(*args, end="\n", sep=" "):
args = [str(i) for i in args]
target = sep.join(args)
with open("output", "a") as file:
file.write(target + end)
func(*args, **kwargs)
print = temp
return wrapper
If I have this function defined in one file and use it in same file it would work perfectly, but if i use this on function from imported module if would print in console not write it in file like it should
Example #1
... toprint function ...
@toprint
def test():
print("test")
It will output in file, but
Example #2
# test.py
def test():
print("test")
# main.py
... toprint function ...
from test import test
toprint(test)()
It will print in console not in file