[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Classes and modules are singletons?

Steven D'Aprano

3/6/2008 1:45:00 AM

I recall that Python guarantees that module objects are singletons, and
that this must hold for any implementation, not just CPython: you can
only ever create one instance of a module via the import mechanism. But
my google-foo is obviously weak today, I cannot find where the Python
language reference guarantees that. Can somebody please point me at the
link making that guarantee?

(Note: you can create multiple modules with the same name and state using
new.module. I don't think that counts, although it may be a good way to
win bar bets with your Python buddies.)


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.

I've done some experimentation, e.g.:

>>> import module
>>> from module import Class
>>> module.Class is Class
True


but I'm not sure if that's (1) meaningful or (2) implementation-specific.


--
Steven
15 Answers

Terry Reedy

3/6/2008 2:06:00 AM

0


"Steven D'Aprano" <steve@REMOVE-THIS-cybersource.com.au> wrote in message
news:13suj3u2d17f205@corp.supernews.com...
|I recall that Python guarantees that module objects are singletons, and
| that this must hold for any implementation, not just CPython: you can
| only ever create one instance of a module via the import mechanism. But
| my google-foo is obviously weak today, I cannot find where the Python
| language reference guarantees that. Can somebody please point me at the
| link making that guarantee?
|
| (Note: you can create multiple modules with the same name and state using
| new.module. I don't think that counts, although it may be a good way to
| win bar bets with your Python buddies.)
|
|
| 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.
|
| I've done some experimentation, e.g.:
|
| >>> import module
| >>> from module import Class
| >>> module.Class is Class
| True
|
|
| but I'm not sure if that's (1) meaningful or (2) implementation-specific.

If I understand your question, classes are not singletons:
>>> ll=[]
>>> for i in range(2):
import string
ll[i]=string

>>> ll[0] is ll[1]
True
>>> for i in range(2):
class C: pass
ll[i] = C


>>> ll[0] is ll[1]
False

tjr



Steven D'Aprano

3/6/2008 2:32:00 AM

0

On Wed, 05 Mar 2008 21:05:31 -0500, Terry Reedy wrote:


> If I understand your question, classes are not singletons:
>>>> ll=[]
>>>> for i in range(2):
> import string
> ll[i]=string

Where's the IndexError? :-)

>>>> ll[0] is ll[1]
> True

But yes, modules are singletons in that way, at least if you go through
the import mechanism.


>>>> for i in range(2):
> class C: pass
> ll[i] = C
>
>
>>>> ll[0] is ll[1]
> False


Ah, but each time around the loop you create a *new class* that just
happens to be called C. An alternative way to see similar behaviour is:


def foo(x=None):
class C(object):
X = x
return C

Naturally foo() is foo() gives False -- although both classes are called
C, they are different classes that just happen to have the same state.


I accept my question about classes being singletons is not well-formed,
not even in my own mind. I guess one way of asking is, for any two class
objects (not instances) C1 and C2, does "C1 == C2" imply "C1 is C2"?




--
Steven

Micah Cowan

3/6/2008 3:39:00 AM

0

Steven D'Aprano <steve@REMOVE-THIS-cybersource.com.au> writes:

> I recall that Python guarantees that module objects are singletons, and
> that this must hold for any implementation, not just CPython: you can
> only ever create one instance of a module via the import mechanism. But
> my google-foo is obviously weak today, I cannot find where the Python
> language reference guarantees that. Can somebody please point me at the
> link making that guarantee?


It's not an absolute strict guarantee; it's just implied by the fact
that "import" uses any appropriate objects already found in
sys.modules.

>>> import sys
>>> ll = []
>>> for i in range(2):
.... import string
.... ll.append(string)
.... del sys.modules['string']
....
>>> ll[0] is ll[1]
False

(I'm sure there are very good reasons never to do what I just did there.)

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.c...

Aaron Brady

3/6/2008 3:44:00 AM

0

On Mar 5, 8:31 pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
> On Wed, 05 Mar 2008 21:05:31 -0500, Terry Reedy wrote:
> > If I understand your question, classes are not singletons:
> >>>> ll=[]
> >>>> for i in range(2):
> >  import string
> >  ll[i]=string
>
> Where's the IndexError? :-)
>
> >>>> ll[0] is ll[1]
> > True
>
> But yes, modules are singletons in that way, at least if you go through
> the import mechanism.
>
> >>>> for i in range(2):
> >  class C: pass
> >  ll[i] = C
>
> >>>> ll[0] is ll[1]
> > False
>
> Ah, but each time around the loop you create a *new class* that just
> happens to be called C. An alternative way to see similar behaviour is:
>
> def foo(x=None):
>     class C(object):
>         X = x
>     return C
>
> Naturally foo() is foo() gives False -- although both classes are called
> C, they are different classes that just happen to have the same state.
>
> I accept my question about classes being singletons is not well-formed,
> not even in my own mind. I guess one way of asking is, for any two class
> objects (not instances) C1 and C2, does "C1 == C2" imply "C1 is C2"?

>>> class metaC( type ):
... def __eq__( self, other ):
... return random.choice([ True, False ])
...
>>> class C( metaclass= metaC ):
... pass
...
>>> import random
>>> C==C
True
>>> C==C
False
>>> C==C
False
>>> C==C
True
>>> C==C
False
>>>

You made me think of this. What relevance it has is open to
debate. ...In a separate thread.

http://en.wikipedia.org/wiki...

It's my Signal-channel signal signal.

Aaron Brady

3/6/2008 3:52:00 AM

0

> > > If I understand your question, classes are not singletons:
> > >>>> ll=[]
> > >>>> for i in range(2):
> > >  import string
> > >  ll[i]=string
>
> > Where's the IndexError? :-)
>
> > I accept my question about classes being singletons is not well-formed,
> > not even in my own mind. I guess one way of asking is, for any two class
> > objects (not instances) C1 and C2, does "C1 == C2" imply "C1 is C2"?


Similarly, no.

>>> class metaC( type ):
... def __eq__( self, other ):
... return True
...
>>> class C( metaclass= metaC ):
... pass
...
>>> class D( metaclass= metaC ):
... pass
...
>>> C==D
True
>>> C is D
False

+1 raised eyebrow

Aaron Brady

3/6/2008 4:35:00 AM

0

On Mar 5, 9:51 pm, castiro...@gmail.com wrote:
> > > > If I understand your question, classes are not singletons:
> > > >>>> ll=[]
> > > >>>> for i in range(2):
> > > >  import string
> > > >  ll[i]=string
>
> > > Where's the IndexError? :-)
>
> > > I accept my question about classes being singletons is not well-formed,
> > > not even in my own mind. I guess one way of asking is, for any two class
> > > objects (not instances) C1 and C2, does "C1 == C2" imply "C1 is C2"?
>
> Similarly, no.
>
> >>> class metaC( type ):
>
> ...     def __eq__( self, other ):
> ...             return True
> ...>>> class C( metaclass= metaC ):
>
> ...     pass
> ...>>> class D( metaclass= metaC ):
>
> ...     pass
> ...>>> C==D
> True
> >>> C is D
>
> False
>
> +1 raised eyebrow

Well, you're not going to believe me, but a <scaryword>"bar"</
scaryword> just had an idea.

Don't forget:
class C( metaclass= metaC )
class metaC( metaclass= metametaC )
class metametaC( type ): bor hor hor.

Now for the academics, is it telling a joke? If so, that's pretty
fine--- but.

Aaron Brady

3/6/2008 6:25:00 AM

0

> I accept my question about classes being singletons is not well-formed,
> not even in my own mind. I guess one way of asking is, for any two class
> objects (not instances) C1 and C2, does "C1 == C2" imply "C1 is C2"?

C and D are instances of metaC in that.

class metaC( type ):
def what( self ):
return self

class C( metaclass= metaC ): pass
class D( metaclass= metaC ): pass

assert C is C.what() and D is D.what()
#print( C.what(), D.what() )

C= metaC('C',(),{})
D= metaC('D',(),{})
assert C is C.what() and D is D.what()
#print( C.what(), D.what() )

furthermore.

Carl Banks

3/6/2008 2:31:00 PM

0

On Mar 5, 8:44 pm, Steven D'Aprano <st...@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

Aaron Brady

3/6/2008 7:07:00 PM

0

On Mar 6, 8:30 am, Carl Banks <pavlovevide...@gmail.com> wrote:
> On Mar 5, 8:44 pm, Steven D'Aprano <st...@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

?

Marc 'BlackJack' Rintsch

3/6/2008 8:58:00 PM

0

On Thu, 06 Mar 2008 11:06:50 -0800, castironpi wrote:

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

You are really a bit thick, a troll, or a bot.

*plonk*

Ciao,
Marc 'BlackJack' Rintsch