Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python lambda return value

I’m learning the lambda options to return in python and i have a question:

I need to fill the returns in this function:

def func(n):
   if n==0:
      print("finished")
   else:
      return ___
func(5)()()()()()
func(3)()()()
func(8)()()()()()()()()

The output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

finished
finished
finished

I thought this one is a recursive call like return func(n-1) but it doesn’t work, and throws an error.
Is there an option to overcome the extra empty brackets? count them? do something, because it should be runnable.

Thanks

>Solution :

You’re right about needing to use lambdas and func n-1, specifically

return lambda: func(n-1)

This returns a lambda that doesn’t need any parameters passed in, to handle the brackets, and the return of the is the function being called with n-1, which in most calls you’re making, is returning the next lambda function call

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading