Re: [Pyobjc-dev] Bug in objc.synthesize
Brought to you by:
ronaldoussoren
From: Ronald O. <ron...@ma...> - 2009-07-13 19:09:12
|
W.r.t. objc.synthesize: I've been experimenting with Python's "property" function as a decorator. In Python 2.6 you can do something like this: class MyClass (object): @property def stringValue(self): return self._value @stringValue.setter def setStringValue(self, value): self._value = value It should be doable to do something simular for objc properties, such as: class MyController (NSObject): stringValue = objc.property(rw=True, copy=True, ivar='_stringValue') @stringValue.getter def get(self): return self._stringValue objc.property would define an ivar and getter and setter methods. Those methods would only be visible on the ObjC side, Python users always access the property using regular attribute access (the bridge would also ensure that the correct selectors are used for the accessor methods, regardless of the name used in the Python code). With some effort this could be extended to add other KVC/KVO related methods where needed, such as an objc.set_property that uses an NSMutableSet as the backing storage and ensure that manipulation of the set is done using the right KVC accessor methods. As sketched above this would need some support in the C code, simularly to how objc.ivar is handled. I do think this could be a useful addition to PyObjC, and will try to find some time to write a proper specification and testsuite for this (followed by an implementation once we're happy about the first two). The scary future scenario for this is to expose existing ObjC properties using the same mechanism. This is "scary" because doing this is incompatible with existing code, and hence something I'd only implement once the basic support is stable (and included in a release). With some luck lib2to3 (the python2.x to 3.x translation tool) can be coaxed into translating the current syntax (aField.setStringValue_(v)) to the new syntax (aField.stringValue = v), but that's something I'll look into when its needed. Ronald |