Skip to content

Instantly share code, notes, and snippets.

@timofurrer
Created October 28, 2013 07:47
Show Gist options
  • Save timofurrer/7192831 to your computer and use it in GitHub Desktop.
Save timofurrer/7192831 to your computer and use it in GitHub Desktop.
# -*- 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