Why not allow empty code blocks?

Steven D'Aprano steve at pearwood.info
Fri Jul 29 09:43:39 EDT 2016


On Fri, 29 Jul 2016 11:15 pm, BartC wrote:

> On 29/07/2016 12:14, D'Arcy J.M. Cain wrote:
>> On Fri, 29 Jul 2016 10:58:35 +0200
>> Antoon Pardon <antoon.pardon at rece.vub.ac.be> wrote:
>>> As BartC already mentions it happens fairly often during debugging.
>>> Something like.
>>>
>>> try:
>>>    Some code
>>> except Some_Exception:
>>>    # Commented code for when I am debugging <Some code>
>>>    pass
>>
>> I realize that that's a simplified example but really, isn't this just
>> as easy?
>>
>> try:
>>    Some code
>> # except Some_Exception:
>>    # Commented code for when I am debugging <Some code>
> 
> Will it behave the same way when there is a Some_Exception exception?

Of course it won't, which is why I don't believe all these folks who claim
that they regularly ("all the time", "fairly often") replace except blocks
with `pass`. I call shenanigans -- perhaps you do it *occasionally*, but as
a general rule, you can rarely replace the exception handler with a
do-nothing clause and expect your code to work:

try:
    block
except SomeException:
    handle error
process

If you remove the error handling block, replacing it with `pass`, in general
your code will just break again as soon as it continues processing.


-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list