singleton problems

jimgardener jimgardener at gmail.com
Sun Oct 3 12:31:55 CEST 2010


hi Steven,
can you explain that?I didn't quite get it.
I have a module say 'managerutils' where I have a class
MyManager..

ie,
managerutils.py
----------
class MyManager(object):
    def __init__(self):
        self.myaddresses={}
    ...

from another main program ,if I call ,
import managerutils

m1=MyManager()
m2=MyManager()
m3=MyManager()

print m1
print m2
print m3

I get
tm1= <managerutils.MyManager object at 0x9f4f58c>
tm2= <managerutils.MyManager object at 0x9f4f5ac>
tm3= <managerutils.MyManager object at 0x9f4f5cc>
tm4= <managerutils.MyManager object at 0x9f4f5ec>

these would print different instances
thanks
jim


> If you do need a singleton, the simplest way in Python is just to use a
> module. You initialise the singleton with:
>
> import module
>
> You can do this as many times as you want. Then you call functions on it
> with:
>
> module.function(args)
>
> You need, literally, zero extra coding.
>



More information about the Python-list mailing list