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

← All Python Developer questions