OOP - how to abort an __init__ when the initialisation code fails ?

R.Wieser address at not.available
Tue Nov 5 02:22:23 EST 2019


Luciano,

> In this context, what I mean by that quote is to say that Python
> never forces you to wrap anything in try/except blocks.

True.  But /not/ doing it means that the casted exeption in the __init__ 
block will just abort the whole program - in a user unfriendly way.  And 
thats normally not something I want.

> The only reason to catch an exception prior to aborting is to
> provide a better error message,

:-) There you go.

> In addition, as Rob said, it is usually a bad idea to wrap
> several lines of code in a single try/except block

I got that part.   So, do you have, for the case I presented, something 
better to offer ?

> Yes, you totally should learn how try/except works in Python,

Well, with less than a week under my belt I'm not doing too bad, am I ?  :-)

(Remark: I've been programming for a while now.  Its just Python thats new 
to me)

> and alsolearn a bit of Python culture, "the way we do things around here" 
> ;-).

:-)   As long as I want to post questions here there will be no way to evade 
getting to know that culture.   And I'm ofcourse open to suggestions, 
especially when they make my python programming life easier.

> It is considered totally OK to use exception handling as a control
> flow mechanism in Python.

I'm not sure what exactly you mean with "a control flow mechanism", but as 
several methods throw exceptions themselves I will have no choice in having 
to deal with them.

... and as the topic of this thread has already shown, I'm not at all 
unwilling to learn about it.  Quite the opposite actually.

Regards,
Rudy Wieser




More information about the Python-list mailing list