From: O'Brien-Strain, E. <eo...@ex...> - 2002-03-23 00:22:28
|
If you want to enforce the singleton pattern you can remove the constructor in the constructor itself. That way only one object can be created. For example: #Define the class class Foo: def __init__(self,...): Foo.__init__ = None ... ... #Create the one-and-only instance Foo.instance = Foo(...) All other code should use "Foo.instance" to refer to the singleton. If any other code erroneously tries to create a second Foo it will get a TypeError complaining about trying to call a non-function. (For a nicer error message, instead of assigning __init__ to None you can assign it to a function that raises a more meaningful exception.) __ Eamonn O'Brien-Strain HP Labs eo...@hp... > -----Original Message----- > From: Ype Kingma [mailto:yk...@xs...] > Sent: Friday, March 22, 2002 3:27 PM > To: jyt...@li... > Subject: Re: [Jython-users] Singleton > > > Stephen, > > >Hi, > > > >I have read lots of stuff on creating the singleton design > pattern using > >python and to be honest most of this information has left me > confused. > >What is the best way to create the singleton in jython? > > There is no single best way. You can use the borg pattern > (use google to look for: python borg pattern singleton). > Or you implement the singleton using module semantics: > > class aSingle: pass > > theSingle = aSingle() > > and use itsModule.theSingle as the singleton. > > Or you can implement it in java as a singleton, > and access it from from jython. > > There are probably at least a few more other ways. > > >Thanks > > Cheers, > Ype > > -- > > _______________________________________________ > Jython-users mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users > |