What do you call a class not intended to be instantiated

Terry Reedy tjreedy at udel.edu
Sat Sep 27 23:41:42 CEST 2008


Aahz wrote:
> In article <8763oiuhj2.fsf at benfinney.id.au>,
> Ben Finney  <bignose+hates-spam at benfinney.id.au> wrote:
>> Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:

>>> I'd like to be able to call [a class] as if it were a function.
>>> Normally calling a class object returns an instance -- I wish to
>>> return something else.
>> In that case, you *don't* want a class at all; the entire point of a
>> class is to define behaviour for instances.
> 
> Absolutely agreed with your first clause, disagreed about the second
> clause.  As I said earlier, the main point of a class singleton is to get
> the effect of a module singleton without the need to create another file
> on disk.

In 3.0, at least, one does not need a disk file to create a module.

 >>> import types
 >>> me = types.ModuleType('me') # type(__builtins__) works, no import
 >>> me
<module 'me' (built-in)>
 >>> me.a = 1
 >>> me.a
1
 >>> me.a + 1
2

That said, a blank class is even easier, and the representation is better.

tjr




More information about the Python-list mailing list