Classes and modules are singletons?

castironpi at gmail.com castironpi at gmail.com
Thu Mar 6 14:06:50 EST 2008


On Mar 6, 8:30 am, Carl Banks <pavlovevide... at gmail.com> 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.
>
> 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

What about

>>> o= object()
>>> b1= o.someattr
>>> reload( o )
>>> b2= o.someattr
>>> b1 is b2

?



More information about the Python-list mailing list