How to shoot yourself in the foot with Python's variables scope

Got fooled again by Pythonic crap...

The code above is a simplified version of a real bug encountered at work. The script executes without error, PEP8 doesn't see anything problematic, and in the original script the bug was completely hidden, only found much later by pure chance.

As written in the comment, the bug is that I forgot to reassign the hoge variable in the second loop. In Python, variables scope is global at function level, so the hoge variable of the first loop was used instead, with the value it has at the end of the first loop. Another occasion for Python to prove itself as a bullshit language by not following principles on which every experience programmer agrees (1, 2, 3, 4, 5, ...)

Try to do the same thing in a serious programming language, C for example:

and the compiler will yell back at you and save you the shame and pain two months later when the bug will resurface.

2022-07-21
in All, Pub talk,
72 views
Copyright 2021-2024 Baillehache Pascal