Timeline for What does the "yield" keyword do in Python?
Current License: CC BY-SA 4.0
96 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jun 18, 2024 at 23:30 | comment | added | user129393192 | @JacobWard on my python implementation, it works even when you do return <value> | |
| May 17, 2024 at 5:08 | comment | added | Zhou Haibo | ``` @pytest.fixture(autouse=True) def setup_page_scenarios(home_page: ProgramHomePage) -> Iterator[None]: home_page.restart_app() yield ``` This function is called automatically(autouse=True), but the code inside it runs which again “When you call a function that contains a yield statement anywhere, you get a generator object, but no code runs. ”, could anyone explain about this? | |
| Mar 24, 2024 at 4:17 | history | edited | Mateen Ulhaq | CC BY-SA 4.0 | Add documentation links. |
| Aug 10, 2023 at 4:26 | history | edited | Michael M. | CC BY-SA 4.0 | minor grammatical/formatting changes for clarification and correctness |
| May 18, 2023 at 20:39 | review | Suggested edits | |||
| May 19, 2023 at 21:42 | |||||
| S Oct 1, 2022 at 18:08 | history | suggested | Saket Thakur | CC BY-SA 4.0 | fixed grammar |
| Oct 1, 2022 at 9:44 | review | Suggested edits | |||
| S Oct 1, 2022 at 18:08 | |||||
| Sep 5, 2022 at 9:12 | comment | added | Karl Knechtel | Interestingly, at least two different people used the Bank example from this answer... to ask duplicate questions about the meaning/necessity of self. | |
| Aug 15, 2022 at 11:35 | comment | added | Parisa Khateri | Where yield is defined: >>> def create_generator(): ... mylist = range(3) should be: >>> def create_generator(): ... mylist = (x*x for x in range(3)) instead. | |
| Dec 29, 2021 at 16:10 | comment | added | chepner | @Zaffer Basically. Each call to the function creates a new generator object based on the generator function which can be iterated over independently. I sometimes wish there was a separate keyword like codef to define such functions, so that yield in a def would be a syntax error. (It used to be a syntax error to use both return and yield in the same function, but then return was allowed as a shortcut for raising StopIteration.) | |
| Jul 17, 2021 at 16:40 | comment | added | Zaffer | So yield anywhere in a function makes the function a generator, which is basically an iterable of yields from a function? | |
| Mar 7, 2021 at 12:59 | history | edited | Georg Plaz | CC BY-SA 4.0 | function names should be snake case https://www.python.org/dev/peps/pep-0008/#:~:text=Use%20the%20function%20naming%20rules,invoke%20Python's%20name%20mangling%20rules. |
| S Jan 27, 2021 at 17:28 | history | suggested | jakubde | CC BY-SA 4.0 | Replaced dead link with a wayback machine variant |
| Jan 27, 2021 at 13:09 | review | Suggested edits | |||
| S Jan 27, 2021 at 17:28 | |||||
| Dec 3, 2020 at 1:23 | comment | added | Jacob Ward | The yield statement suspends function’s execution and sends a value back to the caller, but retains enough state to enable function to resume where it is left off. When resumed, the function continues execution immediately after the last yield run. This allows its code to produce a series of values over time, rather than computing them at once and sending them back like a list. | |
| Sep 26, 2020 at 19:06 | review | Suggested edits | |||
| Sep 26, 2020 at 20:19 | |||||
| Jun 6, 2020 at 6:03 | comment | added | alani | @MatthiasFripp "This continues until the function runs off the end" -- or it encounters a return statement. (return is permitted in a function containing yield, provided that it does not specify a return value.) | |
| May 7, 2020 at 10:12 | comment | added | WoJ | It would be nice to add to this great answer why It is just the same except you used () instead of [], specifically what () is (there may be confusion with a tuple). | |
| S Mar 5, 2020 at 0:39 | history | edited | Błażej Michalik | CC BY-SA 4.0 | reworded for clarity (distinguish between running a loop and running an iteration of a loop) |
| S Mar 5, 2020 at 0:39 | history | suggested | Alan | CC BY-SA 4.0 | reworded for clarity (distinguish between running a loop and running an iteration of a loop) |
| Mar 4, 2020 at 21:40 | review | Suggested edits | |||
| S Mar 5, 2020 at 0:39 | |||||
| Dec 29, 2019 at 11:09 | history | bounty awarded | U13-Forward | ||
| Nov 13, 2019 at 9:59 | review | Suggested edits | |||
| Nov 13, 2019 at 10:11 | |||||
| Sep 23, 2019 at 8:06 | review | Suggested edits | |||
| Sep 23, 2019 at 10:16 | |||||
| S Sep 1, 2019 at 8:21 | history | suggested | shaik moeed | CC BY-SA 4.0 | Improved formatting and corrected grammar |
| Sep 1, 2019 at 7:03 | review | Suggested edits | |||
| S Sep 1, 2019 at 8:21 | |||||
| Aug 15, 2019 at 12:17 | review | Suggested edits | |||
| Aug 15, 2019 at 14:44 | |||||
| May 2, 2019 at 19:34 | history | edited | Alec | CC BY-SA 4.0 | added 35 characters in body |
| Apr 12, 2019 at 6:12 | review | Suggested edits | |||
| Apr 12, 2019 at 7:38 | |||||
| Dec 29, 2018 at 7:21 | review | Suggested edits | |||
| Dec 29, 2018 at 10:55 | |||||
| S Dec 3, 2018 at 5:57 | history | suggested | Srikanth Gopalakrishnan | CC BY-SA 4.0 | Corrected a logical flaw |
| Dec 3, 2018 at 5:38 | review | Suggested edits | |||
| S Dec 3, 2018 at 5:57 | |||||
| May 20, 2018 at 9:49 | history | edited | Peter Mortensen | CC BY-SA 4.0 | Active reading [<https://en.wikipedia.org/wiki/One-liner_program>] |
| S Mar 12, 2018 at 0:42 | history | suggested | Invariance | CC BY-SA 3.0 | Fixed grammar |
| Mar 11, 2018 at 23:20 | review | Suggested edits | |||
| S Mar 12, 2018 at 0:42 | |||||
| Feb 15, 2018 at 19:21 | comment | added | picmate 涅 | "These iterables are handy... but you store all the values in memory and this is not always what you want", is either wrong or confusing. An iterable returns an iterator upon calling the iter() on the iterable, and an iterator doesn't always have to store its values in memory, depending on the implementation of the iter method, it can also generate values in the sequence on demand. | |
| S Nov 29, 2017 at 9:06 | history | suggested | Deepak Sood | CC BY-SA 3.0 | added python3 support for attribute __next__() in generator or next(generator_val) |
| Nov 29, 2017 at 7:46 | review | Suggested edits | |||
| S Nov 29, 2017 at 9:06 | |||||
| Oct 25, 2017 at 20:52 | history | edited | user2357112 | CC BY-SA 3.0 | added 3 characters in body |
| S Aug 6, 2017 at 22:31 | history | edited | tom | CC BY-SA 3.0 | Remove enclosing class introduced by previous edit to match the code in the question. |
| S Aug 6, 2017 at 22:31 | history | suggested | Cory Madden | CC BY-SA 3.0 | example included invalid Python that would not compile. You can't have a "." in a function name |
| Aug 6, 2017 at 14:19 | review | Suggested edits | |||
| S Aug 6, 2017 at 22:31 | |||||
| Aug 6, 2017 at 3:36 | review | Suggested edits | |||
| Aug 6, 2017 at 6:11 | |||||
| May 23, 2017 at 21:41 | comment | added | Matthias Fripp | yield is not as magical this answer suggests. When you call a function that contains a yield statement anywhere, you get a generator object, but no code runs. Then each time you extract an object from the generator, Python executes code in the function until it comes to a yield statement, then pauses and delivers the object. When you extract another object, Python resumes just after the yield and continues until it reaches another yield (often the same one, but one iteration later). This continues until the function runs off the end, at which point the generator is deemed exhausted. | |
| Apr 15, 2017 at 22:55 | history | edited | codeforester | CC BY-SA 3.0 | edited body |
| Oct 16, 2016 at 13:29 | review | Suggested edits | |||
| Oct 16, 2016 at 14:49 | |||||
| S Sep 23, 2016 at 9:08 | history | suggested | Umair Ahmad | CC BY-SA 3.0 | just "an" correction |
| Sep 23, 2016 at 7:26 | review | Suggested edits | |||
| S Sep 23, 2016 at 9:08 | |||||
| Aug 1, 2016 at 19:11 | history | edited | gsamaras | CC BY-SA 3.0 | minor |
| Mar 2, 2016 at 16:04 | history | edited | orokusaki | CC BY-SA 3.0 | Minor punctuation correction |
| Jan 5, 2016 at 6:29 | review | Suggested edits | |||
| Jan 5, 2016 at 7:32 | |||||
| S Dec 22, 2015 at 14:58 | history | suggested | J. A. Corbal | CC BY-SA 3.0 | Excellent post. Just minimal grammar correction (*can not -> cannot) |
| Dec 22, 2015 at 14:21 | review | Suggested edits | |||
| S Dec 22, 2015 at 14:58 | |||||
| Oct 29, 2015 at 14:56 | history | edited | rmtheis | CC BY-SA 3.0 | fixed grammar |
| Jul 15, 2015 at 17:34 | history | bounty awarded | rassa45 | ||
| Jul 14, 2015 at 21:23 | history | rollback | Aaron Hall♦ | Rollback to Revision 26 | |
| Jun 30, 2015 at 11:45 | history | edited | Eric O. Lebigot | CC BY-SA 3.0 | Fixed incorrect statement (iterators cannot be iterated over many times), and qualified restrictive statement about iterators (they do not have to be built from an iterable). |
| S Jun 18, 2015 at 13:37 | history | suggested | Waqar | CC BY-SA 3.0 | improve formatting |
| Jun 18, 2015 at 12:28 | review | Suggested edits | |||
| S Jun 18, 2015 at 13:37 | |||||
| Apr 17, 2015 at 19:28 | history | edited | Kasravnd | CC BY-SA 3.0 | added 6 characters in body |
| Apr 16, 2015 at 3:58 | review | Suggested edits | |||
| Apr 16, 2015 at 4:14 | |||||
| Aug 22, 2014 at 10:22 | history | edited | Alireza Fallah | CC BY-SA 3.0 | [Edit removed during grace period] |
| Jun 9, 2014 at 14:55 | review | Suggested edits | |||
| Jun 9, 2014 at 14:57 | |||||
| May 8, 2014 at 9:21 | review | Suggested edits | |||
| May 8, 2014 at 9:22 | |||||
| May 8, 2014 at 9:19 | review | Suggested edits | |||
| May 8, 2014 at 9:20 | |||||
| Apr 22, 2014 at 14:26 | history | edited | unode | CC BY-SA 3.0 | minor code indentation correction |
| Oct 19, 2013 at 21:25 | history | bounty awarded | alecxe | ||
| S Oct 8, 2013 at 4:58 | history | suggested | Bleeding Fingers | CC BY-SA 3.0 | irrelevant links removed |
| Oct 8, 2013 at 4:52 | review | Suggested edits | |||
| S Oct 8, 2013 at 4:58 | |||||
| Jul 20, 2013 at 18:35 | history | edited | martineau | CC BY-SA 3.0 | Clarified what happens the first time the generator created from a function with yield is used. |
| S May 15, 2013 at 22:03 | history | suggested | twasbrillig | CC BY-SA 3.0 | minor grammar fixes |
| May 15, 2013 at 21:59 | review | Suggested edits | |||
| S May 15, 2013 at 22:03 | |||||
| Apr 13, 2013 at 7:53 | history | edited | TimB | CC BY-SA 3.0 | Tiny wording clarification. |
| S Apr 10, 2013 at 4:30 | history | suggested | Kylar | CC BY-SA 3.0 | Correct terminology. The Python documentation states: When a generator function is called, it returns an iterator known as a generator iterator, or more commonly, a generator. |
| Apr 10, 2013 at 4:27 | review | Suggested edits | |||
| S Apr 10, 2013 at 4:30 | |||||
| S Mar 30, 2013 at 7:07 | history | suggested | Michael Geary | CC BY-SA 3.0 | Change "comprehension list" to "list comprehension" |
| Mar 30, 2013 at 6:52 | review | Suggested edits | |||
| S Mar 30, 2013 at 7:07 | |||||
| Jan 28, 2013 at 12:33 | history | edited | Marius Gedminas | CC BY-SA 3.0 | corrected punctuation (spaces before a colon) |
| Oct 29, 2012 at 17:44 | history | wiki removed | Robert Harvey | ||
| Oct 24, 2012 at 15:43 | history | edited | Dolph | CC BY-SA 3.0 | moved intro statement to outside of the first section |
| May 23, 2012 at 5:30 | history | edited | machineghost | CC BY-SA 3.0 | shortened code comments slightly to eliminate horizontal scrolling |
| S Apr 28, 2012 at 5:57 | history | suggested | Gabriel Gaster | CC BY-SA 3.0 | Corrected typo. Changed the word "expends" to "expands" in: The loop iterate on a list but the list expands while the loop is been iterated |
| Apr 28, 2012 at 0:27 | review | Suggested edits | |||
| S Apr 28, 2012 at 5:57 | |||||
| Jul 5, 2011 at 11:34 | history | edited | Bite code | CC BY-SA 3.0 | deleted 2 characters in body |
| May 26, 2011 at 6:48 | history | edited | Parand | CC BY-SA 3.0 | Corrected spelling / grammar (every to ever) |
| Aug 15, 2010 at 16:19 | history | edited | Zack Marrapese | CC BY-SA 2.5 | deleted 1 characters in body |
| Aug 14, 2010 at 16:41 | history | edited | rz. | CC BY-SA 2.5 | mylist -> mygenerator after the generator example. |
| Feb 19, 2010 at 13:22 | history | edited | Bite code | CC BY-SA 2.5 | Removing the dust from this very old post |
| Nov 10, 2009 at 11:54 | history | edited | Bite code | CC BY-SA 2.5 | typo |
| Oct 20, 2009 at 18:22 | history | edited | Bite code | CC BY-SA 2.5 | adding link to decorators |
| Oct 24, 2008 at 19:36 | history | edited | Bite code | CC BY-SA 2.5 | adding JF comment |
| Oct 24, 2008 at 18:05 | history | edited | Bite code | CC BY-SA 2.5 | adding part on infinite generators |
| Oct 24, 2008 at 6:41 | history | edited | Bite code | CC BY-SA 2.5 | answer to comment |
| Oct 24, 2008 at 2:18 | vote | accept | Alex. S. | ||
| Oct 23, 2008 at 23:16 | history | edited | Bite code | CC BY-SA 2.5 | add code analysis |
| Oct 23, 2008 at 22:48 | history | answered | Bite code | CC BY-SA 2.5 |