How do I create user-defined warnings?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Clay Hobbs

    How do I create user-defined warnings?

    I already know how to make user-defined exceptions, like this one:

    class MyException(Exc eption):
    pass

    But for a module I'm making, I would like to make a warning (so it just
    prints the warning to stderr and doesn't crash the program). I have
    tried this:

    class MyWarning(Warni ng):
    pass

    And it behaves like a normal error. Please help me, I can't figure out
    what I'm doing wrong.

    --
    Ratfink

  • Hans Nowak

    #2
    Re: How do I create user-defined warnings?

    Clay Hobbs wrote:
    I already know how to make user-defined exceptions, like this one:
    >
    class MyException(Exc eption):
    pass
    >
    But for a module I'm making, I would like to make a warning (so it just
    prints the warning to stderr and doesn't crash the program). I have
    tried this:
    >
    class MyWarning(Warni ng):
    pass
    >
    And it behaves like a normal error. Please help me, I can't figure out
    what I'm doing wrong.
    Are you using the warning with 'raise'? Don't do that, use warnings.warn instead:

    In [1]: import warnings

    In [2]: class MyWarning(Warni ng): pass
    ...:

    In [3]: warnings.warn(M yWarning("bah humbug"))
    /Library/Frameworks/Python.framewor k/Versions/2.5/lib/python2.5/site-packages/IPython/FakeModule.py:1 :
    MyWarning: bah humbug
    # -*- coding: utf-8 -*-

    --
    Hans Nowak (zephyrfalcon at gmail dot com)

    Comment

    Working...