Replacement for e.message() in python 2.6

Arnaud Delobelle arnodel at googlemail.com
Wed Feb 17 05:18:52 EST 2010


Nandakumar Chandrasekhar <navanitachora at gmail.com> writes:

> Dear Folks,
>
> In previous versions of Python I used to use e.message() to print out
> the error message of an exception like so:
>
> try:
> 	result = x / y
> except ZeroDivisionError, e:
> 	print e.message()
>
> Unfortunately in Python 2.6 the message method is deprecated.
>
> Is there any replacement for the message method in Python 2.6 or is
> there any best practice that should be used in Python from now on?

You can just use the __str__() method of the BaseException object for
this.  So instead of

    print e.message

You can write

    print str(e)

which in turn is equivalent to

    print e

For more details see PEP 352 (http://www.python.org/dev/peps/pep-0352/)

-- 
Arnaud



More information about the Python-list mailing list