sufficiently pythonic code for testing type of function

Ben Finney bignose+hates-spam at benfinney.id.au
Wed Oct 11 08:02:26 CEST 2006


"Theerasak Photha" <hanumizzle at gmail.com> writes:

> I wrote this for someone else to take an object and list of types,
> then check if obj is one of those types, raising an error otherwise.

This is a way of programming known as "Look Before You Leap" (LBYL).
It is most likely a mistake to do this in Python. (That's not an
absolute, but it's the smart way to bet.)

Far better is to take the objects passed, *use* them in the way that
you need to use them, and catch exceptions that get raised at the
point where there is enough context to handle them. This is the
"Easier to Ask Forgiveness than Permission" (EAFP) principle, and it
is considered to be far more Pythonic.

    <URL:http://jaynes.colorado.edu/PythonIdioms.html>

-- 
 \       "Prediction is very difficult, especially of the future."  -- |
  `\                                                        Niels Bohr |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list