[Pythonmac-SIG] lists and accessors

Ronald Oussoren ronaldoussoren at mac.com
Tue Jul 13 09:52:12 CEST 2010


On 11 Jul, 2010, at 1:18, Georg Seifert wrote:

> Hi Ronald,
> 
> Thanks for your response.
> 
> I did tried this:
> 
> 	class PathNodesProxy (object):
> 		def __init__(self, owner):
> 			self._ower = owner
> 		def __getitem__(self, i):
> 			print "__getitem__", i
> 			return self.nodeAtIndex_(i)
> 		def __setitem__(self, i, Node):
> 			print "__setitem__", i, Node
> 			self.setNode_atIndex_(Node, i)
> 		def __delitem__(self, i):
> 			print "__delitem__", i
> 			self.removeNodeAtIndex_(i)
> 		def append(self, Node):
> 			print "append", Node
> 			self.addNode_(Node)
> 		def count(self):
> 			print "count"
> 			return self.nodes().count()
> 
> 	del GSPath.__dict__['nodes']
> 	GSPath.__dict__['nodes'] = lambda self: PathNodesProxy(self)
> 
> the next to last line gives this error:
> 	TypeError: 'dictproxy' object does not support item deletion
> and the last (if I remove the first):
> 	TypeError: 'dictproxy' object does not support item assignment
> 
> If I used:
> 	GSPath.nodes = lambda self: PathNodesProxy(self)
> It worked somehow, but it actually removed the nodes property from the cocoa object. And I still had to use the parenthesis.

This should work:

     GSPath.nodes = property(lambda self: PathNodesProxy(self))

I've tested this with the count property on an array:

:>>> from Foundation import NSArray
:>>> b = NSArray.arrayWithArray_([1,2])
:>>> type(b).count = property(lambda self: id(self))
:>>> b.pyobjc_instanceMethods.count()
2
:>>> b.count
4359092688
:>>> 

(The convoluted way to change the count property is because NSArray is a class cluster and is not relevant for your problem)

Ronald

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3567 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/pythonmac-sig/attachments/20100713/36aa736a/attachment.bin>


More information about the Pythonmac-SIG mailing list