sufficiently pythonic code for testing type of function

Theerasak Photha hanumizzle at
Wed Oct 11 07:06:11 CEST 2006

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.

Is it enough to rely on side effects or absence thereof, or should I
put return True in here somewhere?

def test_obj_type(obj, types):
  for type in types:
    if isinstance(obj, type):
      raise ValueError, 'object is not in %s' % types

-- Theerasak

More information about the Python-list mailing list