[Tutor] try and except

Kent Johnson kent37 at tds.net
Wed Dec 30 16:02:52 CET 2009


On Wed, Dec 30, 2009 at 8:39 AM, spir <denis.spir at free.fr> wrote:
> Lie Ryan dixit:
>
>> class Error(Exception):
>>      def __init__(self, value):
>>          self.value = value
>>      def printer(self, value):
>>          print self.value
>
> You can also use __str__ instead of printer. This will give a standard output form for your error automatically used by print and also, for exceptions, when python writes it to stderr: you don't need to catch the error to write it yourself.
>
>     def __str_(self, value):
>         print self.value

Should be
  def __str__(self):
    return self.value

i.e. __str__() takes just one argument (self) and returns (rather than
prints) a string.

Kent


More information about the Tutor mailing list