Re: [Pyobjc-dev] Recent change to K/V coding
Brought to you by:
ronaldoussoren
From: Ronald O. <ous...@ci...> - 2003-08-14 20:23:27
|
On Thursday, 14 August, 2003, at 21:49, b.bum wrote: > The only change I made was the addition of KeyValueCodingMixIn -- a > class in KeyValueCoding that can be mixed into any pure python class > to add valueForKey_(), takeValue_forKeyPath_(), etc... Useful, but > easy to duplicate, certainly. Why is this useful? If you want to feed the object to an Objective-C class/method that will use KVC to set/get attributes, the implementation of OC_PythonObject (which is used to wrap Python objects when accessed from Objective-C) already contains a full implementation of KVC. If you want use KVC from Python you can use the functions in PyObjCTools.KeyValueCoding or subclass from NSObject. In either case you don't need the mixin. And as I wrote earlier, the methods in the mixin will *never* be called from Objective-C, which might confuse users. > > The one change I would like to see-- that I feel is very important-- > is to fix the callable behavior such that it is inline with > Objective-C. The end result should be interchangeable with the > Objective-C implementation. The current implementation tries to conform to the description of the default behaviour of the KeyValueCoding protocol. There may be minor differences due to me not understanding the documentation. > > That is, if I start with a class implemented in Objective-C and port > it to Python or vice-versa, the behavior from the context of K/V > coding should be identical. Isn't it? The only difference between Objective-C and Python should be that the accessor method for reading an attribute is named 'get_attribute' instead of 'attribute'. This necessary due to a semantic difference between Python and Objective-C. In Objective-C attributes are in a different namespace from methods, in Python they are not. The common idom for "dumb" attributes in Objective-C seems to be: -myAttribute { return myAttribute } -getMyAttribute: value { [myAttribute autorelease]; myAttribute = [value retain]; } You cannot translate this into Python without at least some changes to the code, in Python you cannot have a method and instance variable named 'myAttribute'. I'd do it like this: def getMyAttribute(self): return self.myAttribute def setMyAttribute(self, value): self.myAttribute = value Ronald |