Python中的assert的作用

Python中的断言,在条件不能达到的时候报错误。

s="我"
word=s.split()
print(word)
assert len(word) >= 2, print([word[0]])


出现错误

"C:\Program Files\Anaconda3\python.exe" C:/Users/User/PycharmProjects/nlpdemo/bilstmcrf/load_sentences_learn.py
['我']
Traceback (most recent call last):
['我']
  File "C:/Users/User/PycharmProjects/nlpdemo/bilstmcrf/load_sentences_learn.py", line 43, in <module>
    assert len(word) >= 2, print([word[0]])
AssertionError: None
正确通过:

s="我 O"
word=s.split()
print(word)
assert len(word) >= 2, print([word[0]])
"C:\Program Files\Anaconda3\python.exe" C:/Users/User/PycharmProjects/nlpdemo/bilstmcrf/load_sentences_learn.py
['我', 'O']

Process finished with exit code 0
详细介绍如下:

学习网址


相关推荐
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页