Quick post about a bug that I fixed this morning that was quite embarrassing. I had a Python function in which I did something like the following:
if getattr(obj, method):
...
Experienced Pythonistas will spot the problem immediately: if obj doesn’t have an attribute method, getattr will throw an exception. What I should have done (and this is how I fixed the bug) was to add a third parameter to specify a default value when the attribute doesn’t exist.
I usually like dynamic typing, but this time I hated it: if this had been Haskell, the type checker would’ve called me a retard and refused to compile my code.
Oh well, what doesn’t kill you makes you stronger I guess. I probably should have had a test for this particular case too.
Posted by gnuvince