求求你,别用 print 来 debug 了

之前看过这个标题的文章,看完后,我觉得太花里胡哨,用起来麻烦,我就用 print 来 debug,没毛病。

print 确实好用,因为它足够简单,简单到不需要思考,简单到任何一个程序员都可以使用任意的编程语言来打印一个 hello world。

直到今天,我还是推荐 debug 时首选 print。

只不过,现在有了冰激凌,打印信息 debug 更甜了。

你说冰激凌不是吃的吗,怎么能 debug 了?

程序员 Ansgar Grunseid 看我们使用 print 即打印变量名称,又打印变量的值,太辛苦啦,就做了一个工具,取名叫 icecream,让打印信息 debug 这种方式更“甜”。

安装:

pip install icecream

使用

from icecream import ic

下面把 icecream 简称 ic。

打印变量时 ic 的用法和 print 的用法基本一样,但比 print 更好用:

ic 可以自动打印变量或表达式的名称。

ic 打印的结果更漂亮的,包含语法高亮。

ic 可以输出程序的上下文:文件名,行号,父函数名等信息。

ic 可以一次导入处处使用。

ic 可以配置前缀。

重要的是,它让你编写 print 语句的时间减少了 40%。

只要 print 不输出到文件,不传入 end 参数,你可以在任何使用到 print 的地方替换为 ic,反之亦然。

你仍然可以不用 ic,但是如果经常用 print 来 debug,我就建议你用,为什么?

因为 print 是你的高频要素,改善“高频要素”会从根本上改善生命体的生活质量。

ic 使用举例:

检查变量

#ice_demo.py
from icecream import ic 

name = "Python七号"
ic(name)

def foo(i):
    return i + 333
ic(foo(123))

d = {'key': {1'one'}}
ic(d['key'][1])

class klass():
    attr = 'yep'
ic(klass.attr)

ic 的输出很漂亮,自动打印了变量或表达式的名称,真的香,如下:

图片icecream

检查执行位置

不带任何参数也是可以的:

from icecream import ic

expression = False

def foo():
    ic()
    #first()

    if expression:
        ic()
        #second()
    else:
        ic()
        #third()
foo()

ic 的输出如下:

ic| demo.py:6 in foo() at 02:49:29.467
ic| demo.py:13 in foo() at 02:49:29.469

可以看出,ic() 还自动输出了时间戳,因此看程序执行到哪里,直接调用 ic() 就完事。

ic 有返回值

ic 的返回值就是它参数的值:

In [1]: from icecream import ic

In [2]: a = 6

In [3]: b = ic(a)
ic| a: 6

In [4]: ic(b)
ic| b: 6
Out[4]: 6

如果你要返回 ic() 的输出,可以这样:

In [5]: out = ic.format(a)

In [6]: out
Out[6]: 'ic| a: 6'

一次导入处处使用

一个工程,会有很多 py 文件,不想在每个文件内部都 import,可以这样:

main.py 只导入一次

from icecream import install
install()
from xxx import foo
foo()

其他被 main 引用的文件 xxx.py 不需要再 import:

def foo():
    x = 3
    ic(x)

执行 main.py 就会看到变量 x 的信息输出。

这是因为,install() 函数把 ic() 添加到了内建模块, 所有被编译器打开的文件都会共享这个 ic,当然,可以通过 uninstall() 来卸载 ic。

灵活的前缀配置

In [1]: from icecream import ic

In [2]: ic.configureOutput(prefix='hello -> ')

In [3]: ic('world')
hello -> 'world'
Out[3]: 'world'

In [4]: import time

In [5]: def Timestamp():
   ...:     return '%s |> ' % time.strftime("%Y-%m-%d %T")
   ...:

In [6]: ic.configureOutput(prefix=Timestamp)

In [7]: ic('world')
2021-04-01 11:02:20 |> 'world'
Out[7]: 'world'

configureOutput 还可以传入 outputFunction、argToStringFunction、includeContext 参数,这里你可以自己尝试。

最后的话

请注意 icecream 是个通用的工具,不只是 Python 能用,以下编程语言也可以用:

  • Dart: icecream[1]
  • Rust: icecream-rs[2]
  • Node.js: node-icecream[3]
  • C++: IceCream-Cpp[4]
  • PHP: icecream-php[5]
  • Go: icecream-go[6]
  • Ruby: Ricecream[7]
  • Java: icecream-java[8]

因此,很容易迁移到其他语言,如果你经常用 print 来 debug,现在是时候用 icecream 来提升下生活质量了。如果你觉得有用,请分享、点赞、在看支持。


猜你喜欢

转载自blog.51cto.com/15127523/2684571