Skip to content

Instantly share code, notes, and snippets.

@creallfluharty
Last active January 9, 2020 04:15
Show Gist options
  • Save creallfluharty/b9b24cca3a3e7f940494262fa38b5bc2 to your computer and use it in GitHub Desktop.
Save creallfluharty/b9b24cca3a3e7f940494262fa38b5bc2 to your computer and use it in GitHub Desktop.
Toy parameterized.expand
def expand(call_args_list):
""" Replaces method m with (completed) partials m_0(*call_args[0]), m_1(*call_args[1]), ... m_{n}(*call_args[n])
ie
```
class TestX:
@expand([
['a', 1],
['b', 2],
])
def asdf(letter, number):
print(letter, number)
```
Functionally becomes:
```
class TestX:
def asdf_0():
print('a', 1)
def asdf_1():
print('b', 2)
```
"""
class decorator:
def __init__(self, func):
self.func = func
def __set_name__(self, obj, name):
for i, call_args in enumerate(call_args_list):
def f():
return self.func(*call_args)
setattr(obj, f'{name}_{i}', f)
delattr(obj, name)
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment