Why would I use inspect.isclass()?

Nicolas Fleury nid_oizo at yahoo.com_removethe_
Thu Dec 30 04:20:29 CET 2004


> an_instance=Abc()

> But what good is that?  Of course I know Abc is a class, why would I want to
> inspect it so that it would tell me what I already know?

Well, for no reason in that case.  For the same reason you would not 
call isinstance(an_instance, Abc) if you already know an_instance is an 
instance of Abc.

>     def Hello(self, some_class):
>     # since I don't know if the argument passed down *is* a class or not, I
> would:
>         if inspect.isclass(some_class)==True:
>            ...
>         return
> ###
> 
> But that obviously isn't what isclass is for.  What should I use?

Well, it's obviously what isclass is for;)  (By the way, you would be 
better not compare your conditions with True, unless it's really what 
you want).

I guess another example would be an assert on the type of argument:
def foo(someClass):
     assert inspect.isclass(someClass)
     # rest of code

This way errors on types are handled at the beginning and at the same 
time the code it documenting itself.  The function could also be useful 
in cases where you do some C++-like overloading mechanism.  Anyway, 
isclass, like iscallable, are functions that are not used often, but you 
still might need them.

Regards,
Nicolas



More information about the Python-list mailing list