Classes and modules are singletons?

Carl Banks pavlovevidence at gmail.com
Thu Mar 6 09:30:41 EST 2008


On Mar 5, 8:44 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> But what about classes? Are they singletons? Obviously classes aren't
> Singleton classes, that is, given an arbitrary class C you can create
> multiple instances of C. But what about class objects themselves? I've
> found a few odd references to "classes are singletons", but nothing in
> the language reference.


Probably because "singleton" is the wrong word.  A singleton means
there is one instance of a type; classes are instances of "type" which
can have many instances so classes are not singletons.

Anyway, the answer to what you are probably asking is No.  Try this:

>>>import module
>>>c1 = module.Someclass
>>>reload(module)
>>>c2 = module.Someclass
>>>c1 is c2


For that matter, try this:

>>>import module
>>>c1 = module.Someclass
>>>module.Someclass = some_other_class()
>>>c2 = module.Someclass
>>>c1 is c2


Carl Banks



More information about the Python-list mailing list