What is meant by "using the EAFP principle" in Python? Could you provide any examples?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
From the glossary:
An example would be an attempt to access a dictionary key.
EAFP:
LBYL:
The LBYL version has to search the key inside the dictionary twice, and might also be considered slightly less readable.
I call it "optimistic programming". The idea is that most times people will do the right thing, and errors should be few. So code first for the "right thing" to happen, and then catch the errors if they don't.
My feeling is that if a user is going to be making mistakes, they should be the one to suffer the time consequences. People who use the tool the right way are sped through.