Re: Python lambda is useless ^ H ^ H ^ H ^ H ^ H Difficult to use

In response to @ buchio's article Python lambda is useless ^ H ^ H ^ H ^ H ^ H difficult to use to watch.

Why this happened

It is easy to understand if you think that the area of local variables is reserved behind them. What you're referring to in lambda is the area, not the value of the variable (visible at the time of definition). In other words, test1 in the original article is equivalent to the following code.

>>> def test1_aux():
...     env = [None]
...     funcs = []
...     for i in range(10):
...         env[0] = i
...         funcs.append(lambda a: a+env[0])
...     return funcs
... 
>>> print [f(10) for f in test1_aux()]
[19, 19, 19, 19, 19, 19, 19, 19, 19, 19]

The fact that the scope of the variable i is not limited to the inside of the for statement is certainly a bit confusing, but I don't think there is anything strange about it other than that.

By the way, for list comprehensions that are closely related to the for statement, variables can no longer be referenced from the outer scope from Python 3.

python2


>>> [x for x in range(3)]
[0, 1, 2]
>>> x
2

python3


>>> [x for x in range(3)]
[0, 1, 2]
>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined

What should i do

In Python, you can call it by implementing the \ _ \ _ call \ _ \ _ method in any class. So you can use objects where you need closures.

>>> class Adder(object):
...     def __init__(self, i):
...         self.i = i
...     def __call__(self, a):
...         return a + self.i
... 
>>> def test_callable():
...     funcs = []
...     for i in range(10):
...         funcs.append(Adder(i))
...     return funcs
... 
>>> print [f(10) for f in test_callable()]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

In Python, it's more readable and simpler to use functions and objects, so it's more Pythonic to not use lambdas except for simple expressions.

Recommended Posts

Re: Python lambda is useless ^ H ^ H ^ H ^ H ^ H Difficult to use
How to use Python lambda
[Road to intermediate Python] Use lambda expressions
How to use is and == in Python
Summary of studying Python to use AWS Lambda
Creating a Python document generation tool because it is difficult to use sphinx
python3: How to use bottle (2)
[Python] How to use list 1
Python is painful. But use
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
[Python] How to use input ()
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
[Python] I want to use the -h option with argparse
[Introduction to Udemy Python 3 + Application] 58. Lambda
Python: How to use async with
Golang is difficult to translate. .. .. (Part 1)
Use fabric as is in python (fabric3)
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
[Lambda] [Python] Post to Twitter from Lambda!
Basic grammar of Python3 system (how to use functions, closures, lambda functions)
How to use the asterisk (*) in Python. Maybe this is all? ..
[Introduction to Python] Let's use pandas
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
[Introduction to Python] Let's use pandas
How to use python zip function
Use PostgreSQL with Lambda (Python + psycopg2)
[Introduction to Python] Let's use pandas
[Python] How to use Typetalk API
When you want to use it as it is when using it with lambda memo
It is convenient to use Layers when putting a library on Lambda
[Python] Summary of how to use pandas
[Introduction to Python] How to use class in Python?
Use print in a Python2 lambda expression
Is it deprecated to use pip directly?
[Road to intermediate Python] Use ternary operators
How to install and use pandas_datareader [Python]
[Pandas] What is set_option [How to use]
[python] How to use __command__, function explanation
I want to use jar from python
[Introduction to Python] Let's use foreach with Python
[Python] How to use import sys sys.argv
Easy way to use Wikipedia in Python
[Python] Organizing how to use for statements
Lambda function to take AMI backup (python)
python: How to use locals () and globals ()
Install python on xserver to use pip
How to use "deque" for Python data
How to use Python zip and enumerate
[Python] Understand how to use recursive functions