[Tutor] Types of instances

Einar Th. Einarsson einarth@decode.is
Thu, 20 Jun 2002 11:04:58 +0000


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hey folks.

How do I discover the type of an object at runtime?

For example if I have piece of code like:

try:
    raise AttributeException, "blah"
except Exception, e:
    print type(e)

python happily tells me that e is an instance....which is kinda bloody obvi=
ous=20
and utterly useless information.  I want to know if e is a AttributeError, =
or=20
a TypeError or something else alltogether.=20

The repr(object) function almost does what I want, and it's trivial to writ=
e a=20
wrapper function around that would serve my purposes, except that I'd also =

like to know the type of the parent class(es), if any, and thus potentially=
=20
discover the pedigree of the class in question at runtime.

So, is there something in the Python language or it's built-in stuff that d=
oes=20
this?

- --=20
Disclaimer: I'm not as smart as I think I am.

Yours etc.
    Einar Th.

-----BEGIN PGP SIGNATURE-----
Version: PGP 6.5.8

iQA/AwUBPRG22m1/ORZtyd/tEQL3QgCeITKueiMGLqs2xraaF7rRDZ+xCyQAnAo1
ISHbcGvl6kB7hHX0g3wF0APH
=3Da345
-----END PGP SIGNATURE-----