print 确实好用,因为它足够简单,简单到不需要思考,简单到任何一个程序员都可以使用任意的编程语言来打印一个 hello world。
直到今天,我还是推荐 debug 时首选 print。
只不过,现在有了冰激凌,打印信息 debug 更甜了。
你说冰激凌不是吃的吗,怎么能 debug 了?
程序员 Ansgar Grunseid 看我们使用 print 即打印变量名称,又打印变量的值,太辛苦啦,就做了一个工具,取名叫 icecream,让打印信息 debug 这种方式更“甜”。
安装:
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 来提升下生活质量了。如果你觉得有用,请分享、点赞、在看支持。
参考资料
icecream: https://github.com/HallerPatrick/icecream
[2]
icecream-rs: https://github.com/ericchang00/icecream-rs
[3]
node-icecream: https://github.com/jmerle/node-icecream
[4]
IceCream-Cpp: https://github.com/renatoGarcia/icecream-cpp
[5]
icecream-php: https://github.com/ntzm/icecream-php
[6]
icecream-go: https://github.com/WAY29/icecream-go
[7]
Ricecream: https://github.com/nodai2hITC/ricecream
[8]
icecream-java: https://github.com/Akshay-Thakare/icecream-java
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ 如何在cmd中运行python文件10/03
- ♥ python是如何实现插入排序的?01/14
- ♥ 自学应该用什么版本的python11/02
- ♥ APScheduler 实现定时任务10/22
- ♥ 如何在python中对随机数求和09/07
- ♥ python的生成器是什么10/09
内容反馈