Giter Club home page Giter Club logo

Comments (3)

rasbt avatar rasbt commented on May 24, 2024

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.

dplepage avatar dplepage commented on May 24, 2024

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.

rasbt avatar rasbt commented on May 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.