Comments (3)
Thanks for the comment! I am a little bit unsure what exact point you want to emphasize. Like you said, the whole list
is evaluated and the variable i
will therefore be 4
for the lambda function. When you do a _ = list(my_gen)
, isn't it the same as using the list in the first place then !?
from python_reference.
You currently say "This, however, does not apply to generators"; I'm pointing out that it does apply to generators - the functions returned by my_gen
are still all returning the variable from the closure.
You'll be fine as long as you only ever use the functions within the loop over the generator, but if you save any of them, you'll find that their values have changed after the end of the loop:
>>> my_gen = (lambda: n for n in range(5))
>>> for i,l in enumerate(my_gen):
... if i == 1: x = l # keep a reference to the 2nd function, which returned 1
... print(l())
...
0
1
2
3
4
>>> x() # the 2nd function now always returns 4
4
So even when you're using generators, you probably still want to use (lambda x=n: x for n in range(5))
.
from python_reference.
What I basically tried to say with the statement ("This, however, does not apply to generators") was that the variable inside the lambda-generator construct won't be set to the last value of the range object since the generator - in contrast to the list - was not evaluated completely (or "exhausted"/ "consumed"). But as we are progressing forward it will return the current state that the variable n
has in the closure.
I think I should change the sentences
[
"Since the list is already constructed when we for-loop through the list, it will be set to the last value 4."
...
"This, however, does not apply to generators"]
into something like
[
Since the list
comprehension has already been constructed and evaluated when we for-loop through the list, the closure-variable will be set to the last value 4.
However, by using a generator expression, we can make use of its stepwise evaluation (note that the returned variable still stems from the same closure, but the value changes as we iterate over the generator). ]
from python_reference.
Related Issues (16)
- Wrong iteration test HOT 1
- Workaround for HTML Conversion in Table of Contents HOT 1
- Sentence in Example 3.1 "Here is Why" ends prematurely HOT 1
- Modifying a list while looping through it HOT 1
- old
- Broken link in SQLite3_howto README HOT 1
- Printing database summary HOT 3
- Bad generated html HOT 1
- Sorting Algorithms IPython nb link is bad. HOT 1
- python_reference
- python-reference
- Use header cell. HOT 3
- More difference between range@py3 and xrange@py2 HOT 3
- %watermark addition. HOT 4
- PyBuilder HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python_reference.