Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Functional Python The Lambda Lambada Currying

Roger Perelló
Roger Perelló
3,713 Points

Help with the last return lambda inside curried_f

Hi! I understand that this first part of the function in the video comes into effect for some combinations of arguments (if y and z passed, if only z is not passed) when curried_f is called:

def curried_f(x, y=None, z=None):
    def f(x, y, z):
        return x**3 + y**2 + z

    if y is not None and z is not None:
        return f(x, y, z)
    if y is not None:
        return lambda z: f(x, y, z)

I also assume that the next part takes care of the other possible combinations (if neither z nor y are passed, if y is not passed):

    return lambda y, z=None: (
        f(x, y, z) if (y is not None and z is not None)
        else (lambda z: f(x, y, z)))

However, what happens in there doesn't seem to translate into that. The "if (y is not None and z is not None)" and "(lambda z: f(x, y, z))" both look like they refer to combinations of arguments already tackled in the first part (respectively: if y and z passed, if only z is not passed). How does that last part work to achieve "if neither z nor y are passed, if y is not passed"?

2 Answers

Steven Parker
Steven Parker
215,940 Points

You're confusing the conditions of the returned lambda with the conditions of curried_f itself.

That final "return" gives you another function (a lambda) that will give you the result if called with 2 arguments, but give you yet another lambda if called with only one.

Consider what happens in each of the 4 possible usage cases to get a final value:

curried_f(2, 3, 4))  # first return
curried_f(2, 3)(4))  # second return
curried_f(2)(3, 4))  # third return, then first case
curried_f(2)(3)(4))  # third return, then second case
Roger Perelló
Roger Perelló
3,713 Points

I think I get it. So that last return is for people who only passed in X. It would say something like: "I transform the caller into a function that requires 'Y' and accepts 'Z', which simply returns the final result if both are given. If this is not the case, and I only get 'Y', I change the caller to a function that needs 'Z'." Thanks!