[three]Bean
dynamically reassigning methods from one class to another in python (dunder func, who knew?)
Jan 27, 2011 | categories: python View CommentsI don't know if you've ever wanted to take a method from one class and attach it to another. I never knew I'd wanted to, but it turns out I did.
#!/usr/bin/python class Foo(object): def __init__(self, id): self.id = id def bar(self): print self.id class Oof(object): def __init__(self, id): self.id = id # Wow Oof.bar = Foo.bar.__func__ Foo(52).bar() # prints 52 Oof(10).bar() # prints 10