Re: [Pyobjc-dev] PyObjC Singleton
Brought to you by:
ronaldoussoren
From: Ronald O. <ron...@ma...> - 2007-01-30 20:49:30
|
On 30 Jan, 2007, at 21:28, Koen Bok wrote: > That's fast! Here you go! > > Please note I put the full python path in setup.py as it was acting > weird on my system. > > <Singleton.zip> > The Singleton in the zip is broken, as is the one below now that I look at it again. The init method: def init(self): cls = self.__class__ if not cls._instance: cls._instance = super(cls, self).init() return cls._instance This basically calls 'super(self.__class__, self).init()', which is wrong. You should also use the literal class there instead of deriving it from the type of self (this b.t.w. has nothing to do with PyObjC). Changing the init method to the one below solves your problem: def init(self): if not Singleton._instance: Singleton._instance = super(Singleton, self).init() return Singleton._instance This means you'll have replicate the init method if you want to subclass Singleton, but that is questionable behaviour anyway. Ronald > On 30-jan-2007, at 21:21, Ronald Oussoren wrote: > >> >> On 30 Jan, 2007, at 21:11, Koen Bok wrote: >> >>> Hi there, >>> >>> I am trying to make a Singleton class to work with PyObjC for my >>> controller classes. I wrote a little text case that works fine, but >>> when I try to use it in combination with nibs I get a recursive >>> error. >>> >>> Hint... anyone? >> >> The code below seems to be have as expected, creating three >> references to the singleton. Please create an application that >> completely demonstrates the problem. >> >> Ronald >> >>> >>> from Foundation import NSObject >>> >>> class Singleton(NSObject): >>> >>> _instance = None >>> >>> def init(self): >>> cls = self.__class__ >>> if not cls._instance: >>> cls._instance = super(cls, self).init() >>> return cls._instance >>> >>> @classmethod >>> def sharedInstance(cls): >>> return cls.new() >>> >>> a = Singleton.alloc().init() >>> b = Singleton.sharedInstance() >>> c = Singleton.new() >>> >>> print a == b == c >>> >>> -------------------------------------------------------------------- >>> -- >>> --- >>> Take Surveys. Earn Cash. Influence the Future of IT >>> Join SourceForge.net's Techsay panel and you'll get the chance to >>> share your >>> opinions on IT & business topics through brief surveys - and earn >>> cash >>> http://www.techsay.com/default.php? >>> page=join.php&p=sourceforge&CID=DEVDEV >>> _______________________________________________ >>> Pyobjc-dev mailing list >>> Pyo...@li... >>> https://lists.sourceforge.net/lists/listinfo/pyobjc-dev >> >> >> --------------------------------------------------------------------- >> ---- >> Take Surveys. Earn Cash. Influence the Future of IT >> Join SourceForge.net's Techsay panel and you'll get the chance to >> share your >> opinions on IT & business topics through brief surveys - and earn >> cash >> http://www.techsay.com/default.php? >> page=join.php&p=sourceforge&CID=DEVDEV >> _______________________________________________ >> Pyobjc-dev mailing list >> Pyo...@li... >> https://lists.sourceforge.net/lists/listinfo/pyobjc-dev > |