As we know in Python 3 print()
is a function, is it possible to create a decorated version of it wrapped under json.dumps(indent=4)
for ex.
Aside from BrenBarn's answer(accepted), posting another gist here by @Adam Smith
import builtins
import functools
import json
orig_print = builtins.print
def indent4(f):
@functools.wraps(f)
def wrapped(*args, **kwargs):
return f(json.dumps(*args, **kwargs, indent=4))
return wrapped
@indent4
def print(*args, **kwargs):
return orig_print(*args, **kwargs)