Created
February 7, 2020 06:26
-
-
Save arpitbbhayani/a9c33286f99a1d50ed4b3fa2a59cb79c 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
from inspect import getfullargspec | |
class Function(object): | |
"""Function is a wrap over standard python function. | |
""" | |
def __init__(self, fn): | |
self.fn = fn | |
def __call__(self, *args, **kwargs): | |
"""when invoked like a function it internally invokes | |
the wrapped function and returns the returned value. | |
""" | |
return self.fn(*args, **kwargs) | |
def key(self, args=None): | |
"""Returns the key that will uniquely identify | |
a function (even when it is overloaded). | |
""" | |
# if args not specified, extract the arguments from the | |
# function definition | |
if args is None: | |
args = getfullargspec(self.fn).args | |
return tuple([ | |
self.fn.__module__, | |
self.fn.__class__, | |
self.fn.__name__, | |
len(args or []), | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment