Use print in a Python2 lambda expression

background

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.

code

#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

Use print in a Python2 lambda expression
A python lambda expression ...
Use let expression in Python
Use config.ini in Python
Use dates in Python
Use Valgrind in Python
Use profiler in Python
Regular expression in Python
Regular expression in Python
Use a custom error page in python / tornado
Let's use def in python
Take a screenshot in Python
Use Measurement Protocol in Python
Create a function in Python
Create a dictionary in Python
[Python] Scraping in AWS Lambda
Use callback function in Python
Use parameter store in Python
[Python] Use a string sequence
Use HTTP cache in Python
Use MongoDB ODM in Python
Use list-keyed dict in Python
Use Random Forest in Python
Use regular expressions in Python
Use Spyder in Python IDE
Make a bookmarklet in Python
Use communicate () when receiving output in a Python subprocess
Draw a heart in Python
How to use Python lambda
How to use the __call__ method in a Python class
[AWS Lambda] Use any container Image in Python very simply
Print PDF in Python on Windows: Use an external application
AtCoder ABC168 A case expression solved in Ruby and Python
Write a binary search in Python
Use pymol as a python library
[python] Manage functions in a list
Hit a command in Python (Windows)
Use fabric as is in python (fabric3)
Create a DI Container in Python
Draw a scatterplot matrix in python
How to use SQLite in Python
Write AWS Lambda function in Python
ABC166 in Python A ~ C problem
Write A * (A-star) algorithm in Python
Create a binary file in Python
Use rospy with virtualenv in Python3
[Python] Make the function a lambda function
Use blender as a python module
Solve ABC036 A ~ C in Python
Write a pie chart in Python
Write a vim plugin in Python
How to use Mysql in python
Use Python in pyenv with NeoVim
Write a depth-first search in Python
How to use ChemSpider in Python
How to use PubChem in Python
Implementing a simple algorithm in Python 2
Create a Kubernetes Operator in Python
Use OpenCV with Python 3 in Window
Solve ABC037 A ~ C in Python
Run a simple algorithm in Python