Python (2.7): dynamically adding classmethods
<p>I have some classes whose methods which I want to be able to add to other classes on optionally. My initial solution was to use mixins, but this can get a bit ugly:</p>

<pre><code>class Schedule(Enumerator, Humanizer, Inferer, ...):
    ...
</code></pre>

<p>So I thought hey, perhaps I could use class decorators to achieve the same effect.</p>

<pre><code>@enumerator
@humanizer
@inferer
class Schedule(object):
    ...
</code></pre>

<p>And here is a sample of the decorator functions:</p>

<pre><code>import inspect

def inferer(original_class):
    from myproj.lib.inferer import Inferer
    methods = inspect.getmembers(Inferer, predicate=inspect.ismethod)
    for method in methods:
        setattr(original_class, method[0], types.MethodTypes(method[1], original_class))
    return original_class
</code></pre>

<p>...which seems to add the methods and classmethods appropriately to decorated classes. However, when I call one of these added methods (or classmethods) on the decorated class, I get some errors.</p>

<p>For methods:</p>

<pre><code>&gt;&gt;&gt; Schedule().humanize()
TypeError: unbound method humanize() must be called with Humanizer instance as first argument (got type instance instead)
</code></pre>

<p>...which seems to indicate these are added as classmethods?</p>

<p>For classmethods:</p>

<pre><code>&gt;&gt;&gt; schedule = Schedule.infer(foo)
TypeError: infer() takes exactly 2 arguments (3 given)
</code></pre>

<p>Note the definition of infer:</p>

<pre><code>class Inferer(object):
    @classmethod
    def infer(cls, dates):
        ...
</code></pre>

<p>I added some lines to <code>infer</code> to show what arguments it's getting when called as <code>Schedule.infer()</code>:</p>

<pre><code>cls: &lt;class 'myproj.lib.inferer.Inferer'&gt;
dates: &lt;class 'myproj.Schedule'&gt;
</code></pre>

<hr>

<p>So, my question:</p>

<p>What is going wrong in the decorator function to cause these added methods and classmethods to behave strangely? Or, better put, how to I modify the decorator function to handle these additions properly?</p>

<p>Please let me know if I can provide any clarification on any point.</p>
