Subclassing hidden class

Alex Martelli aleaxit at
Mon Feb 17 15:36:33 CET 2003

John Smith wrote:

> Is there a quick way to subclass that usually use a constructor function
> to return objects?
> For example,
>>>> import numarray
>>>> class myarray(numarray.array):
>  pass
> Traceback (most recent call last):
>   File "<pyshell#36>", line 1, in ?
>     class myarray(numarray.array):
> TypeError: cannot create 'function' instances
> I know there is probably a way to subclass numarray.array by tracing the
> sorce code. But I wonder if
> there is a better way.

Only if a type WANTS to be subclassed, can you subclass it.  I.e.:
you can try

class myarray(type(numarray.array(0))):

but that will work if and ONLY if that type is set up to allow you to
subclass it.

> Or, is there an easier way to create a wrapper class without define all
> the methods?  Thanks


I discuss it a bit better in the printed edition of the Cookbook, but
I think the key idea is well exhibited even in this online version.


More information about the Python-list mailing list