Why would I use inspect.isclass()?

It's me itsme at yahoo.com
Wed Dec 29 22:40:49 EST 2004


Nicolas,

Thanks for the response.  Please see comment below.

"Nicolas Fleury" <nid_oizo at yahoo.com_removethe_> wrote in message
news:asKAd.30749$%k.2293869 at weber.videotron.net...
> > 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).
>

But inspect.isclass(some_class) returns a False no matter what.   That's why
I am confused.

The only way I get isclass to return True is inspect.isclass(Abc) in my
example.


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

But that would always fail!  (I just tried it).

> 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