Warnings in Python (original) (raw)

Last Updated : 23 Jan, 2020

Warnings are provided to warn the developer of situations that aren't necessarily exceptions. Usually, a warning occurs when there is some obsolete of certain programming elements, such as keyword, function or class, etc. A warning in a program is distinct from an error. Python program terminates immediately if an error occurs. Conversely, a warning is not critical. It shows some message, but the program runs. The warn() function defined in the 'warning' module is used to show warning messages. The warning module is actually a subclass of Exception which is a built-in class in Python.

Python3 `

program to display warning a message

import warnings

print('Geeks')

displaying the warning message

warnings.warn('Warning Message: 4')

print('Geeks !')

`

Output:

Geeks main.py:8: UserWarning: Warning Message: 4
warnings.warn('Warning Message: 4') Geeks!

In the above program, the warn() function of the warning module is used to display the message Warning: Message: 4, the UserWarning is the default category of warn() function.

Warning Categories

In Python there are a variety of built-in exceptions which reflect categories of warning, some of them are:

Warning Filters

The warning filter in Python handles warnings (presented, disregarded or raised to exceptions). The warnings filter establishes an organized list of filter parameters, any particular warnings are matched on each filter requirement throughout the list till the match is made, the filter determines the match arrangement. Every entry is indeed a tuple (action, message, category, module, lineno) of the form in which:

Warning Functions

Some of the commonly used functions of the warning module are:

program to illustrate warn()

function in warning module

importing modules

import warnings

displaying warning

warnings.warn('Geeks 4 Geeks')
**Output:** main.py:2: UserWarning: Geeks 4 Geeks warnings.warn('Geeks 4 Geeks') In the above program warnings are displayed using thewarn()` function of warning module.

program to illustrate filterwarnings()

function in warning module

importing module

import warnings

adding entry into the specifications

of the warnings filter.

warnings.filterwarnings('ignore', '.do not.', )

displaying warinings

warnings.warn('Geeks 4 Geeks !')

this warning will not be displayed

warnings.warn('Do not show this message')
**Output:** main.py:8: UserWarning: Geeks 4 Geeks! warnings.warn('Geeks 4 Geeks!') Here the second warning message is not displayed due towarnings.filterwarnings('ignore', '.do not.', )in which action is"ignore"` .

program to illustrate simplefilter()

function in warning module

importing module

import warnings

adding a single entry into warnings filter

warnings.simplefilter('error', UserWarning)

displaying the warning

warnings.warn('This is a warning message')
`

Output:

Traceback (most recent call last): File "main.py", line 8, in
warnings.warn('This is a warning message') UserWarning: This is a warning message

In the above program, a single entry is added to the warnings filter using warnings.simplefilter('error', UserWarning) in which the action is "error" and category is UserWrning and then the warning is displayed using the warn() method.