Skip to content

Instantly share code, notes, and snippets.

@juanarrivillaga
Created March 12, 2019 05:38
Show Gist options
  • Save juanarrivillaga/7c6428a70146ba951f8daa36f8b52bf1 to your computer and use it in GitHub Desktop.
Save juanarrivillaga/7c6428a70146ba951f8daa36f8b52bf1 to your computer and use it in GitHub Desktop.
import types
class Function:
def __init__(self, func):
self._func = func
def __call__(self, *args, **kwargs):
return self._func(*args, **kwargs)
def __get__(self, obj, objtype=None):
"Simulate func_descr_get() in Objects/funcobject.c https://docs.python.org/3/howto/descriptor.html#functions-and-methods"
if obj is None:
return self
else:
return types.MethodType(self, obj)
class Foo:
def __init__(self):
self.foo = 42
bar = Function(lambda self:
self.foo ** 2
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment