Python Developer interview question · Most asked
What do EAFP and LBYL mean, and which is more Pythonic?
Summary
EAFP ('Easier to Ask Forgiveness than Permission') tries the operation and handles exceptions; LBYL ('Look Before You Leap') checks conditions first. EAFP is generally considered more Pythonic and avoids race conditions (e.g., a file deleted between check and open).
Longer Answer
Use EAFP with narrow except clauses. LBYL is fine when failures are common and checks are cheap, or when exceptions would be costly in a hot loop.
Key Points
- EAFP: try/except
- LBYL: check first
- EAFP avoids TOCTOU races
- Keep except clauses narrow
More most-asked Python Developer questions
- Backend Python interview: explain N+1 queries.
- Backend Python interview: explain authN vs authZ.
- Backend Python interview: explain SQL injection prevention.
- Explain decorators in Python.
- Explain context managers in Python.
- Explain GIL in Python.
- Explain threading vs multiprocessing vs asyncio in Python.
- Explain generators in Python.