'Is there an advantage to using the warnings module in Python rather than printing a warning, when the warning is just for reference?

Here are two examples of warning the user if the variable x is not equal to 'apple':

First the set up:

import warnings

x = 'banana'

First example, just using print:

if x != 'apple':

   print('Warning: X is not apple!')

returns:

Warning: X is not apple!

The second example uses the warnings module:

if x != 'apple':

   warnings.warn('X is not apple!',UserWarning)

returns:

C:\Users\XXX\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: UserWarning: X is not apple!
  This is separate from the ipykernel package so we can avoid doing imports until

If all I want to do is produce output either on the Jupyter Notebook or in the command window where the script is being run, is there any advantage to using the warnings module to do so, or is the print function sufficient? I want to achieve a user warning in the most pythonic manner i.e. what is best practice here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source