Using a class attribute without knowing it's existence.
What to do without a Case/Switch feature.
Wrong Ways
My evolution to finding this pattern. Lets say you have a class and you want to see if it has an attribute. The first way I thought to do this was:
Very wrong way
Then I found out about the hasattr
function.
less wrong way
Right Way - "Easier to Ask for Forgiveness instead of Permission"
The Python community generally prefers "EAFP" rather than "LBYL" (Look Before Your Leap).
What this means in practice is to use a try and except block.
Right Way
Sources and References
Last updated