{"id":11793,"date":"2025-01-20T05:19:56","date_gmt":"2025-01-20T05:19:56","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=11793"},"modified":"2025-01-20T05:19:56","modified_gmt":"2025-01-20T05:19:56","slug":"python-yield-keyword","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/python\/python-yield-keyword","title":{"rendered":"What Does the &#8220;Yield&#8221; Keyword Do in Python?"},"content":{"rendered":"<p>The yield keyword in Python is used in a function to turn it into a generator. A generator is a type of iterable, like a list or tuple, but it does not store its values in memory all at once. Instead, it generates each value on the fly, one at a time, as needed, using the yield statement.<\/p>\n<p>When a function contains yield, it becomes a generator function, which doesn&#8217;t return a value all at once. Instead, it produces a sequence of results lazily, one at a time, each time the yield statement is encountered. This is a powerful tool for handling large data sets or streams of data where you don&#8217;t want to store everything in memory at once.<\/p>\n<h2>Key Features of yield:<\/h2>\n<ul>\n<li>Pauses the function&#8217;s execution: When the yield keyword is encountered, the function&#8217;s execution pauses, and the value is returned to the caller.<\/li>\n<li>Resumes where it left off: The function can resume execution from the point it was paused (just after the yield statement) when the next value is requested.<\/li>\n<li>Does not require returning all values at once: The function can yield values one by one, conserving memory by not keeping all results in memory at once.<\/li>\n<\/ul>\n<p><strong>Common Simple Example of yield:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def count_up_to(max):\r\n   count = 1\r\n   while count &lt;= max:\r\n       yield count\r\n       count += 1\r\n\r\ncounter = count_up_to(5)\r\nfor num in counter:\r\n   print(num)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\n1\r\n2\r\n3\r\n4\r\n5\r\n<\/pre>\n<p>Here, count_up_to yields values from 1 to max, one at a time. The function pauses after each yield and resumes when the next value is requested in the loop.<\/p>\n<h3>Another Example Breakdown:<\/h3>\n<p>Consider the following method with yield:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\ndef _get_child_candidates(self, distance, min_dist, max_dist):\r\n   if self._leftchild and distance - max_dist < self._median:\r\n       yield self._leftchild\r\n   if self._rightchild and distance + max_dist >= self._median:\r\n       yield self._rightchild\r\n<\/pre>\n<p>What happens here?<br \/>\nInstead of returning a list of child nodes, the function yields each child node one by one based on the conditions.<br \/>\nEach call to _get_child_candidates will return a single child node when the conditions are met, and the function pauses until the next call.<\/p>\n<p><strong>Example of How the Caller Interacts with yield:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nresult, candidates = [], [self]\r\nwhile candidates:\r\n   node = candidates.pop()\r\n   distance = node._get_dist(obj)\r\n   if distance <= max_dist and distance >= min_dist:\r\n       result.extend(node._values)\r\n   candidates.extend(node._get_child_candidates(distance, min_dist, max_dist))\r\nreturn result\r\n<\/pre>\n<p>Here, candidates.extend() calls _get_child_candidates and collects nodes one at a time through the generator\u2019s yield.<\/p>\n<h3>What Happens When _get_child_candidates is Called?<\/h3>\n<ul>\n<li>When _get_child_candidates is invoked, it does not return a list. Instead, it yields values one at a time based on the conditions.<\/li>\n<li>Each time yield is encountered, the function pauses and returns a single child node (either self._leftchild or self._rightchild) to the caller.<\/li>\n<li>After that, the function resumes execution when the next value is requested, allowing it to yield more values if further conditions are met.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The yield keyword in Python is used in a function to turn it into a generator. A generator is a type of iterable, like a list or tuple, but it does not store its values in memory all at once. Instead, it generates each value on the fly, one at a time, as needed, using [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11794,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[16],"tags":[],"class_list":["post-11793","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11793"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=11793"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11793\/revisions"}],"predecessor-version":[{"id":11795,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11793\/revisions\/11795"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/11794"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=11793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=11793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=11793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}