mirror of
https://github.com/apachecn/ailearning.git
synced 2026-04-23 18:13:05 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -6,20 +6,20 @@
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
import warnings
|
||||
|
||||
```
|
||||
|
||||
在需要的地方,我们使用 `warnings` 中的 `warn` 函数:
|
||||
|
||||
```
|
||||
```py
|
||||
warn(msg, WarningType = UserWarning)
|
||||
```
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
def month_warning(m):
|
||||
if not 1<= m <= 12:
|
||||
msg = "month (%d) is not between 1 and 12" % m
|
||||
@@ -29,14 +29,14 @@ month_warning(13)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
c:\Anaconda\lib\site-packages\IPython\kernel\__main__.py:4: RuntimeWarning: month (13) is not between 1 and 12
|
||||
|
||||
```
|
||||
|
||||
有时候我们想要忽略特定类型的警告,可以使用 `warnings` 的 `filterwarnings` 函数:
|
||||
|
||||
```
|
||||
```py
|
||||
filterwarnings(action, category)
|
||||
```
|
||||
|
||||
@@ -44,7 +44,7 @@ filterwarnings(action, category)
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
warnings.filterwarnings(action = 'ignore', category = RuntimeWarning)
|
||||
|
||||
month_warning(13)
|
||||
|
||||
Reference in New Issue
Block a user