Is there any way how to code recursive function containing if with lamba? Is possible to rewrite this using lambda.
def factorial(x):
if x <= 1:
return 1
return x * factorial(x - 1)
print(factorial(5))
Is there any way how to code recursive function containing if with lamba? Is possible to rewrite this using lambda.
def factorial(x):
if x <= 1:
return 1
return x * factorial(x - 1)
print(factorial(5))
No, or at least not without assigning the function to a variable and then using that name to call it. But you still cannot use any statements in the function.
In any case, don't do that. A lambda function is exactly like a normal def function in Python besides the restriction that it cannot contain statements.
The only reason to use a lambda in Python is to avoid defining an named function if you just need a simple callable returning some value.
>>> fa= lambda x :1 if x <= 1 else x*fa(x-1) ?