Lambda and multiple statements in Python
Question: Can anyone explaine that behavior of lambda functions?

    
    
    import sys
    X = lambda: sys.stdout.write('first');sys.stdout.write("second")
    X()
    

Returns: -> secondfirst

And one more problem:

    
    
    lambda: sys.stdout.write("...");sys.exit(0) 
    

Or

    
    
    lambda: sys.exit(0);sys.stdout.write("...")
    

Doesn't execute correctly. And one more question, why in first primer
execution flow goes from right to left?

Trying with: Python3+(3.4, 3.2) OS: Linux (Ubuntu), OSX


Answer: sys.stdout.write("second") is not part of the lambda.

'second' is always printed even if you don't call X. In other words calling X
only prints 'first'.

Your code can be rewritten as;

    
    
    import sys
    X = lambda: sys.stdout.write('first')
    sys.stdout.write("second")
    print X()
    

If you want two statements executed by the lambda place them in a tuple;

    
    
    lambda: (sys.stdout.write('first'),sys.stdout.write("second"))
    

