How to do Obj-C Categories in Python?
<p>Obj-C (which I have not used for a long time) has something called <a href="http://macdevelopertips.com/objective-c/objective-c-categories.html" rel="nofollow">categories</a> to extend classes. Declaring a category with new methods and compiling it into your program, all instances of the class suddenly have the new methods.</p>

<p>Python has mixin possibilities, which I use, but mixins must be used from the bottom of the program: the class has to declare it itself.</p>

<p>Foreseen category use-case: Say you have a big class hierarchy that describe different ways of interacting with data, declaring polymorphic ways to get at different attributes. Now a category can help the consumer of these describing classes by implementing a convenient interface to access these methods in one place. (A category method could for example, try two different methods and return the first defined (non-None) return value.)</p>

<p>Any way to do this in Python?</p>

<h1>Illustrative code</h1>

<p>I <em>hope</em> this clarifies what I mean. The point is that the Category is like an aggregate interface, that the consumer of AppObj can change in <em>its</em> code.</p>

<pre><code>class AppObj (object):
  """This is the top of a big hierarchy of subclasses that describe different data"""
  def get_resource_name(self):
    pass
  def get_resource_location(self):
    pass

# dreaming up class decorator syntax
@category(AppObj)
class AppObjCategory (object):
  """this is a category on AppObj, not a subclass"""
  def get_resource(self):
    name = self.get_resource_name()
    if name:
      return library.load_resource_name(name)
    else:
      return library.load_resource(self.get_resource_location())
</code></pre>
