3 ms·
I routinely encounter places where I am forced to use try..except where it would be better and more easily understood if I did not have to work around the fact
by R0b0t1 5y ago
I routinely encounter places where I am forced to use try..except where it would be better and more easily understood if I did not have to work around the fact I am forced to goto out of the current scope.
- guenthert 5y agoI didn't downvote you, but I suspect you have been because you're mistaken. try...except doesn't force you to go out of scope, it keeps you in it. raising an exception leaves the scope, but nobody forces you to use it.
- R0b0t1 5y agoNot mistaken. The try..except creates a scope. It is not always, but often, that I need to address this new nested scope when structuring exception handling when I could simply not if exceptions were not used.
- formerly_proven 5y agoYou are mistaken. try...except does not introduce a scope in Python. Only "def" and "class" do.
- kdmccormick 5y agoYup. try: x = uncertain_op() except ...: x = 'fallback' do_something(x) is a common Python idiom; no initial definition required. Although this isn't true in every language.
- R0b0t1 5y agoHm, I suppose you're right. I just tested it. I can't for the life of me remember what I was thinking of then.
- formerly_proven 5y agoIn many (most?) other languages compounds / blocks always introduce a scope. E.g. in C++ (or Java) try { /* new scope */ } catch (FooException &foo) { /* another scope */ } Python is probably one of not that many languages where this is not the case.