Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Classes and modules are singletons?

Reply
Thread Tools

Classes and modules are singletons?

 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      03-06-2008
On Thu, 06 Mar 2008 11:06:50 -0800, castironpi wrote:

> On Mar 6, 8:30┬*am, Carl Banks <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Aahz
Guest
Posts: n/a
 
      03-06-2008
In article <(E-Mail Removed)>,
Steven D'Aprano <(E-Mail Removed)> wrote:
>
>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"?


Even that stricture fails under the presence of metaclasses. But
answering your real question, I don't remember off-hand the required
sequence, but it is possible to import a class two different ways such
that the classes are not the object. This can cause problems with e.g.
pickle. Within a single module, given a class defined only once within
that module, the class will be a singleton.
--
Aahz ((E-Mail Removed)) <*> http://www.pythoncraft.com/

"All problems in computer science can be solved by another level of
indirection." --Butler Lampson
 
Reply With Quote
 
 
 
 
castironpi@gmail.com
Guest
Posts: n/a
 
      03-06-2008
On Mar 6, 2:57*pm, Marc 'BlackJack' Rintsch <(E-Mail Removed)> wrote:
> On Thu, 06 Mar 2008 11:06:50 -0800, castironpi wrote:
> > On Mar 6, 8:30*am, Carl Banks <(E-Mail Removed)> 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.


The point was, that's one difference between classes and modules: you
can't reload classes plonk.
 
Reply With Quote
 
castironpi@gmail.com
Guest
Posts: n/a
 
      03-06-2008
On Mar 6, 3:24*pm, (E-Mail Removed) (Aahz) wrote:
> In article <(E-Mail Removed)>,
> Steven D'Aprano *<(E-Mail Removed)> wrote:
>
>
>
> >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"?

>
> Even that stricture fails under the presence of metaclasses. * *But
> answering your real question, I don't remember off-hand the required
> sequence, but it is possible to import a class two different ways such
> that the classes are not the object. *This can cause problems with e.g.
> pickle. *Within a single module, given a class defined only once within
> that module, the class will be a singleton.
> --
> Aahz ((E-Mail Removed)) * * * * * <*> * * * *http://www.pythoncraft.com/
>
> "All problems in computer science can be solved by another level of * *
> indirection." *--Butler Lampson


I'd like to question the source of the definition of C.__eq__.

Observation:

>>> class C: pass

...
>>> class D: pass

...
>>> C== D

False

What is different about them? I've created two empty classes, nothing
more.
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      03-06-2008
En Thu, 06 Mar 2008 20:45:09 -0200, <(E-Mail Removed)> escribi´┐Ż:

> I'd like to question the source of the definition of C.__eq__.
>
> Observation:
>
>>>> class C: pass

> ...
>>>> class D: pass

> ...
>>>> C== D

> False
>
> What is different about them? I've created two empty classes, nothing
> more.


Their __name__ attribute?
Types are compared by their memory addresses, not by contents, and that's
enough and efficient for most people. If you require something different,
use a metaclass.

--
Gabriel Genellina

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      03-07-2008
On Thu, 06 Mar 2008 06:30:41 -0800, Carl Banks 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.


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Big projects divided into modules and sub-modules ImpalerCore C Programming 0 03-10-2011 03:01 PM
Modules and Classes, Includes and Extends Alpha Blue Ruby 0 01-30-2010 11:42 PM
Importing modules from within other modules Tobiah Python 2 09-14-2003 09:18 PM
imputils - import problem modules from sys.modules Remy Cool Python 1 08-27-2003 02:25 PM



Advertisments