pythonlambda

Empty Parameter for Python Lambda Function


Is it possible in python to write a lambda function that does not need any parameters passed to it? For instance, is there a way to translate this function:

def file_opener():
    f = open('filename.txt').read()
    return f

or any other function with no passed input into a lambda expression?


Solution

  • You certainly can do it..

    x = lambda : 6
    print(x())  # prints -> 6
    

    You probably shouldn't though. Once you feel the need to bind a function to a variable name you should go the long road instead (def ..) as you do it in your example.