Re: [Pyobjc-dev] Inheritance questions....
Brought to you by:
ronaldoussoren
From: Bob I. <bo...@re...> - 2004-04-14 21:27:48
|
On Apr 14, 2004, at 4:59 PM, Jiva DeVoe wrote: > How do I call functions in a class which inherits from an objc class > *before* i have finished initializing self? My goal here is to call > some subfunctions in my constructor before calling the inherited > constructor. But self is not yet initialized, so it doesn't work... You don't. You want class methods. > So, say I have the following code in a python file: > > from Foundation import * > from AppKit import * from objc import selector > class BaseView(NSView): > def __new__(cls, *args, **kwargs): > return cls.alloc().init() Is this really a good idea? > def initWithFrame_(self, frame): > super(BaseView, self).initWithFrame_(frame) > return self self is allowed to change after an init call! def initWithFrame_(self, frame): self = super(BaseView, self).initWithFrame_(frame) return self > def initWithPrintInfo_(self, info): > self.info = info > > self.frame = self.getFrameFromPaperSize(info.papersize()) > > self.initWithFrame_(frame) > > return self Note that you did misspell paperSize ... def initWithPrintInfo_(self, info): frame = self.__class__.getFrameFromPaperSize_(info.paperSize()) self = self.initWithFrame_(frame) return self > def getFrameFromPaperSize(self, size): > return ((0, 0), (10, 10)) def getFrameFromPaperSize_(klass, size): return ((0, 0), (10, 10)) # this should technically have a signature that returns NSRect and takes NSSize. # I'll leave that as an exercise to the reader getFrameFromPaperSize_ = selector(getFrameFromPaperSize_, isClassMethod=True) > class Inherit1(BaseView): > def getFrameFromPaperSize(self, size): > return ((0, 0), (20, 20)) def getFrameFromPaperSize_(klass, size): return ((0, 0), (20, 20)) # the selector wrapper is not necessary here, it'll get picked up because we're subclassing |