[Pyobjc-dev] Structs, tuples and instances
Brought to you by:
ronaldoussoren
From: Dinu G. <gh...@da...> - 2003-10-30 09:35:57
|
Hi, I'm trying to fake some kind of a solution which is more struct-like than plain Python tuples for things like NSPoint, NSSize, etc. Here's a simple one: class NSPoint: """An experimental NSPoint class/struct hybrid. This is supposed to be usable with attribute names as well as indices. """ def __init__(self, x, y): self.x, self.y = x, y def __getitem__(self, index): assert index in (0, 1) return (self.x, self.y)[index] def __setitem__(self, index, value): assert index in (0, 1) setattr(self, "xy"[index], value) Doing anything like the following: view = NSView.alloc().init() #origin = (10, 20) origin = NSPoint(10, 20) origin.x = 11 origin[1] = 22 print origin[0], origin[1] print origin.x, origin.y view.setFrameOrigin_(origin) then gives me a "ValueError: depythonifying 'struct', got 'instance'" on the last line, so I assume the background checks test for the type of the argument and reject an instance. What about testing only for __getitem__ and __len__? Then, I could probably really use the type of hack above, which would make a cer- tain kind of code much more readable? Dinu -- Dinu C. Gherman - http://python.net/~gherman ...................................................................... "I never apologize for the United States of America, I don't care what the facts are." (George Bush, Sr.) |