how to find out if an object is a class?

Stefan Behnel stefan_ml at behnel.de
Fri Aug 8 02:31:59 EDT 2008


Ben Finney wrote:
> szczepiq writes:
> 
>> Pardon me for most likely a dummy question but how do I find out if
>> an object is a class?
> 
> Presumably you want to know whether it's a class in order to use it
> for instantiating it. It is usually more Pythonic to use the object as
> intended, and allow the object itself to tell you (via exceptions)
> when it's not behaving as you expect.

This also allows passing a factory function instead of a class, BTW.

I recently had the reverse case that a (stupidly implemented) extension module
required a callback function and I wanted to pass a function wrapped in a
wrapper object. That failed, because it specifically checked for the argument
being a function, not just a callable object. I had to pull quite a number of
tricks to reimplement the wrapper class as a function (thank god, it's Python!).

Stefan



More information about the Python-list mailing list