Skip to content

Instantly share code, notes, and snippets.

@arpitbbhayani
Created February 7, 2020 06:26
Show Gist options
  • Save arpitbbhayani/a9c33286f99a1d50ed4b3fa2a59cb79c to your computer and use it in GitHub Desktop.
Save arpitbbhayani/a9c33286f99a1d50ed4b3fa2a59cb79c to your computer and use it in GitHub Desktop.
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