[PYTHON] I want to bind a local variable with lambda

Not what I expected ...

>>> funcs = []
>>> for fruit in [ 'apple', 'orange', 'banana' ]:
...     funcs.append(lambda: 'I like ' + fruit)
... 
>>> for f in funcs:
...     print f()
... 
I like banana
I like banana
I like banana

Python's lambda resolves the name of the variable at the time of the call, so it becomes "fruit" (='banana') at the time of execution.

Solution

Change lambda:'I like' + fruit to lambda fruit = fruit:'I like' + fruit.

I think that it is standard to solve functionally with higher-order functions, but if it is ↑, it is good to go with one-liner. (^ ◇ ^)

>>> funcs = []
>>> for fruit in [ 'apple', 'orange', 'banana' ]:
...     funcs.append(lambda fruit=fruit: 'I like ' + fruit)
... 
>>> for f in funcs:
...     print f()
... 
I like apple
I like orange
I like banana

Recommended Posts

I want to bind a local variable with lambda
I want to write to a file with Python
I want to embed a variable in a Python string
I want to transition with a button in flask
I want to climb a mountain with reinforcement learning
I want to work with a robot in python.
I want to split a character string with hiragana
I want to AWS Lambda with Python on Mac!
I want to manually create a legend with matplotlib
I want to run a quantum computer with Python
I want to do ○○ with Pandas
I want to debug with Python
I want to make a blog editor with django admin
I want to start a jupyter environment with one command
I want to make a click macro with pyautogui (desire)
I want to use a virtual environment with jupyter notebook!
I want to install a package from requirements.txt with poetry
[Visualization] I want to draw a beautiful graph with Plotly
I want to use a wildcard that I want to shell with Python remove
I want to print in a comprehension
I want to do a full text search with elasticsearch + python
I want to analyze logs with Python
I want to play with aws with python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
I want to make matplotlib a dark theme
I want to easily create a Noise Model
I want to use MATLAB feval with python
I want to analyze songs with Spotify API 2
I want to INSERT a DataFrame into MSSQL
I want to mock datetime.datetime.now () even with pytest!
I want to display multiple images with matplotlib.
I want to knock 100 data sciences with Colaboratory
I want to be an OREMO with setParam!
I want to analyze songs with Spotify API 1
I want to use Temporary Directory with Python2
I don't want to use -inf with np.log
#Unresolved I want to compile gobject-introspection with Python3
I want to create a plug-in type implementation
I want to use ip vrf with SONiC
I want to solve APG4b with Python (Chapter 2)
I want to start over with Django's Migrate
I want to easily find a delicious restaurant
I want to upload a Django app to heroku
I want to display only different lines of a text file with diff
I want to write an element to a file with numpy and check it.
When I connect to a remote Jupyter Server with VScode, it's remote but local
I want to convert an image to WebP with lollipop
I want to easily implement a timeout in python
I want to iterate a Python generator many times
I want DQN Puniki to hit a home run
I want to detect unauthorized login to facebook with Jubatus (1)
100 image processing knocks !! (021-030) I want to take a break ...
I want to give a group_id to a pandas data frame
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to handle optimization with python and cplex
I tried to draw a route map with Python
I want to write in Python! (2) Let's write a test
I want to find a popular package on PyPi
I want to randomly sample a file in Python
I want to inherit to the back with python dataclass