python – Currying a function

The goal is to have a function that can transform any python function into a curry version of this function.

To be precise, consider this code:

def f (x, y, z, info = none):
if info:
print (info, end = ":")
returns x + y + z

g = curry (f)
print ("g (2, 3, 4) =", g (2, 3, 4))
print ("g (2, 3) (4) =", g (2, 3) (4))
print ("g (2) (3) (4) =", g (2) (3) (4))
print ("g (2) (3, 4) =" g (2) (3, 4))
print (g (2, info = "test A") (3, 4))
print (g (2, info = "test A") (3, 4, info = "test B"))
print (g (2,3,4) (5))

The goal is to have the following output:

g (2, 3, 4) = 9
g (2, 3) (4) = 9
g (2) (3) (4) = 9
g (2) (3, 4) = 9
test A: 9
test B: 9

And throw an exception on the last line.

My current implementation of curry is the following, I just wonder if this is as simple as possible.

curry class:
def __init __ (auto, function):
self._partial = functools.partial (function)

def __call __ (self, * args, ** kwargs):
partial = functools.partial (self._partial, * args, ** kwargs)
sign = signature (partial.func)
try:
sign.bind (* partial.args, ** partial.keywords)
output = partial ()
return from exit
except TypeError as e:
return Curry (copy (partial))

def __str __ (auto):
text = "partial:"
text + = str (self._partial.func .__ name__)
text + = str (self._partial.args)
text + = str (signature (self._partial))
return text

I added a __str__ This method is useful when you want to debug with partials. for example print (g (2,3)) impressions:

partial: f (2, 3) (z, info = None)

Any suggestion to improve this is welcome.