tco
tco copied to clipboard
TCOs in functions with keywords
Hey,
I'm trying to make the accumulator argument, k
, in the simple factorial example implicit by using it as a keyword argument like so:
@with_continuations()
def factorial_kw(n, k=1, self=None):
return self(n-1, k=n*k) if n > 1 else k
print(factorial_kw(10))
However, this results in the following error:
Traceback (most recent call last):
File "./test.py", line 12, in <module>
print(factorial_kw(10))
File "/usr/local/lib/python3.6/dist-packages/tco/__init__.py", line 41, in __call__
return f(*args)
File "/usr/local/lib/python3.6/dist-packages/tco/__init__.py", line 86, in <lambda>
f(*args, self=kself, **dict(zip(keys, conts)))) (*k)
File "./test.py", line 11, in factorial_kw
return self(n-1, k=n*k) if n > 1 else k
TypeError: t() got an unexpected keyword argument 'k'
Not sure whether this is expected or if it's a bug, so I figured I'd submit an issue.
Hi, as stated in the title of your post, I am pretty sure the issue comes from the keyword argument. I think it was intended to be used with no keyword argument, but maybe you can achieve what you want by using the lower level C
function. I will try to see if it works.
Okay! Good to know that it's intended not to be used with keyword arguments. Maybe that should be stated somewhere.