其他
答应我,调试 Python 代码,不要再用 Print 了!
The following article is from 法纳斯特 Author 小F
相信大部分人学习 Python,肯定会用 print() 这个内置函数,来调试代码的。
num2 = 40
print(num1)
print(num2)
40
num2 = 40
print("num1" num1)
print("num2" num1)
num2 40
def plus_five(num):
return num + 5
ic(plus_five(4))
ic(plus_five(5))
ic| plus_five(5): 10
if user:
print("I'm user")
else:
print("I'm not user")
hello(user=True)
def hello(user:bool):
if user:
ic()
else:
ic()
hello(user=True)
from icecream import ic
import time
from datetime import datetime
def time_format():
return f'{datetime.now()}|> '
ic.configureOutput(prefix=time_format)
for _ in range(3):
time.sleep(1)
ic('Hello')
2021-01-24 10:38:24.545628|> 'Hello'
2021-01-24 10:38:25.550777|> 'Hello'
def plus_five(num):
return num + 5
ic.configureOutput(includeContext=True)
ic(plus_five(4))
ic(plus_five(5))
ic| ice_test.py:8 in <module>- plus_five(5): 10
prefix,自定义输出前缀
outputFunction,更改输出函数
argToStringFunction,自定义参数序列化字符串
includeContext,显示文件名、代码行、函数信息
def plus_five(num):
return num + 5
ic.configureOutput(includeContext=True)
ic(plus_five(4))
ic(plus_five(5))
for i in range(10):
print(f'****** Training model {i} ******')
ic| ice_1.py:8 in <module>- plus_five(5): 10
****** Training model 0 ******
****** Training model 1 ******
****** Training model 2 ******
****** Training model 3 ******
****** Training model 4 ******
****** Training model 5 ******
****** Training model 6 ******
****** Training model 7 ******
****** Training model 8 ******
****** Training model 9 ******
总结
到此,你就应该就学会了如何使用icecream去打印调试。更多功能可以访问「GitHub」,了解详情~
https://github.com/gruns/icecream
- EOF -
1、用 VS Code 写 Python,这几个插件是必装的
觉得本文对你有帮助?请分享给更多人
推荐关注「Python开发者」,提升Python技能
点赞和在看就是最大的支持❤️