Decorating Python's builtin print() function

2020-04-06 00:50发布

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.

Calling print(mydict) should produce the same result as print(json.dumps(mydict, indent=4))

4条回答
可以哭但决不认输i
2楼-- · 2020-04-06 01:37

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)
查看更多
够拽才男人
3楼-- · 2020-04-06 01:43

No, it's not possible since print is a builtin function and it's not even a builtin C-level class. This answer provides a way to subclass a builtin object like a str and apply a decorator to one of it's methods.

查看更多
虎瘦雄心在
4楼-- · 2020-04-06 01:44

Alternatively, take a look at pprint. https://docs.python.org/3/library/pprint.html

查看更多
姐就是有狂的资本
5楼-- · 2020-04-06 01:45

You don't need a decorator per se to do that. Just define a new function and call it print:

import builtins

def print(*args, **kwargs):
    builtins.print(json.dumps(*args, **kwargs, indent=4))

You can use the builtins module as shown to access the original print function.

The thing is that doing this doesn't really gain anything over calling your new function something besides print, except it will confuse people.

If you want to really confuse people you could store old_print = builtins.print, define your new function as my_print (accessing the original as old_print) and then do builtins.print = my_print. Then your modified print will actually replace the regular print, even in other modules that know nothing about your shenanigans. But that is an even worse idea.

查看更多
登录 后发表回答