Created
October 28, 2013 07:47
-
-
Save timofurrer/7192831 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from types import FunctionType | |
def copy_func(f, name=None): | |
return FunctionType(f.func_code, f.func_globals, name or f.func_name, f.func_defaults, f.func_closure) | |
class Foo(object): | |
def method(self, arg): | |
self.value = arg | |
return self.value | |
class Bar(object): | |
def get_value(self): | |
return self.value | |
if __name__ == "__main__": | |
foo = Foo() | |
bar = Bar() | |
method = copy_func(Foo.method) | |
Bar.method = method | |
assert foo.method is not bar.method and bar.method is not method, "Method was not copied correctly" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment