[Python-ideas] adding a Debug exception?

Steven D'Aprano steve at pearwood.info
Tue Feb 28 07:39:50 CET 2012

On Mon, Feb 27, 2012 at 09:34:02PM -0700, Mark Janssen wrote:
> Had an idea on another thread (doctest) about a special exception called
> "Debug" that could could be raised to generate arbitrary output to stderr.
>  This would be used instead of spurious print statements in code to inform
> developers during debugging (which might throw off doctest, for example).

Printing to sys.stderr does not throw off doctest. If you use 

    print(something, file=sys.stderr)   # Python 3
    print >>sys.stderr, something  # Python 2

the output is invisible to doctest.

>  It could also replace "assert" (and improve upon it) which seems to be
> deprecated.

What makes you think assert is deprecated?

Informational messages printed to stderr and assertions are completely 
different functions. You can't replace one with the other.

> Also, the __debug__ global could actually gain some
> functionality...

What makes you think it doesn't?

__debug__ is very useful for conditional compilation of debugging code 
that is safe to optimise away when running under -O. I use it in most of 
my projects.

> Its "argument" could be an `eval`uatable string (checked at compile time)
> and it's output, this very string *plus* the output of it (if it evaluates
> to something different than itself).

So you mean, anything except a quine would be printed?

I don't get what you mean, or how you intend for this to be used. 


More information about the Python-ideas mailing list