jeudi 19 avril 2018

How to appropriately use the EAFP principle

I'm currently learning Python and came across Python's mantra that it's "Easier to ask for forgiveness than permission". I'm not a very experienced programmer (having only learnt a bit of Java in high school), and so I feel more familiar with the 'LBYL' principle involving a bunch of 'if' statements when trying to do something.

The question I have is :

How does one know when to use the EAFP principle by using 'try and except' instead of using if statements?

I think what I'm trying to get at is when is it appropriate to replace an 'if' statement with 'try-except' and when is it appropriate just use 'if' statement instead?

I'd like to learn and apply good coding practice, but reading about the EAFP principle has got me a bit confused about when I should be using 'try and except' instead of if statements.

Aucun commentaire:

Enregistrer un commentaire