Classes and modules are singletons?
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Fri Mar 7 07:15:17 EST 2008
On Thu, 06 Mar 2008 06:30:41 -0800, Carl Banks wrote:
> 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.
Right. I knew there was something funny about using the term "singleton"
to refer to classes, but I couldn't put my finger on it.
[snip]
> For that matter, try this:
>
>>>>import module
>>>>c1 = module.Someclass
>>>>module.Someclass = some_other_class()
>>>>c2 = module.Someclass
>>>>c1 is c2
That example is cheating because you rebind the *name* module.Someclass.
Of course you get something different.
But in any case, I'm satisfied now... the name singleton is inappropriate
for modules and classes, although they are both singleton-like. I like
Gabriel's term "named singleton" (from another thread).
Thank you to everybody who answered.
--
Steven
More information about the Python-list
mailing list