Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I bind an existing instance method in one class to another class?

Tags:

python

I'm trying to do a limited form of dynamic mixin on a class, taking methods from a third-party library class and binding them to my class. But nothing I've tried has worked correctly. All examples I've seen elsewhere bind a unbound function to a class but, in my case, I need to bind an already bound method to a different class.

Some example code I've been working with, with my failed attempts included:

import types
import traceback

class Class1(object):
    output = 'class1'
    def method(self):
        print self.output

class Class2(object):
    output = 'class2'

try:
    Class2.method = types.MethodType( Class1.method, None, Class2 )
    class2 = Class2()
    class2.method()
except:
    traceback.print_exc() # TypeError: unbound method method() must be called with Class1 instance as first argument (got Class2 instance instead)

try:
    class1 = Class1()
    class2 = Class2()
    class2.method = types.MethodType( class1.method, class2, Class2 )
    class2.method()
except:
    traceback.print_exc() # TypeError: method() takes exactly 1 argument (2 given)

class1 = Class1()
class2 = Class2()
class2.method = class1.method.__get__( class2, Class2 )
class2.method() # outputs 'class1' not 'class2'

Is this possible? Am I doing something wrong? Is there another technique I haven't seen?

like image 559
Shane Breatnach Avatar asked Feb 24 '23 15:02

Shane Breatnach


1 Answers

Add it as a parent class dynamically, by modifying YourClass.__bases__:

>>> class Base: pass
>>> class Foo(Base): pass
>>> class Bar(Base): attr = True
>>> Foo.__bases__ = (Bar,) + Foo.__bases__
>>> Foo.attr
True

Alternatively, get the bound method object and extract the original function, then attach that as an attribute of your original class:

YourClass.method = OtherClass.bound_method.im_func

EDIT: you modify __bases__ not __mro__. Oops.

like image 184
Katriel Avatar answered Feb 26 '23 05:02

Katriel