[Python-Dev] subclass a module?

Guido van Rossum guido@python.org
Sat, 01 Jun 2002 09:36:51 -0400


> > > Can I now subclass from modules?
> > 
> > It's a bug IMO.  
> > 
> > > And if so, what good does that do me?
> > 
> > None whatsoever.  The resulting class cannot be instantiated.
> 
> Really?
> 
> >>> import re
> >>> class X(type(re)):
> ...     def hello(): print 'hi'
> ...
> >>> newmod = X()
> >>> newmod.hello
> <bound method X.hello of <module '?' (built-in)>>

You subclass the module metaclass.  The example we were discussing was
different: it subclassed the module itself, like this:

    >>> import re
    >>> class X(re):
	 pass
    ...
    >>> X()
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    TypeError: 'module' object is not callable
    >>> 

--Guido van Rossum (home page: http://www.python.org/~guido/)