I’m trying to use functools.partial in a module
unfortunatly functools is not yet implemented in Skulpt
Attempting to recreate this functionality myself I created the function
def merge_two_dicts(x, y):
x.update(y)
return x
def partial(fun, *part_args, **part_kwargs):
def wrapper(*extra_args, **extra_kwargs):
args = list(part_args)
args.extend(extra_args)
kwargs = merge_two_dicts(part_kwargs, extra_kwargs)
return fun(*args, **kwargs)
return wrapper
Testing
test_fun = lambda x, y: x + y*2
fun = partial(fun, 1)
print(fun(2))
This code works fine in python3 on my local machine (giving 5).
In an anvil module this gives the error
UnboundLocalError: local variable ‘x’ referenced before assignment
at anvil-app-modules/Form1.py, line undefined
What is the best way to achieve this functionality