知行编程网知行编程网  2022-01-31 12:00 知行编程网 隐藏边栏 |   抢沙发  7 
文章评分 0 次,平均分 0.0

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 来提升下生活质量了。如果你觉得有用,请分享、点赞、在看支持。

参考资料

[1]

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

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写

发表评论

表情 格式 链接 私密 签到
扫一扫二维码分享