Re: [Pyobjc-dev] Key/Value Coding
Brought to you by:
ronaldoussoren
From: Pierce T. W. I. <pi...@tw...> - 2003-08-14 16:51:11
|
On Tuesday, August 12, 2003, at 09:41 PM, b.bum wrote: > I committed a new Key/Value coding unit test and a bit of additional > functionality in the KeyValueCoding module within PyObjCTools. It > includes a compiled module. > > The unit tests do not pass and are currently, more or less, a feature > request. > > The goal is to make PyObjC's key/value coding support compliant with > ObjC to the point where Python, ObjC or mixed PyObjC instances can be > transparently passed into K/V dependent functionality. We need this > to fully support the increasing emphasis on Key/Value Coding found > within Mac OS X. As a WO hacker, I love KVC, so having KVC support would be cool and possibly should be a python feature separate from pyobjc. I forsee some problems on the Python side because python doesn't have separate name spaces based on type. That is: class.method.method.value is not the same thing as: class.method().method().value For instance, being new to python, but used to ObjC I tried to code: class example: def __init__(self): self.instancevar=1 def instancevar(self) return self.instancevar Which doesn't work, because you can't have an instance variable and a class method with the same name, because they're stored in the same dictionary. Instead: class example: def __init__(self): self.instancevar=1 def getInstancevar(self) return self.instancevar works because it avoids the name collision. So KVC can't use the same rule set on both Python and ObjC. In fact, that's why your first test is failing, which you can see if you change the error message from: raise KeyError, "Key %s does not exist"%(key,) to raise KeyError, "Key %s does not exist on obj %s"%(key,obj) KeyError: 'Key indirectString does not exist on obj <bound method KVPyPath.indirectHead of <__main__.KVPyPath instance at 0x1e6170>>' directHead.indirectString returns the indirectString method, not the instance variable, so then the recursion breaks. So getKey() needs to be tweaked such that it looks at the result and if the result is callable, then it returns attribute(), otherwise it just returns the attribute. However, you're officially at the frontier of my python knowledge. Pierce P.S. I think Python people might find KVC confusing, since KVC would have a very similar but different syntax to python. However, I think the way to sell it to them is that its useful with config files and other tools where () is in appropriate: config.subsystem.setting = 1 The config file doesn't need to know what's under config, subsystem, or setting, and any or all of these can be instance variables instead of functions. |