What is the super call point in custom error classes in python?

So, I have a simple custom error class in Python that I created based on Python 2.7 documentation:

class InvalidTeamError(Exception): def __init__(self, message='This user belongs to a different team'): self.message = message 

This gives me warning W0231: __init__ method from base class %r is not called in PyLint, so I look at it and get a very useful description of the “necessary explanation”. Usually I just ignored this error, but I noticed that the online thin code includes calling super at the beginning of the init method for custom error classes, so my question is: is this really the purpose or are they just trying to appease the fake peeling warning ?

+8
python exception pylint
source share
2 answers

This was a valid pylint warning: if you are not using the __init__ superclass, you can skip implementing the changes to the parent class. And indeed, you have - because BaseException.message deprecated from Python 2.6.

There would be an implementation that avoids the W0231 warning, and also avoids the python erase warning about the message attribute.

 class InvalidTeamError(Exception): def __init__(self, message='This user belongs to a different team'): super(InvalidTeamError, self).__init__(message) 

This is the best way to do this, because the implementation for BaseException.__str__ only considers the "args" tuple, it doesn’t look at the message at all. With your old implementation, print InvalidTeamError() will print only an empty string, which is probably not what you like!

+10
source share

Lookinv in the cpython2.7 source code should not have problems not calling the super init call, and yes, this was done only because it is generally recommended to use the init base class in init.

https://github.com/python/cpython/blob/master/Objects/exceptions.c see line 60 for initializing BaseException and line 456 as an exception comes from BaseException.

-one
source share

All Articles