<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Mahmoud Mahajna — Software Engineer And Security Researcher</title><description>I build scalable software, investigate security vulnerabilities, and analyze software through reverse engineering. My work spans software engineering, offensive security, malware analysis, and vulnerability research.</description><link>https://mahajnam.com</link><language>en-us</language><item><title>Reading large files in Python</title><link>https://mahajnam.com/blog/reading-large-files-in-python</link><guid isPermaLink="true">https://mahajnam.com/blog/reading-large-files-in-python</guid><description>Why read() blows up on huge files, and how iterators, iterables, generators and the yield keyword let you process them one line at a time.</description><pubDate>Sat, 12 Nov 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;What will you learn?&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Opening files&lt;/li&gt;
&lt;li&gt;Reading files&lt;/li&gt;
&lt;li&gt;Iteration&lt;/li&gt;
&lt;li&gt;Iterators&lt;/li&gt;
&lt;li&gt;Iterables&lt;/li&gt;
&lt;li&gt;Generators&lt;/li&gt;
&lt;li&gt;Yield&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;how-to-open-a-file-in-python&quot;&gt;How to open a file in Python?&lt;/h2&gt;
&lt;p&gt;Opening a file in Python is done by means of the built-in function &lt;strong&gt;&lt;code&gt;open&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;link rel=&quot;stylesheet&quot; href=&quot;/_astro/ec.p9ju4.css&quot;&gt;&lt;script type=&quot;module&quot; src=&quot;/_astro/ec.0vx5m.js&quot;&gt;&lt;/script&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;open&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#FFAB70;--1:#AE4B07&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--0:#FFAB70;--1:#AE4B07&quot;&gt;mode&lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#9ECBFF;--1:#032F62&quot;&gt;&apos;r&apos;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--0:#FFAB70;--1:#AE4B07&quot;&gt;buffering&lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=-&lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--0:#FFAB70;--1:#AE4B07&quot;&gt;encoding&lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--0:#FFAB70;--1:#AE4B07&quot;&gt;errors&lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--0:#FFAB70;--1:#AE4B07&quot;&gt;newline&lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--0:#FFAB70;--1:#AE4B07&quot;&gt;closefd&lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--0:#FFAB70;--1:#AE4B07&quot;&gt;opener&lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;copy&quot;&gt;&lt;div aria-live=&quot;polite&quot;&gt;&lt;/div&gt;&lt;button title=&quot;Copy to clipboard&quot; data-copied=&quot;Copied!&quot; data-code=&quot;open(file, mode=&amp;#x27;r&amp;#x27;, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/button&gt;&lt;/div&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;Let’s say you have a text file named &lt;strong&gt;words&lt;/strong&gt;, you can open it as follows:&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;words &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;open&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#9ECBFF;--1:#032F62&quot;&gt;&quot;words.txt&quot;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--0:#9ECBFF;--1:#032F62&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;copy&quot;&gt;&lt;div aria-live=&quot;polite&quot;&gt;&lt;/div&gt;&lt;button title=&quot;Copy to clipboard&quot; data-copied=&quot;Copied!&quot; data-code=&quot;words = open(&amp;quot;words.txt&amp;quot;, &amp;quot;r&amp;quot;)&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/button&gt;&lt;/div&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;where &lt;strong&gt;&lt;code&gt;words&lt;/code&gt;&lt;/strong&gt; is a file stream object and &lt;strong&gt;&lt;code&gt;r&lt;/code&gt;&lt;/strong&gt; stands for read mode.&lt;/p&gt;
&lt;h2 id=&quot;how-to-read-a-file-in-python&quot;&gt;How to read a file in Python?&lt;/h2&gt;
&lt;p&gt;Upon opening a file, the &lt;strong&gt;&lt;code&gt;open&lt;/code&gt; function&lt;/strong&gt; returns a &lt;strong&gt;file stream object&lt;/strong&gt;. That file stream object has methods which enable us to read the file content.&lt;/p&gt;
&lt;p&gt;One way to open, read, then close a file is as follows:&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;fstream &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;open&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#9ECBFF;--1:#032F62&quot;&gt;&quot;words.txt&quot;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--0:#9ECBFF;--1:#032F62&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;content &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; fstream.read()&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#99A0A6;--1:#616972&quot;&gt;# do something with the content&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;(content)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#99A0A6;--1:#616972&quot;&gt;# close file&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;fstream.close()&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;copy&quot;&gt;&lt;div aria-live=&quot;polite&quot;&gt;&lt;/div&gt;&lt;button title=&quot;Copy to clipboard&quot; data-copied=&quot;Copied!&quot; data-code=&quot;fstream = open(&amp;quot;words.txt&amp;quot;, &amp;quot;r&amp;quot;)content = fstream.read()# do something with the contentprint(content)# close filefstream.close()&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/button&gt;&lt;/div&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;where &lt;code&gt;content&lt;/code&gt; is a string variable holding the file content in memory.&lt;/p&gt;
&lt;p&gt;If we wish to close the file implicitly, we can do it as follows:&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;with&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;open&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#9ECBFF;--1:#032F62&quot;&gt;&quot;words.txt&quot;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; fstream:&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;content &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; fstream.read()&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;copy&quot;&gt;&lt;div aria-live=&quot;polite&quot;&gt;&lt;/div&gt;&lt;button title=&quot;Copy to clipboard&quot; data-copied=&quot;Copied!&quot; data-code=&quot;with open(&amp;quot;words.txt&amp;quot;) as fstream:    content = fstream.read()&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/button&gt;&lt;/div&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;where the &lt;strong&gt;&lt;code&gt;with&lt;/code&gt;&lt;/strong&gt; statement takes care of closing the file, since it knows how to deal with file streams.&lt;/p&gt;
&lt;h2 id=&quot;whats-wrong-with-the-above-approach&quot;&gt;What’s wrong with the above approach?&lt;/h2&gt;
&lt;p&gt;Reading a large file using the above method is problematic and will unnecessarily overflow the memory, since it attempts to load the &lt;strong&gt;whole&lt;/strong&gt; content into memory at once — and there may never be enough room for that huge amount of data.&lt;/p&gt;
&lt;h2 id=&quot;so-how-to-get-around-that&quot;&gt;So, how to get around that?&lt;/h2&gt;
&lt;p&gt;Introducing iteration, iterators, iterables, generators and the &lt;code&gt;yield&lt;/code&gt; keyword…&lt;/p&gt;
&lt;h2 id=&quot;what-is-iteration&quot;&gt;What is iteration?&lt;/h2&gt;
&lt;p&gt;It’s the process of iterating/looping through the items in a collection (lists, dictionaries and tuples are examples of collections).&lt;/p&gt;
&lt;h2 id=&quot;what-is-an-iterator&quot;&gt;What is an iterator?&lt;/h2&gt;
&lt;p&gt;An object representing a sequence of data which can be accessed one item at a time.&lt;/p&gt;
&lt;p&gt;A Python iterator follows &lt;strong&gt;the iterator protocol&lt;/strong&gt;, so it implements and exposes a &lt;strong&gt;&lt;code&gt;__next__()&lt;/code&gt;&lt;/strong&gt; method which returns the next element of an &lt;strong&gt;iterable&lt;/strong&gt; object.&lt;/p&gt;
&lt;p&gt;It’s also important to note that iterators are how Python (the PVM) is able to iterate through a collection such as a list (e.g. in a &lt;code&gt;for … in&lt;/code&gt; statement). In other words, under the hood, a &lt;code&gt;for … in&lt;/code&gt; loop makes use of the collection’s iterator to walk through its items.&lt;/p&gt;
&lt;h2 id=&quot;what-is-an-iterable&quot;&gt;What is an iterable?&lt;/h2&gt;
&lt;p&gt;Any object which implements the &lt;strong&gt;&lt;code&gt;__iter__&lt;/code&gt;&lt;/strong&gt; function is called an iterable. That function returns an &lt;strong&gt;iterator&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Lists, dictionaries and tuples in Python are examples of iterables, since they implement that function:&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; numbers &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; numbers_iter &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; numbers.&lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;__iter__&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;()&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; numbers_iter.&lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;__next__&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;()&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;1&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; numbers_iter.&lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;__next__&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;()&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;2&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; numbers_iter.&lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;__next__&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;()&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;3&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; numbers_iter.&lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;__next__&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;()&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;Traceback (most recent call last):&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;File &lt;/span&gt;&lt;span style=&quot;--0:#9ECBFF;--1:#032F62&quot;&gt;&quot;&amp;lt;pyshell#31&amp;gt;&quot;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;, line &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;module&lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;numbers_iter.&lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;__next__&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;()&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;StopIteration&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;copy&quot;&gt;&lt;div aria-live=&quot;polite&quot;&gt;&lt;/div&gt;&lt;button title=&quot;Copy to clipboard&quot; data-copied=&quot;Copied!&quot; data-code=&quot;&gt;&gt;&gt; numbers = [1,2,3]&gt;&gt;&gt; numbers_iter = numbers.__iter__()&gt;&gt;&gt; numbers_iter.__next__()1&gt;&gt;&gt; numbers_iter.__next__()2&gt;&gt;&gt; numbers_iter.__next__()3&gt;&gt;&gt; numbers_iter.__next__()Traceback (most recent call last):  File &amp;quot;&lt;pyshell#31&gt;&amp;quot;, line 1, in &lt;module&gt;    numbers_iter.__next__()StopIteration&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/button&gt;&lt;/div&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;Notice that the iterator raises a &lt;strong&gt;&lt;code&gt;StopIteration&lt;/code&gt;&lt;/strong&gt; exception when there are no remaining values.&lt;/p&gt;
&lt;h2 id=&quot;what-is-a-generator&quot;&gt;What is a generator?&lt;/h2&gt;
&lt;p&gt;A special type of iterator where a sequence of data is lazily loaded into memory, one item at a time. This makes it perfect for reading large amounts of data.&lt;/p&gt;
&lt;p&gt;Looking at the generator definition:&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#B392F0;--1:#6F42C1&quot;&gt;Generator&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]):&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;__next__&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;(self) -&amp;gt; _T_co: &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;...&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#B392F0;--1:#6F42C1&quot;&gt;@abstractmethod&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#B392F0;--1:#6F42C1&quot;&gt;send&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;(self, __value: _T_contra) -&amp;gt; _T_co: &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;...&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#B392F0;--1:#6F42C1&quot;&gt;@overload&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#B392F0;--1:#6F42C1&quot;&gt;@abstractmethod&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#B392F0;--1:#6F42C1&quot;&gt;throw&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;(&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;        &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;self, __typ: Type[&lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;BaseException&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;], __val: &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;BaseException&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;|&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;, __tb: TracebackType &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;|&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;...&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;) -&amp;gt; _T_co: &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;...&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#B392F0;--1:#6F42C1&quot;&gt;@overload&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#B392F0;--1:#6F42C1&quot;&gt;@abstractmethod&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#B392F0;--1:#6F42C1&quot;&gt;throw&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;(self, __typ: &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;BaseException&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;, __val: &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;, __tb: TracebackType &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;|&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;) -&amp;gt; _T_co: &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;...&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#B392F0;--1:#6F42C1&quot;&gt;close&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;(self) -&amp;gt; &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;...&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;__iter__&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;(self) -&amp;gt; Generator[_T_co, _T_contra, _V_co]: &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;...&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;copy&quot;&gt;&lt;div aria-live=&quot;polite&quot;&gt;&lt;/div&gt;&lt;button title=&quot;Copy to clipboard&quot; data-copied=&quot;Copied!&quot; data-code=&quot;class Generator(Iterator[_T_co], Generic[_T_co, _T_contra, _V_co]):    def __next__(self) -&gt; _T_co: ...    @abstractmethod    def send(self, __value: _T_contra) -&gt; _T_co: ...    @overload    @abstractmethod    def throw(        self, __typ: Type[BaseException], __val: BaseException | object = ..., __tb: TracebackType | None = ...    ) -&gt; _T_co: ...    @overload    @abstractmethod    def throw(self, __typ: BaseException, __val: None = ..., __tb: TracebackType | None = ...) -&gt; _T_co: ...    def close(self) -&gt; None: ...    def __iter__(self) -&gt; Generator[_T_co, _T_contra, _V_co]: ...&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/button&gt;&lt;/div&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;It can be understood from the above definition that a generator is an &lt;strong&gt;iterator&lt;/strong&gt; as well as an &lt;strong&gt;iterable&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 id=&quot;what-is-the-yield-keyword&quot;&gt;What is the &lt;code&gt;yield&lt;/code&gt; keyword?&lt;/h2&gt;
&lt;p&gt;This keyword is used to &lt;strong&gt;return&lt;/strong&gt; a generator from generator functions. In other words, what makes a function a generator function is the presence of &lt;code&gt;yield&lt;/code&gt; in the function body.&lt;/p&gt;
&lt;p&gt;What’s special about &lt;code&gt;yield&lt;/code&gt; is its ability to &lt;strong&gt;suspend&lt;/strong&gt; the generator function’s execution and &lt;strong&gt;return&lt;/strong&gt; the &lt;strong&gt;yielded value&lt;/strong&gt; to the caller. Meanwhile, the &lt;strong&gt;function state&lt;/strong&gt; and other details are saved for the &lt;strong&gt;next hit&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 id=&quot;putting-the-pieces-together&quot;&gt;Putting the pieces together&lt;/h2&gt;
&lt;p&gt;Let’s suppose we have a text file named &lt;strong&gt;words&lt;/strong&gt;. This file contains random words separated by newlines, and we want to find the &lt;strong&gt;longest&lt;/strong&gt; word and print it out. Let’s also suppose that the file size is &lt;strong&gt;100 MB&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;This is how it can be done:&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame has-title&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;span class=&quot;title&quot;&gt;longest_word.py&lt;/span&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#99A0A6;--1:#616972&quot;&gt;# this generator function returns a generator in each yield hit&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#B392F0;--1:#6F42C1&quot;&gt;read_words&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;(filename):&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#FFAB70;--1:#AE4B07&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;open&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;(filename, &lt;/span&gt;&lt;span style=&quot;--0:#9ECBFF;--1:#032F62&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; line &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#FFAB70;--1:#AE4B07&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;:&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;yield&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; line&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#99A0A6;--1:#616972&quot;&gt;# the file won&apos;t close automatically since&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#99A0A6;--1:#616972&quot;&gt;# we don&apos;t use with statement thus we need to explicitly close it&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#FFAB70;--1:#AE4B07&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;.close()&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#B392F0;--1:#6F42C1&quot;&gt;find_max_word&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;() -&amp;gt; &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;:&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;max_word_len &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;0&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;max_word &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;None&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#99A0A6;--1:#616972&quot;&gt;# since the generator function returns a generator&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#99A0A6;--1:#616972&quot;&gt;# and the generator is a special type of iterator&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#99A0A6;--1:#616972&quot;&gt;# we can simply iterate over it using for .. in&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; yielded_word &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; read_words(&lt;/span&gt;&lt;span style=&quot;--0:#9ECBFF;--1:#032F62&quot;&gt;&quot;words.txt&quot;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;):&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#99A0A6;--1:#616972&quot;&gt;# remove newline and line break characters&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;        &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;word &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; yielded_word.strip()&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;        &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;len_of_word &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;(word)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; len_of_word &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; max_word_len:&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;            &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;max_word &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; word&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;            &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;max_word_len &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; len_of_word&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; max_word&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;max_word &lt;/span&gt;&lt;span style=&quot;--0:#F97583;--1:#BF3441&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt; find_max_word()&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#79B8FF;--1:#005CC5&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;--0:#E1E4E8;--1:#24292E&quot;&gt;(max_word)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class=&quot;copy&quot;&gt;&lt;div aria-live=&quot;polite&quot;&gt;&lt;/div&gt;&lt;button title=&quot;Copy to clipboard&quot; data-copied=&quot;Copied!&quot; data-code=&quot;# this generator function returns a generator in each yield hitdef read_words(filename):    file = open(filename, &amp;quot;r&amp;quot;)    for line in file:        yield line    # the file won&amp;#x27;t close automatically since    # we don&amp;#x27;t use with statement thus we need to explicitly close it    file.close()def find_max_word() -&gt; str:    max_word_len = 0    max_word = None    # since the generator function returns a generator    # and the generator is a special type of iterator    # we can simply iterate over it using for .. in    for yielded_word in read_words(&amp;quot;words.txt&amp;quot;):        # remove newline and line break characters        word = yielded_word.strip()        len_of_word = len(word)        if len_of_word &gt; max_word_len:            max_word = word            max_word_len = len_of_word    return max_wordmax_word = find_max_word()print(max_word)&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/button&gt;&lt;/div&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;where &lt;strong&gt;&lt;code&gt;read_words&lt;/code&gt;&lt;/strong&gt; is the generator function.&lt;/p&gt;
</content:encoded><category>Python</category><category>Programming</category><category>Files</category><author>mahmod.mahajnaa@outlook.com (Mahmoud Mahajna)</author></item></channel></rss>