Re: [Pyobjc-dev] Headache over hacking an XML editor
Brought to you by:
ronaldoussoren
From: <bb...@ma...> - 2003-01-14 19:12:44
|
On Tuesday, Jan 14, 2003, at 13:57 US/Eastern, Just van Rossum wrote: > Ugh, this is depressing :-(. (So you _can_ find out the return type of > a > method at runtime but not whether it returns a new or a borrowed > reference. Great.) Correct. There is no concept of 'new vs. borrowed' at anything but the object level. This isn't a problem from the Objective-C side of the fence -- it is only a problem in that Python and ObjC take different approaches to solving the memory management problem. I like Python's a lot better, but I have to live with ObjC's because I'm leveraging ObjC frameworks-- even when writing PyObJC code. > Here's a quick idea to work around the most common (I think) > irritation: > instead of raising an exception when calling a (subclass of) NSObject > ("TypeError: Use class methods to instantiate new Objective-C objects") > have objc-object.m::object_new() do this: > > self = cls.alloc() > self.release() # ownership transferred to the Python wrapper > return self This code is lacking the call to the -init method -- problematic. What about arguments to the constructor? We can handle the no argument case transparently, but what about-- say-- NSView's -initWithFrame: designated initializer? Also, keep in mind that -init* methods return an object reference for a reason; a number of intializers return a different instance than the one that was called for a number of [valid] reasons. > This doesn't touch the alloc() semantics, yet provides a much more > Pythonic way to create instances. Heck, this will even call __init__() > for you! ;-) But __init__() is not generally the designated intiailizer for ObjC classes-- even Python implemented subclasses of ObjC classes. When subclassing ObjC from Python, it makes more sense to follow the ObjC semantics than the Python semantics -- at least, it does to me. > What I still don't understand is how pyobjc manages the refcount of > Python objects passed to objc: the Python object can't have a reference > to it, so when will the wrapper be released? Is it an autorelease > object? Ronald can better answer this question. b.bum |