Created
March 12, 2019 05:38
-
-
Save juanarrivillaga/7c6428a70146ba951f8daa36f8b52bf1 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
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