【发布时间】:2020-12-08 16:28:24
【问题描述】:
我正在尝试设计一个打印功能dprint 以供调试使用。具体来说,这就是我想要实现的目标:
def func1(a,b,debug=0):
if debug:
print(Something) # print something if debug mode
下面是我如何设计dprint
def func2(a,b,debug=0):
def dprint(*args):
if debug:
print(*args) # using the local variable `debug` to detect whether is under debug mood
dprint(Something) # print something if debug mode
func2 可以工作,但不是很好,因为我需要将dprint 函数插入到其他所有函数中。不知道有没有更体面的方法来实现这个dprint功能。
【问题讨论】:
-
为什么不用python日志模块?
-
我无法理解您的代码...为什么是
func2、func1和dprint?是的,你为什么不使用logging的内置功能?即logger.debug
标签: python