Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
This is great! Thank you so much for the detailed walkthrough!
Question: I had never seen the super syntax used when defining custom exceptions. I've always just defined them as empty classes inheriting from a parent class (either Exception or one of my other custom exceptions.
When I do that, I get to see the message, but when I pass-through the message using the __init__ method like you've got shown, it looks different.
It seems almost better to just declare an empty class with pass to me. Is that wrong? Are there downsides? The official docs aren't super clear as to what's more common/expected.
I'd be curious how this behaves in terms of actually catching the exception with try...except, and what information would be available to e in except GloopException as e:? To be honest, I really don't know; I only understand that the standard is to use super().
I'll ask the Python friends who have been editing this series if they have any insight. ;)
Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
Okay, so after a bit of discussion in #python on Freenode IRC, we've come to this verdict:
Any time you have an __init__() and you've inherited, call super().__init__(). Period. However...
There are some camps that argue that if you don't otherwise need an __init__() (other than to call super), don't bother with it at all. Others, including myself, believe in always explicitly declaring an initializer with super(). I don't know that there's a clear right or wrong here. They both work out the same.
The weird behavior in your code is because there's a typo...which honestly came from me. You should NOT pass self to super().__init__(self, message)! That should instead read super().__init__(message). (I've fixed that in my article.)
Thanks to altendky, dude-x, _habnabit, TML, and Yhg1s of Freenode IRC's #python for the insight.
Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
Ooooohkay, gotcha. That makes total sense. Yeah, I think I fall in the camp of not showing the __init__ unless I'm extending it (because I think it looks prettier), but I could see the "explicit is better" argument for the other way.
The most important thing is that you showed me the right way to extend if I need to, which I really appreciate!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This is great! Thank you so much for the detailed walkthrough!
Question: I had never seen the
super
syntax used when defining custom exceptions. I've always just defined them as empty classes inheriting from a parent class (either Exception or one of my other custom exceptions.When I do that, I get to see the message, but when I pass-through the
message
using the__init__
method like you've got shown, it looks different.It seems almost better to just declare an empty class with
pass
to me. Is that wrong? Are there downsides? The official docs aren't super clear as to what's more common/expected.I'd be curious how this behaves in terms of actually catching the exception with
try...except
, and what information would be available toe
inexcept GloopException as e:
? To be honest, I really don't know; I only understand that the standard is to usesuper()
.I'll ask the Python friends who have been editing this series if they have any insight. ;)
Yeah, definitely. Good to know what is standard to use. Thanks so much! :)
Incidentally, I'm trying to find out more, because there seems to be some debate! (It's Python, so of course there is.)
Okay, so after a bit of discussion in
#python
on Freenode IRC, we've come to this verdict:Any time you have an
__init__()
and you've inherited, callsuper().__init__()
. Period. However...There are some camps that argue that if you don't otherwise need an
__init__()
(other than to call super), don't bother with it at all. Others, including myself, believe in always explicitly declaring an initializer withsuper()
. I don't know that there's a clear right or wrong here. They both work out the same.The weird behavior in your code is because there's a typo...which honestly came from me. You should NOT pass
self
tosuper().__init__(self, message)
! That should instead readsuper().__init__(message)
. (I've fixed that in my article.)Thanks to
altendky
,dude-x
,_habnabit
,TML
, andYhg1s
of Freenode IRC's#python
for the insight.Ooooohkay, gotcha. That makes total sense. Yeah, I think I fall in the camp of not showing the
__init__
unless I'm extending it (because I think it looks prettier), but I could see the "explicit is better" argument for the other way.The most important thing is that you showed me the right way to extend if I need to, which I really appreciate!