From: Bill Bumgarner <bbum@co...> - 2003-01-25 22:40:18
I committed a new FFI-only feature that allows the developer to append
methods to pre-existing classes.
This feature is almost like categories, but better in that you don't
need to have the class declaration and you can add a method to any
number of classes.
[bumbox:~/bbum-developer/sourceforge/pyobjc] bbum% python
Python 2.3a1 (#1, Jan 11 2003, 15:30:09)
[GCC 3.1 20020420 (prerelease)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from Foundation import *
>>> anObject = NSObject.new()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: No attribute doSomething
>>> class RandomClass(NSObject):
... def doSomething(self):
... print "doing something...."
>>> import objc
>>> objc.classAddMethods(NSObject, [RandomClass.doSomething])
It can also be used to override existing methods:
>>> class Foo(NSObject):
... def description(self): print "<new description>"
>>> objc.classAddMethods(NSObject, [Foo.description])
Things that don't work:
- can't cache the original implementation pointer. In the example
above, it would be ideal if one could do 'oldDescriptionMethod =
NSObject.description', then do the classAddMethods(), then be able to
call oldDescriptionMethod() from in the body of the new description
method's implementation (or anywhere else).
- you can't take the methods from a pure python class and add them
to some random ObjC class somewhere -- the method source class must
inherit from NSObject or some random subclass. There is a unit test
that fails because of this.
- be careful with signatures.... :-)
This lays the foundation for Category type implementation. From here,
implementation is a matter of convenience -- i.e. do we want to be able
to add all methods from class A into class B with a simple line of code?
If the first item in 'things that don't work' was fixed, then that
would lay the foundation for posing, as well.
>>>>> On Sat, 25 Jan 2003 17:40:03 -0500, Bill Bumgarner <bbum@...=
BB> This feature is almost like categories, but better in that you
BB> don't need to have the class declaration and you can add a
BB> method to any number of classes.
Yup! This is much like what GNUstep folks know as "behaviour", a
low-level functionality that's used for implementing much of the
Foundation "abstract" capabilities.
nickname: Lele Gaifax | Quando vivr=F2 di quello che ho pensato ieri
real: Emanuele Gaifas | comincer=F2 ad aver paura di chi mi copia.
email: lele@... | -- Fortunato Depero, 1929.