What do you call a class not intended to be instantiated

Ben Finney bignose+hates-spam at benfinney.id.au
Sun Sep 28 10:03:45 CEST 2008


Terry Reedy <tjreedy at udel.edu> writes:

> Steven D'Aprano wrote:
> 
> > And modules aren't callable. I've often thought they should be.
> 
> Modules are not callable because their class, module, has no
> __call__ instance method. But (in 3.0, which is all I will check)
> you can subclass module and add one.

Works fine in Python 2.5.2 also::

    Python 2.5.2 (r252:60911, Aug  8 2008, 11:09:00)
    [GCC 4.3.1] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> module = type(__builtins__)
    >>> module
    <type 'module'>
    >>> '__call__' in dir(module)
    False

    >>> import sys
    >>> class CallableModule(module):
    ...     def __call__(self, *args, **kwargs):
    ...         sys.stdout.write("%(self)r, %(args)r, %(kwargs)r\n" % vars())
    ...
    >>> '__call__' in dir(CallableModule)
    True
    >>> foo = CallableModule('foo')
    >>> foo(1, 2, 3, a=4, b=5)
    <module 'foo' (built-in)>, (1, 2, 3), {'a': 4, 'b': 5}
    >>> foo
    <module 'foo' (built-in)>

-- 
 \        “There are only two ways to live your life. One is as though |
  `\          nothing is a miracle. The other is as if everything is.” |
_o__)                                                 —Albert Einstein |
Ben Finney



More information about the Python-list mailing list