assert versus print [was Re: The curious behavior of integer objects]

Steven D'Aprano steve at REMOVEME.cybersource.com.au
Mon Jan 15 22:42:35 EST 2007


On Mon, 15 Jan 2007 21:01:35 -0600, Ron Adam wrote:


> There have been times where I would like assert to be a little more assertive 
> than it is.  :-)
> 
> ie.. not being able to turn them off with the -0/-00 switches, and having them 
> generate a more verbose traceback.

If you want something more verbose, you have it:

>>> assert False, "verbose traceback"
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AssertionError: verbose traceback

If you want something that won't be turned off by -O, you maybe need to
write your own:

def assert_(condition, msg=""):
    if not condition:
        raise AssertionError(msg)

> Maybe have an alternative 'warn' as an alternative debugging version that could 
> be set to ignore, soft (print to log), and hard (raise an error).

Check out the warnings module.


-- 
Steven D'Aprano 




More information about the Python-list mailing list