I’m curious how folks react to this; it appears to exactly hit the goals of this idea, while also being clear that you intend to use particular variables with a specific function and protecting against spelling errors. It could use a little more intelligence when interleaving positional and keyword args.
class 'Partial' or 'Closure' or 'SimpleArgspace' or ...
from typing import Callable
import inspect
class Partial:
def __init__(self, func: Callable):
self.__dict__['_func'] = func
self.__dict__['_kwargs'] = inspect.getargs(func.__code__).args
self.__dict__['_argvals'] = {}
def __setattr__(self, name: str, value):
if name in self._kwargs:
self.__dict__['_argvals'][name] = value
else:
raise TypeError(f'"{name}" not an argument for function "{self._func.__name__}"')
def __call__(self, *args, **kwargs):
return self._func(*args, **(self._argvals | kwargs))
def f(*,x,y,z):
return x*y*z
p = Partial(f)
p.x = 1
p.y = 2
print(p(z=3))