-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document best practices for exceptions #59239
Comments
And I wish I knew what those were. |
I don’t understand the request. |
The obvious example is that the tutorial makes no mention of calling 'super' in __init__. I'm also aware that there are issues of pickleability that arise if you do things one way, but do not arise if you do things another way. But I don't know the details, and I'd like to see the tutorial show an example of the *best* way to write a user defined exception so that they behave like the built in Python exceptions. |
This seems more like a Stack Overflow question or fodder for a blog post. When such best practices become well known and agreed upon, they can be added toa HOWTO document. The tutorial is problematic because beginners start there and sprinkling the nuances of super() throughout the tutorial will only get in the way of the main learning points for a given section. |
OK, let's move this, then. I asked the question because I'd like to know what the best practice is for exceptions in the stdlib. This is an area in which we have made quite a bit of progress recently (ie: the work done on exceptions for Python3, and PEP-3151), but I think there is still a lack of documentation (and possibly consensus?) on best practices for the stdlib. |
I agree with Raymond that this, especially super() is not tutorial material. I agree with David that we need something somewhere else. Just today a commit was pushed about IOException subclasses causing problems because they have an __init__ but don't call super(). (Something like that.) I had no idea about this issue. The stdlib has things like "class ModException(Exception): pass". Is that okay because it does *not* have __init__ override? |
I'm not sure this belongs to the devguide. |
I think the Library Manual's chapter 5 on exceptions, currently called Built-in Exceptions, would be a good place for a section on subclassing exceptions. |
I'm removing the "devguide" component and update the title accordingly. |
There are examples in the tutorial where super().__init__ is not called, like here: https://docs.python.org/3/tutorial/errors.html#user-defined-exceptions |
Terry: of course, if a method is not overridden, there's no need to call super()'s method, since the only thing it would do is call the same thing that would be called without overriding. But of course, if Exception's __init__ does anything, then super() should be called from the overrridden method. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: