On Oct 26, 2003, at 6: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):
Categories can also lead to extremely hard to maintain code because
they effectively break encapsulation within your code base. In the 15
years (jeez!) that I have been doing ObjC-- often by acting as a
consultant to help others fix/maintain/extend their codebase-- abuse of
categories has often been the source of serious pain and consternation
(and billable hours). Effectively categories allow you to use a class
design where your functionality is not "below" the existing classes,
but is effectively inserted throughout.
Powerful? Yes. Extremely. Wonderfully so. Use them with care.
PyObjC fully supports categories, but in slightly different-- and even
more dangerous-- fashion. Effectively you can stick any method-- from
objc or python-- onto any class.
>>> import objc
classAddMethods takes an array of methods and sticks 'em on a class
somewhere. classAddMethod allows you to specify the selector used
when the method is installed. I.e. you could do:
def doFoo:(self, arg1):
objc.classAddMethod(Foundation.NSBundle, "handleRequest:", doFoo)
Like categories, the method will be immediately available across all
instances -- preexisting or newly allocated.