问题
I have created custom exception in python 3 and the over all code works just fine. But there is one thing I am not able to wrap my head around is that why do I need to send my message to the Exception class's __init__() and how does it convert the Custom exception into that string message when I try to print the exception since the code in the Exception or even the BaseException does not do much.
Not quite able to understand why call the super().__init__() from custom exception?
回答1:
This is so that your custom exceptions can start off with the same instance attributes as a BaseException
object does, including the value
attribute, which stores the exception message, which is needed by certain other methods such as __str__
, which allows the exception object to be converted to a string directly. You can skip calling super().__init__
in your subclass's __init__
and instead initialize all the necessary attributes on your own if you want, but then you would not be taking advantage of one of the key benefits of class inheritance. Always call super().__init__
unless you have very specific reasons not to reuse any of the parent class's instance attributes.
回答2:
Beazley, David. Python Cookbook (p. 578). O'Reilly Media.
If you are going to define a new exception that overrides the init() method of Exception, make sure you always call Exception.init() with all of the passed arguments. For example: class CustomError(Exception):
def __init__(self, message, status):
super().__init__(message, status)
self.message = message
self.status = status
This might look a little weird, but the default behavior of Exception is to accept all arguments passed and to store them in the .args attribute as a tuple. Various other libraries and parts of Python expect all exceptions to have the .args attribute, so if you skip this step, you might find that your new exception doesn’t behave quite right in certain contexts. To illustrate the use of .args, consider this interactive session with the built-in RuntimeError exception, and notice how any number of arguments can be used with the raise statement:
Phillips, Dusty. Python 3 Object-Oriented Programming: Build robust and maintainable software with object-oriented design patterns in Python 3.8, 3rd Edition (p. 119)
class AuthException(Exception):
def __init__(self, username, user=None):
super().__init__(username, user)
self.username = username
self.user = user
class UsernameAlreadyExists(AuthException):
pass
class PasswordTooShort(AuthExceptio)
pass
I have to note that instead of pass it would be better to add a string documentation here as recommended by Luciano Ramalho
So let see its use
class Authenticator:
def __init__(self):
"""Construct an authenticator to manage
users logging in and out."""
self.users = {}
def add_user(self, username, password):
if username in self.users:
raise UsernameAlreadyExists(username)
if len(password) < 6:
raise PasswordTooShort(username)
self.users[username] = User(username, password)
Hope this helps
来源:https://stackoverflow.com/questions/52509910/python3-calling-supers-init-from-a-custom-exception