Re: Singleton implementation problems

2008-07-05 Thread Urizev
Great! Thanks everyone for so many references and comments. Lots of
doubts have been solved.

On Fri, Jul 4, 2008 at 10:33 AM, Peter Otten <[EMAIL PROTECTED]> wrote:
> Ben Finney wrote:
>
>> Peter Otten <[EMAIL PROTECTED]> writes:
>>
>>> The problem is the structure of your program. The myset module is
>>> imported twice by Python, once as "myset" and once as "__main__".
>>
>> Yes, this is the problem. Each module imports the other.
>>
>>> Therefore you get two distinct MySet classes, and consequently two
>>> distinct MySet.__instance class attributes.
>>
>> Are you sure? This goes against my understanding: that 'import foo'
>> will not re-import a module that's already been imported, but will
>> instead simply return the existing module.
>
> The main script is put into the sys.modules cache as "__main__", not under
> the script's name. Therefore the cache lookup fails.
>
>> So, I think if one evaluated 'myset is __main__', you'd find they are
>> exactly the same module under different names; and therefore that
>> there is only *one* instance of 'MySet', again under two names.
>
> No:
>
> $ cat tmp.py
> import tmp
> import __main__
>
> print tmp is __main__
>
> $ python tmp.py
> False
> False
>
> Peter
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Saludos

Juan Carlos

"¡¡Viva lo rancio!!"
--
http://mail.python.org/mailman/listinfo/python-list


Singleton implementation problems

2008-07-03 Thread Urizev
Hi everyone

I have developed the singleton implementation. However I have found a
strange behaviour when using from different files. The code is
attached.

Executing main
new MySet object
No singleton instance
New singleton:
<__main__.Singleton instance at 0x2b98be474a70>
new MySet object
There is a singlenton instance
new Member
new MySet object
No singleton instance
New singleton:

new Member
new MySet object
There is a singlenton instance
new Member
new MySet object
There is a singlenton instance

I do not know why, but it creates two instances of the singleton. Does
anybody know why?

Regards
import myset

class Member:
def __init__ (self):
print "new Member"
self.name = "one member"
instance = myset.MySet ()
instance.add (self)
import member

class MySet:
"""MySet aplication"""

__instance = None

## Singleton instance

## This class implements all methods of MySet
class Singleton:

def __init__(self):
#Set the Glade file
print "New singleton:"
print self
self.members = {}

def add (self, member):
self.members[member.name] = member;



###
#
#  MYSET DELEGATES EVERYTHING IN SINGLETON METHOD
#
###

def __init__( self ):
print "new MySet object"

if MySet.__instance is None:
print "No singleton instance"
MySet.__instance = MySet.Singleton()
else:
print "There is a singlenton instance"
 
self.__dict__['_EventHandler_instance'] = MySet.__instance


def __getattr__(self, aAttr):
return getattr(self.__instance, aAttr) 

 
def __setattr__(self, aAttr, aValue):
return setattr(self.__instance, aAttr, aValue)


###
#
# MAIN CALL
#
###


if __name__ == "__main__":
print "Executing main"
set1 = MySet ()
set2 = MySet ()

mbr1 = member.Member ()
mbr2 = member.Member ()
mbr3 = member.Member ()
--
http://mail.python.org/mailman/listinfo/python-list