Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Functional Python The Lambda Lambada Currying

Sahar Nasiri
Sahar Nasiri
7,454 Points

The else part

We already have this statement above why do we write it again? I mean this part:

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

We check this statement here and again we write it here:

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)))
Brady Huang
Brady Huang
16,360 Points

Having the same question as mine, the f(x, y, z) if (y is not None and z is not None) is redundant.

1 Answer

Haydar Al-Rikabi
Haydar Al-Rikabi
5,971 Points

You are right. The whole thing can be shortened as follows:

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)

    return lambda y, z=None: lambda z: f(x,y,z)