instantiate all subclasses of a class

Peter Otten __peter__ at web.de
Sun Jul 16 10:54:52 EDT 2006


Daniel Nogradi wrote:

> Thanks, this looks pretty good. However there is some wierdness with
> isclass: whenever a class has a __getattr__ method an instance of it
> will be detected by isclass as a class (although it is not).
> 
> >>> from inspect import isclass
> >>>
> >>> class x:
>  ...     def __getattr__( self, attr ):
>  ...         pass
>  ...
> >>> y = x( )
> >>> isclass( y )
> True

Which reinforces Michael Spencer's instinct that the inspect.isclass()
implementation is a bit too clever (see
http://mail.python.org/pipermail/python-list/2006-July/351448.html).

> If there is no __getattr__ method isclass works as expected. Am I
> misunderstanding something here or isclass should return False for any
> instance of any class including those with a __getattr__ method?

It certainly should, and I believe that the obvious test

isinstance(obj, (types.ClassType, type))

will work.

Peter



More information about the Python-list mailing list