Passing a function as an argument in Python I wanted to pass a lambda expression when writing a function, but in Python2 I couldn't put it in a lambda expression because print is a statement rather than an expression. did. Therefore, import the \ _ \ _ future__ module and change the print statement to a print expression as in Python3.
#Cannot be defined in Python2(Because print is a statement)
# a = lambda: print "hoge"
#Can be defined in Python 3(Because print is an expression)
a = lambda: print("hoge")
#How to define in Python2
from __future__ import print_function
a = lambda: print("hoge")
However, the problem is that you have to change all __print "hogehoge" to print ("hogehoge") __. When using it in Python2, it is better to limit the use such as importing it in the method.
appendix
Action<string> act = (string x) =>
{
if(x == "hoge") Console.WriteLine(x);
else Console.WriteLine("Except for hoge");
}
a = lambda: 5 #Lambda expression that returns 5
It seems that you can write with.
Recommended Posts