Re: [Pyobjc-dev] PyObjC equivalent of ObjC's categories?
Brought to you by:
ronaldoussoren
From: Bob I. <bo...@re...> - 2003-10-26 18:11:17
|
On Oct 26, 2003, at 9:17 AM, Jacob Kaplan-Moss wrote: > For me, one of the most powerful tools that ObjC gives is the ability > to create "categories" on existing classes. This allows extending > existing classes without actually needing to subclass them. For > example (taken from Apple's AnimatedSlider sample code): > > @interface NSSlider (SliderAnimation) > > - (IBAction) animateToFloatValueFrom: sender; > - (void) animateToFloatValue:(float) newValue; > > @end > > The above adds two instance methods to the NSSlider used in the > application. > > How would I go about doing the same thing in PyObjC? Obviously I > could subclass NSSlider to add the new methods, but I guess I'm > looking for something that's a closer functional match to what the > ObjC code does. from the objc module: Help on built-in function classAddMethods: classAddMethods(...) classAddMethods(targetClass, methodsArray) Adds methods in methodsArray to class. The effect is similar to how categories work. If class already implements a method as defined in methodsArray, the original implementation will be replaced by the implementation from methodsArray. I don't believe this is very well known, I had the same question as you yesterday, but I found the answer by reading through the PyObjC source code. -bob |