How do I get warnings.warn to issue a warning and not ignore the line?

后端 未结 3 805
旧巷少年郎
旧巷少年郎 2020-12-16 10:07

I\'m trying to raise a DeprecationWarning, with a code snippet based on the example shown in the docs. http://docs.python.org/2/library/warnings.html#warnings.w

3条回答
  •  长情又很酷
    2020-12-16 10:52

    The warnings module implements filtering of warnings based on certain conditions. You can show the default filters by printing warnings.filters:

    $ python -c "import warnings; print(warnings.filters)"
    [('ignore', None, , None, 0),
     ('ignore', None, , None, 0),
     ('ignore', None, , None, 0),
     ('ignore', None, , None, 0)]
    

    As you can see, DeprecationWarning is ignored by default. You can use the functions in the warnings module and the -W command-line option to Python to configure the filters -- see the documentation for details.

    Example:

    $ python -Wall
    Python 2.7.3 (default, Sep 26 2013, 20:03:06) 
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import warnings
    >>> warnings.warn("test", DeprecationWarning)
    __main__:1: DeprecationWarning: test
    

提交回复
热议问题