Warning, /frameworks/syndication/autotests/atom/bug411626.xml is written in an unsupported language. File is not indexed.

0001 <?xml version="1.0" encoding="utf-8"?>
0002 <feed xmlns="http://www.w3.org/2005/Atom">
0003 
0004   <title>Real Python</title>
0005   <link href="https://realpython.com/atom.xml" rel="self"/>
0006   <link href="https://realpython.com/"/>
0007   <updated>2019-09-04T14:00:00+00:00</updated>
0008   <id>https://realpython.com/</id>
0009   <author>
0010     <name>Real Python</name>
0011   </author>
0012 
0013   
0014     <entry>
0015       <title>Python args and kwargs: Demystified</title>
0016       <id>https://realpython.com/python-kwargs-and-args/</id>
0017       <link href="https://realpython.com/python-kwargs-and-args/"/>
0018       <updated>2019-09-04T14:00:00+00:00</updated>
0019       <summary>In this step-by-step tutorial, you&#39;ll learn how to use args and kwargs in Python to add more flexibility to your functions. You&#39;ll also take a closer look at the single and double-asterisk unpacking operators, which you can use to unpack any iterable object in Python.</summary>
0020       <content type="html">
0021         &lt;p&gt;Sometimes, when you look at a function definition in Python, you might see that it takes two strange arguments: &lt;strong&gt;&lt;code&gt;*args&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;**kwargs&lt;/code&gt;&lt;/strong&gt;. If you&amp;rsquo;ve ever wondered what these peculiar variables are, or why your IDE defines them in &lt;code&gt;main()&lt;/code&gt;, then this article is for you. You&amp;rsquo;ll learn how to use args and kwargs in Python to add more flexibility to your functions.&lt;/p&gt;
0022 &lt;p&gt;&lt;strong&gt;By the end of the article, you&amp;rsquo;ll know:&lt;/strong&gt;&lt;/p&gt;
0023 &lt;ul&gt;
0024 &lt;li&gt;What &lt;code&gt;*args&lt;/code&gt; and &lt;code&gt;**kwargs&lt;/code&gt; actually mean&lt;/li&gt;
0025 &lt;li&gt;How to use &lt;code&gt;*args&lt;/code&gt; and &lt;code&gt;**kwargs&lt;/code&gt; in function definitions&lt;/li&gt;
0026 &lt;li&gt;How to use a single asterisk (&lt;code&gt;*&lt;/code&gt;) to unpack iterables&lt;/li&gt;
0027 &lt;li&gt;How to use two asterisks (&lt;code&gt;**&lt;/code&gt;) to unpack dictionaries&lt;/li&gt;
0028 &lt;/ul&gt;
0029 &lt;p&gt;This article assumes that you already know how to define Python functions and work with &lt;a href=&quot;https://realpython.com/lessons/mutable-data-structures-lists-dictionaries/&quot;&gt;lists and dictionaries&lt;/a&gt;.&lt;/p&gt;
0030 &lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Free Bonus:&lt;/strong&gt; &lt;a href=&quot;&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-cheat-sheet-shortened&quot; data-focus=&quot;false&quot;&gt;Click here to get a Python Cheat Sheet&lt;/a&gt; and learn the basics of Python 3, like working with data types, dictionaries, lists, and Python functions.&lt;/p&gt;&lt;/div&gt;
0031 
0032 &lt;h2 id=&quot;passing-multiple-arguments-to-a-function&quot;&gt;Passing Multiple Arguments to a Function&lt;/h2&gt;
0033 &lt;p&gt;&lt;strong&gt;&lt;code&gt;*args&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;**kwargs&lt;/code&gt;&lt;/strong&gt; allow you to pass multiple arguments or keyword arguments to a function. Consider the following example. This is a simple function that takes two arguments and returns their sum:&lt;/p&gt;
0034 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
0035     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
0036 &lt;/pre&gt;&lt;/div&gt;
0037 
0038 &lt;p&gt;This function works fine, but it&amp;rsquo;s limited to only two arguments. What if you need to sum a varying number of arguments, where the specific number of arguments passed is only determined at runtime? Wouldn&amp;rsquo;t it be great to create a function that could sum &lt;em&gt;all&lt;/em&gt; the integers passed to it, no matter how many there are?&lt;/p&gt;
0039 &lt;h2 id=&quot;using-the-python-args-variable-in-function-definitions&quot;&gt;Using the Python args Variable in Function Definitions&lt;/h2&gt;
0040 &lt;p&gt;There are a few ways you can pass a varying number of arguments to a function. The first way is often the most intuitive for people that have experience with collections. You simply pass a list or a &lt;a href=&quot;https://realpython.com/python-sets/&quot;&gt;set&lt;/a&gt; of all the arguments to your function. So for &lt;code&gt;my_sum()&lt;/code&gt;, you could pass a list of all the integers you need to add:&lt;/p&gt;
0041 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# sum_integers_list.py&lt;/span&gt;
0042 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_integers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
0043     &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
0044     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_integers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0045         &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
0046     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;
0047 
0048 &lt;span class=&quot;n&quot;&gt;list_of_integers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0049 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list_of_integers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
0050 &lt;/pre&gt;&lt;/div&gt;
0051 
0052 &lt;p&gt;This implementation works, but whenever you call this function you&amp;rsquo;ll also need to create a list of arguments to pass to it. This can be inconvenient, especially if you don&amp;rsquo;t know up front all the values that should go into the list.&lt;/p&gt;
0053 &lt;p&gt;This is where &lt;code&gt;*args&lt;/code&gt; can be really useful, because it allows you to pass a varying number of positional arguments. Take the following example:&lt;/p&gt;
0054 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# sum_integers_args.py&lt;/span&gt;
0055 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
0056     &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
0057     &lt;span class=&quot;c1&quot;&gt;# Iterating over the Python args tuple&lt;/span&gt;
0058     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0059         &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
0060     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;
0061 
0062 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
0063 &lt;/pre&gt;&lt;/div&gt;
0064 
0065 &lt;p&gt;In this example, you&amp;rsquo;re no longer passing a list to &lt;code&gt;my_sum()&lt;/code&gt;. Instead, you&amp;rsquo;re passing three different positional arguments. &lt;code&gt;my_sum()&lt;/code&gt; takes all the parameters that are provided in the input and packs them all into a single iterable object named &lt;code&gt;args&lt;/code&gt;.&lt;/p&gt;
0066 &lt;p&gt;Note that &lt;strong&gt;&lt;code&gt;args&lt;/code&gt; is just a name.&lt;/strong&gt; You&amp;rsquo;re not required to use the name &lt;code&gt;args&lt;/code&gt;. You can choose any name that you prefer, such as &lt;code&gt;integers&lt;/code&gt;:&lt;/p&gt;
0067 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# sum_integers_args_2.py&lt;/span&gt;
0068 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;integers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
0069     &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
0070     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;integers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0071         &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
0072     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;
0073 
0074 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
0075 &lt;/pre&gt;&lt;/div&gt;
0076 
0077 &lt;p&gt;The function still works, even if you pass the iterable object as &lt;code&gt;integers&lt;/code&gt; instead of &lt;code&gt;args&lt;/code&gt;. All that matters here is that you use the &lt;strong&gt;unpacking operator&lt;/strong&gt; (&lt;code&gt;*&lt;/code&gt;).&lt;/p&gt;
0078 &lt;p&gt;Bear in mind that the iterable object you&amp;rsquo;ll get using the unpacking operator &lt;code&gt;*&lt;/code&gt; is &lt;a href=&quot;https://realpython.com/python-lists-tuples/&quot;&gt;not a &lt;code&gt;list&lt;/code&gt; but a &lt;code&gt;tuple&lt;/code&gt;&lt;/a&gt;. A &lt;code&gt;tuple&lt;/code&gt; is similar to a &lt;code&gt;list&lt;/code&gt; in that they both support slicing and iteration. However, tuples are very different in at least one aspect: lists are &lt;a href=&quot;https://realpython.com/courses/immutability-python/&quot;&gt;mutable&lt;/a&gt;, while tuples are not. To test this, run the following code. This script tries to change a value of a list:&lt;/p&gt;
0079 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# change_list.py&lt;/span&gt;
0080 &lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0081 &lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;
0082 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0083 &lt;/pre&gt;&lt;/div&gt;
0084 
0085 &lt;p&gt;The value located at the very first index of the list should be updated to &lt;code&gt;9&lt;/code&gt;. If you execute this script, you will see that the list indeed gets modified:&lt;/p&gt;
0086 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python change_list.py
0087 &lt;span class=&quot;go&quot;&gt;[9, 2, 3]&lt;/span&gt;
0088 &lt;/pre&gt;&lt;/div&gt;
0089 
0090 &lt;p&gt;The first value is no longer &lt;code&gt;0&lt;/code&gt;, but the updated value &lt;code&gt;9&lt;/code&gt;. Now, try to do the same with a tuple:&lt;/p&gt;
0091 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# change_tuple.py&lt;/span&gt;
0092 &lt;span class=&quot;n&quot;&gt;my_tuple&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0093 &lt;span class=&quot;n&quot;&gt;my_tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;
0094 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0095 &lt;/pre&gt;&lt;/div&gt;
0096 
0097 &lt;p&gt;Here, you see the same values, except they&amp;rsquo;re held together as a tuple. If you try to execute this script, you will see that the Python interpreter returns an &lt;a href=&quot;https://realpython.com/python-exceptions/&quot;&gt;error&lt;/a&gt;:&lt;/p&gt;
0098 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python change_tuple.py
0099 &lt;span class=&quot;go&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
0100 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;change_tuple.py&amp;quot;, line 3, in &amp;lt;module&amp;gt;&lt;/span&gt;
0101 &lt;span class=&quot;go&quot;&gt;    my_tuple[0] = 9&lt;/span&gt;
0102 &lt;span class=&quot;go&quot;&gt;TypeError: &amp;#39;tuple&amp;#39; object does not support item assignment&lt;/span&gt;
0103 &lt;/pre&gt;&lt;/div&gt;
0104 
0105 &lt;p&gt;This is because a tuple is an immutable object, and its values cannot be changed after assignment. Keep this in mind when you&amp;rsquo;re working with tuples and &lt;code&gt;*args&lt;/code&gt;.&lt;/p&gt;
0106 &lt;h2 id=&quot;using-the-python-kwargs-variable-in-function-definitions&quot;&gt;Using the Python kwargs Variable in Function Definitions&lt;/h2&gt;
0107 &lt;p&gt;Okay, now you&amp;rsquo;ve understood what &lt;code&gt;*args&lt;/code&gt; is for, but what about &lt;code&gt;**kwargs&lt;/code&gt;? &lt;code&gt;**kwargs&lt;/code&gt; works just like &lt;code&gt;*args&lt;/code&gt;, but instead of accepting positional arguments it accepts keyword (or &lt;strong&gt;named&lt;/strong&gt;) arguments. Take the following example:&lt;/p&gt;
0108 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# concatenate.py&lt;/span&gt;
0109 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;concatenate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
0110     &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;
0111     &lt;span class=&quot;c1&quot;&gt;# Iterating over the Python kwargs dictionary&lt;/span&gt;
0112     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
0113         &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt;
0114     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;
0115 
0116 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;concatenate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Real&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Python&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Is&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Great&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;!&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
0117 &lt;/pre&gt;&lt;/div&gt;
0118 
0119 &lt;p&gt;When you execute the script above, &lt;code&gt;concatenate()&lt;/code&gt; will iterate through the Python kwargs &lt;a href=&quot;https://realpython.com/python-dicts/&quot;&gt;dictionary&lt;/a&gt; and concatenate all the values it finds:&lt;/p&gt;
0120 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python concatenate.py
0121 &lt;span class=&quot;go&quot;&gt;RealPythonIsGreat!&lt;/span&gt;
0122 &lt;/pre&gt;&lt;/div&gt;
0123 
0124 &lt;p&gt;Like &lt;code&gt;args&lt;/code&gt;, &lt;code&gt;kwargs&lt;/code&gt; is just a name that can be changed to whatever you want. Again, what is important here is the use of the &lt;strong&gt;unpacking operator&lt;/strong&gt; (&lt;code&gt;**&lt;/code&gt;).&lt;/p&gt;
0125 &lt;p&gt;So, the previous example could be written like this:&lt;/p&gt;
0126 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# concatenate_2.py&lt;/span&gt;
0127 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;concatenate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
0128     &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;
0129     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
0130         &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt;
0131     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;
0132 
0133 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;concatenate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Real&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Python&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Is&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Great&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;!&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
0134 &lt;/pre&gt;&lt;/div&gt;
0135 
0136 &lt;p&gt;Note that in the example above the iterable object is a standard &lt;code&gt;dict&lt;/code&gt;. If you &lt;a href=&quot;https://realpython.com/iterate-through-dictionary-python/&quot;&gt;iterate over the dictionary&lt;/a&gt; and want to return its values, like in the example shown, then you must use &lt;code&gt;.values()&lt;/code&gt;.&lt;/p&gt;
0137 &lt;p&gt;In fact, if you forget to use this method, you will find yourself iterating through the &lt;strong&gt;keys&lt;/strong&gt; of your Python kwargs dictionary instead, like in the following example:&lt;/p&gt;
0138 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# concatenate_keys.py&lt;/span&gt;
0139 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;concatenate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
0140     &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;
0141     &lt;span class=&quot;c1&quot;&gt;# Iterating over the keys of the Python kwargs dictionary&lt;/span&gt;
0142     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0143         &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt;
0144     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;
0145 
0146 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;concatenate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Real&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Python&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Is&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Great&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;!&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
0147 &lt;/pre&gt;&lt;/div&gt;
0148 
0149 &lt;p&gt;Now, if you try to execute this example, you&amp;rsquo;ll notice the following output:&lt;/p&gt;
0150 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python concatenate_keys.py
0151 &lt;span class=&quot;go&quot;&gt;abcde&lt;/span&gt;
0152 &lt;/pre&gt;&lt;/div&gt;
0153 
0154 &lt;p&gt;As you can see, if you don&amp;rsquo;t specify &lt;code&gt;.values()&lt;/code&gt;, your function will iterate over the keys of your Python kwargs dictionary, returning the wrong result.&lt;/p&gt;
0155 &lt;h2 id=&quot;ordering-arguments-in-a-function&quot;&gt;Ordering Arguments in a Function&lt;/h2&gt;
0156 &lt;p&gt;Now that you have learned what &lt;code&gt;*args&lt;/code&gt; and &lt;code&gt;**kwargs&lt;/code&gt; are for, you are ready to start writing functions that take a varying number of input arguments. But what if you want to create a function that takes a changeable number of both positional &lt;em&gt;and&lt;/em&gt; named arguments?&lt;/p&gt;
0157 &lt;p&gt;In this case, you have to bear in mind that &lt;strong&gt;order counts&lt;/strong&gt;. Just as non-default arguments have to precede default arguments, so &lt;code&gt;*args&lt;/code&gt; must come before &lt;code&gt;**kwargs&lt;/code&gt;.&lt;/p&gt;
0158 &lt;p&gt;To recap, the correct order for your parameters is:&lt;/p&gt;
0159 &lt;ol&gt;
0160 &lt;li&gt;Standard arguments&lt;/li&gt;
0161 &lt;li&gt;&lt;code&gt;*args&lt;/code&gt; arguments&lt;/li&gt;
0162 &lt;li&gt;&lt;code&gt;**kwargs&lt;/code&gt; arguments&lt;/li&gt;
0163 &lt;/ol&gt;
0164 &lt;p&gt;For example, this function definition is correct:&lt;/p&gt;
0165 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# correct_function_definition.py&lt;/span&gt;
0166 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
0167     &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
0168 &lt;/pre&gt;&lt;/div&gt;
0169 
0170 &lt;p&gt;The &lt;code&gt;*args&lt;/code&gt; variable is appropriately listed before &lt;code&gt;**kwargs&lt;/code&gt;. But what if you try to modify the order of the arguments? For example, consider the following function:&lt;/p&gt;
0171 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# wrong_function_definition.py&lt;/span&gt;
0172 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
0173     &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
0174 &lt;/pre&gt;&lt;/div&gt;
0175 
0176 &lt;p&gt;Now, &lt;code&gt;**kwargs&lt;/code&gt; comes before &lt;code&gt;*args&lt;/code&gt; in the function definition. If you try to run this example, you&amp;rsquo;ll receive an error from the interpreter:&lt;/p&gt;
0177 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python wrong_function_definition.py
0178 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;wrong_function_definition.py&amp;quot;, line 2&lt;/span&gt;
0179 &lt;span class=&quot;go&quot;&gt;    def my_function(a, b, **kwargs, *args):&lt;/span&gt;
0180 &lt;span class=&quot;go&quot;&gt;                                    ^&lt;/span&gt;
0181 &lt;span class=&quot;go&quot;&gt;SyntaxError: invalid syntax&lt;/span&gt;
0182 &lt;/pre&gt;&lt;/div&gt;
0183 
0184 &lt;p&gt;In this case, since &lt;code&gt;*args&lt;/code&gt; comes after &lt;code&gt;**kwargs&lt;/code&gt;, the Python interpreter throws a &lt;code&gt;SyntaxError&lt;/code&gt;.&lt;/p&gt;
0185 &lt;h2 id=&quot;unpacking-with-the-asterisk-operators&quot;&gt;Unpacking With the Asterisk Operators: &lt;code&gt;*&lt;/code&gt; &amp;amp; &lt;code&gt;**&lt;/code&gt;&lt;/h2&gt;
0186 &lt;p&gt;You are now able to use &lt;code&gt;*args&lt;/code&gt; and &lt;code&gt;**kwargs&lt;/code&gt; to define Python functions that take a varying number of input arguments. Let&amp;rsquo;s go a little deeper to understand something more about the &lt;strong&gt;unpacking operators&lt;/strong&gt;.&lt;/p&gt;
0187 &lt;p&gt;The single and double asterisk unpacking operators were introduced in Python 2. As of the 3.5 release, they have become even more powerful, thanks to &lt;a href=&quot;https://www.python.org/dev/peps/pep-0448/&quot;&gt;PEP 448&lt;/a&gt;. In short, the unpacking operators are operators that unpack the values from iterable objects in Python. The single asterisk operator &lt;code&gt;*&lt;/code&gt; can be used on any iterable that Python provides, while the double asterisk operator &lt;code&gt;**&lt;/code&gt; can only be used on dictionaries.&lt;/p&gt;
0188 &lt;p&gt;Let&amp;rsquo;s start with an example:&lt;/p&gt;
0189 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# print_list.py&lt;/span&gt;
0190 &lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0191 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0192 &lt;/pre&gt;&lt;/div&gt;
0193 
0194 &lt;p&gt;This code defines a list and then prints it to the standard output:&lt;/p&gt;
0195 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python print_list.py
0196 &lt;span class=&quot;go&quot;&gt;[1, 2, 3]&lt;/span&gt;
0197 &lt;/pre&gt;&lt;/div&gt;
0198 
0199 &lt;p&gt;Note how the list is printed, along with the corresponding brackets and commas.&lt;/p&gt;
0200 &lt;p&gt;Now, try to prepend the unpacking operator &lt;code&gt;*&lt;/code&gt; to the name of your list:&lt;/p&gt;
0201 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# print_unpacked_list.py&lt;/span&gt;
0202 &lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0203 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0204 &lt;/pre&gt;&lt;/div&gt;
0205 
0206 &lt;p&gt;Here, the &lt;code&gt;*&lt;/code&gt; operator tells &lt;code&gt;print()&lt;/code&gt; to unpack the list first.&lt;/p&gt;
0207 &lt;p&gt;In this case, the output is no longer the list itself, but rather &lt;em&gt;the content&lt;/em&gt; of the list:&lt;/p&gt;
0208 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python print_unpacked_list.py
0209 &lt;span class=&quot;go&quot;&gt;1 2 3&lt;/span&gt;
0210 &lt;/pre&gt;&lt;/div&gt;
0211 
0212 &lt;p&gt;Can you see the difference between this execution and the one from &lt;code&gt;print_list.py&lt;/code&gt;? Instead of a list, &lt;code&gt;print()&lt;/code&gt; has taken three separate arguments as the input.&lt;/p&gt;
0213 &lt;p&gt;Another thing you&amp;rsquo;ll notice is that in &lt;code&gt;print_unpacked_list.py&lt;/code&gt;, you used the unpacking operator &lt;code&gt;*&lt;/code&gt; to call a function, instead of in a function definition. In this case, &lt;code&gt;print()&lt;/code&gt; takes all the items of a list as though they were single arguments.&lt;/p&gt;
0214 &lt;p&gt;You can also use this method to call your own functions, but if your function requires a specific number of arguments, then the iterable you unpack must have the same number of arguments.&lt;/p&gt;
0215 &lt;p&gt;To test this behavior, consider this script:&lt;/p&gt;
0216 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# unpacking_call.py&lt;/span&gt;
0217 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
0218     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0219 
0220 &lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0221 &lt;span class=&quot;n&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0222 &lt;/pre&gt;&lt;/div&gt;
0223 
0224 &lt;p&gt;Here, &lt;code&gt;my_sum()&lt;/code&gt; explicitly states that &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, and &lt;code&gt;c&lt;/code&gt; are required arguments.&lt;/p&gt;
0225 &lt;p&gt;If you run this script, you&amp;rsquo;ll get the sum of the three numbers in &lt;code&gt;my_list&lt;/code&gt;:&lt;/p&gt;
0226 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python unpacking_call.py
0227 &lt;span class=&quot;go&quot;&gt;6&lt;/span&gt;
0228 &lt;/pre&gt;&lt;/div&gt;
0229 
0230 &lt;p&gt;The 3 elements in &lt;code&gt;my_list&lt;/code&gt; match up perfectly with the required arguments in &lt;code&gt;my_sum()&lt;/code&gt;.&lt;/p&gt;
0231 &lt;p&gt;Now look at the following script, where &lt;code&gt;my_list&lt;/code&gt; has 4 arguments instead of 3:&lt;/p&gt;
0232 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# wrong_unpacking_call.py&lt;/span&gt;
0233 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
0234     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0235 
0236 &lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0237 &lt;span class=&quot;n&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0238 &lt;/pre&gt;&lt;/div&gt;
0239 
0240 &lt;p&gt;In this example, &lt;code&gt;my_sum()&lt;/code&gt; still expects just three arguments, but the &lt;code&gt;*&lt;/code&gt; operator gets 4 items from the list. If you try to execute this script, you&amp;rsquo;ll see that the Python interpreter is unable to run it:&lt;/p&gt;
0241 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python wrong_unpacking_call.py
0242 &lt;span class=&quot;go&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
0243 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;wrong_unpacking_call.py&amp;quot;, line 6, in &amp;lt;module&amp;gt;&lt;/span&gt;
0244 &lt;span class=&quot;go&quot;&gt;    my_sum(*my_list)&lt;/span&gt;
0245 &lt;span class=&quot;go&quot;&gt;TypeError: my_sum() takes 3 positional arguments but 4 were given&lt;/span&gt;
0246 &lt;/pre&gt;&lt;/div&gt;
0247 
0248 &lt;p&gt;When you use the &lt;code&gt;*&lt;/code&gt; operator to unpack a list and pass arguments to a function, it&amp;rsquo;s exactly as though you&amp;rsquo;re passing every single argument alone. This means that you can use multiple unpacking operators to get values from several lists and pass them all to a single function.&lt;/p&gt;
0249 &lt;p&gt;To test this behavior, consider the following example:&lt;/p&gt;
0250 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# sum_integers_args_3.py&lt;/span&gt;
0251 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
0252     &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
0253     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0254         &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
0255     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;
0256 
0257 &lt;span class=&quot;n&quot;&gt;list1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0258 &lt;span class=&quot;n&quot;&gt;list2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0259 &lt;span class=&quot;n&quot;&gt;list3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0260 
0261 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
0262 &lt;/pre&gt;&lt;/div&gt;
0263 
0264 &lt;p&gt;If you run this example, all three lists are unpacked. Each individual item is passed to &lt;code&gt;my_sum()&lt;/code&gt;, resulting in the following output:&lt;/p&gt;
0265 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python sum_integers_args_3.py
0266 &lt;span class=&quot;go&quot;&gt;45&lt;/span&gt;
0267 &lt;/pre&gt;&lt;/div&gt;
0268 
0269 &lt;p&gt;There are other convenient uses of the unpacking operator. For example, say you need to split a list into three different parts. The output should show the first value, the last value, and all the values in between. With the unpacking operator, you can do this in just one line of code:&lt;/p&gt;
0270 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# extract_list_body.py&lt;/span&gt;
0271 &lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0272 
0273 &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt;
0274 
0275 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0276 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0277 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0278 &lt;/pre&gt;&lt;/div&gt;
0279 
0280 &lt;p&gt;In this example, &lt;code&gt;my_list&lt;/code&gt; contains 6 items. The first variable is assigned to &lt;code&gt;a&lt;/code&gt;, the last to &lt;code&gt;c&lt;/code&gt;, and all other values are packed into a new list &lt;code&gt;b&lt;/code&gt;. If you run the &lt;a href=&quot;https://realpython.com/run-python-scripts/&quot;&gt;script&lt;/a&gt;, &lt;code&gt;print()&lt;/code&gt; will show you that your three variables have the values you would expect:&lt;/p&gt;
0281 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python extract_list_body.py
0282 &lt;span class=&quot;go&quot;&gt;1&lt;/span&gt;
0283 &lt;span class=&quot;go&quot;&gt;[2, 3, 4, 5]&lt;/span&gt;
0284 &lt;span class=&quot;go&quot;&gt;6&lt;/span&gt;
0285 &lt;/pre&gt;&lt;/div&gt;
0286 
0287 &lt;p&gt;Another interesting thing you can do with the unpacking operator &lt;code&gt;*&lt;/code&gt; is to split the items of any iterable object. This could be very useful if you need to merge two lists, for instance:&lt;/p&gt;
0288 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# merging_lists.py&lt;/span&gt;
0289 &lt;span class=&quot;n&quot;&gt;my_first_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0290 &lt;span class=&quot;n&quot;&gt;my_second_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0291 &lt;span class=&quot;n&quot;&gt;my_merged_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_first_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_second_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0292 
0293 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_merged_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0294 &lt;/pre&gt;&lt;/div&gt;
0295 
0296 &lt;p&gt;The unpacking operator &lt;code&gt;*&lt;/code&gt; is prepended to both &lt;code&gt;my_first_list&lt;/code&gt; and &lt;code&gt;my_second_list&lt;/code&gt;.&lt;/p&gt;
0297 &lt;p&gt;If you run this script, you&amp;rsquo;ll see that the result is a merged list:&lt;/p&gt;
0298 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python merging_lists.py
0299 &lt;span class=&quot;go&quot;&gt;[1, 2, 3, 4, 5, 6]&lt;/span&gt;
0300 &lt;/pre&gt;&lt;/div&gt;
0301 
0302 &lt;p&gt;You can even merge two different dictionaries by using the unpacking operator &lt;code&gt;**&lt;/code&gt;:&lt;/p&gt;
0303 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# merging_dicts.py&lt;/span&gt;
0304 &lt;span class=&quot;n&quot;&gt;my_first_dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;B&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
0305 &lt;span class=&quot;n&quot;&gt;my_second_dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;C&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;D&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
0306 &lt;span class=&quot;n&quot;&gt;my_merged_dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_first_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_second_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
0307 
0308 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_merged_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0309 &lt;/pre&gt;&lt;/div&gt;
0310 
0311 &lt;p&gt;Here, the iterables to merge are &lt;code&gt;my_first_dict&lt;/code&gt; and &lt;code&gt;my_second_dict&lt;/code&gt;.&lt;/p&gt;
0312 &lt;p&gt;Executing this code outputs a merged dictionary:&lt;/p&gt;
0313 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python merging_dicts.py
0314 &lt;span class=&quot;go&quot;&gt;{&amp;#39;A&amp;#39;: 1, &amp;#39;B&amp;#39;: 2, &amp;#39;C&amp;#39;: 3, &amp;#39;D&amp;#39;: 4}&lt;/span&gt;
0315 &lt;/pre&gt;&lt;/div&gt;
0316 
0317 &lt;p&gt;Remember that the &lt;code&gt;*&lt;/code&gt; operator works on &lt;em&gt;any&lt;/em&gt; iterable object. It can also be used to unpack a &lt;a href=&quot;https://realpython.com/python-strings/&quot;&gt;string&lt;/a&gt;:&lt;/p&gt;
0318 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# string_to_list.py&lt;/span&gt;
0319 &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;RealPython&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0320 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0321 &lt;/pre&gt;&lt;/div&gt;
0322 
0323 &lt;p&gt;In Python, strings are iterable objects, so &lt;code&gt;*&lt;/code&gt; will unpack it and place all individual values in a list &lt;code&gt;a&lt;/code&gt;:&lt;/p&gt;
0324 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python string_to_list.py
0325 &lt;span class=&quot;go&quot;&gt;[&amp;#39;R&amp;#39;, &amp;#39;e&amp;#39;, &amp;#39;a&amp;#39;, &amp;#39;l&amp;#39;, &amp;#39;P&amp;#39;, &amp;#39;y&amp;#39;, &amp;#39;t&amp;#39;, &amp;#39;h&amp;#39;, &amp;#39;o&amp;#39;, &amp;#39;n&amp;#39;]&lt;/span&gt;
0326 &lt;/pre&gt;&lt;/div&gt;
0327 
0328 &lt;p&gt;The previous example seems great, but when you work with these operators it&amp;rsquo;s important to keep in mind the seventh rule of &lt;a href=&quot;https://www.python.org/dev/peps/pep-0020/&quot;&gt;&lt;em&gt;The Zen of Python&lt;/em&gt;&lt;/a&gt; by Tim Peters: &lt;em&gt;Readability counts&lt;/em&gt;.&lt;/p&gt;
0329 &lt;p&gt;To see why, consider the following example:&lt;/p&gt;
0330 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# mysterious_statement.py&lt;/span&gt;
0331 &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;RealPython&amp;quot;&lt;/span&gt;
0332 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0333 &lt;/pre&gt;&lt;/div&gt;
0334 
0335 &lt;p&gt;There&amp;rsquo;s the unpacking operator &lt;code&gt;*&lt;/code&gt;, followed by a variable, a comma, and an assignment. That&amp;rsquo;s a lot packed into one line! In fact, this code is no different from the previous example. It just takes the string &lt;code&gt;RealPython&lt;/code&gt; and assigns all the items to the new list &lt;code&gt;a&lt;/code&gt;, thanks to the unpacking operator &lt;code&gt;*&lt;/code&gt;.&lt;/p&gt;
0336 &lt;p&gt;The comma after the &lt;code&gt;a&lt;/code&gt; does the trick. When you use the unpacking operator with variable assignment, Python requires that your resulting variable is either a list or a tuple. With the trailing comma, you have actually defined a tuple with just one named variable &lt;code&gt;a&lt;/code&gt;.&lt;/p&gt;
0337 &lt;p&gt;While this is a neat trick, many Pythonistas would not consider this code to be very readable. As such, it&amp;rsquo;s best to use these kinds of constructions sparingly.&lt;/p&gt;
0338 &lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
0339 &lt;p&gt;You are now able to use &lt;strong&gt;&lt;code&gt;*args&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;**kwargs&lt;/code&gt;&lt;/strong&gt; to accept a changeable number of arguments in your functions. You have also learned something more about the unpacking operators. &lt;/p&gt;
0340 &lt;p&gt;You&amp;rsquo;ve learned:&lt;/p&gt;
0341 &lt;ul&gt;
0342 &lt;li&gt;What &lt;code&gt;*args&lt;/code&gt; and &lt;code&gt;**kwargs&lt;/code&gt; actually mean&lt;/li&gt;
0343 &lt;li&gt;How to use &lt;code&gt;*args&lt;/code&gt; and &lt;code&gt;**kwargs&lt;/code&gt; in function definitions&lt;/li&gt;
0344 &lt;li&gt;How to use a single asterisk (&lt;code&gt;*&lt;/code&gt;) to unpack iterables&lt;/li&gt;
0345 &lt;li&gt;How to use two asterisks (&lt;code&gt;**&lt;/code&gt;) to unpack dictionaries&lt;/li&gt;
0346 &lt;/ul&gt;
0347 &lt;p&gt;If you still have questions, don&amp;rsquo;t hesitate to reach out in the comments section below! To learn more about the use of the asterisks in Python, have a look at &lt;a href=&quot;https://treyhunner.com/2018/10/asterisks-in-python-what-they-are-and-how-to-use-them/&quot;&gt;Trey Hunner&amp;rsquo;s article on the subject&lt;/a&gt;.&lt;/p&gt;
0348         &lt;hr /&gt;
0349         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
0350       </content>
0351     </entry>
0352   
0353     <entry>
0354       <title>Lists and Tuples in Python</title>
0355       <id>https://realpython.com/courses/lists-tuples-python/</id>
0356       <link href="https://realpython.com/courses/lists-tuples-python/"/>
0357       <updated>2019-09-03T14:00:00+00:00</updated>
0358       <summary>In this course, you&#39;ll cover the important characteristics of lists and tuples in Python 3. You&#39;ll learn how to define them and how to manipulate them. When you&#39;re finished, you&#39;ll have a good feel for when and how to use these object types in a Python program.</summary>
0359       <content type="html">
0360         &lt;p&gt;In this course, you&amp;rsquo;ll learn about working with lists and tuples. &lt;strong&gt;Lists&lt;/strong&gt; and &lt;strong&gt;tuples&lt;/strong&gt; are arguably Python&amp;rsquo;s most versatile, useful &lt;a href=&quot;https://realpython.com/python-data-types/&quot;&gt;data types&lt;/a&gt;. You&amp;rsquo;ll find them in virtually every non-trivial Python program.&lt;/p&gt;
0361 &lt;p&gt;&lt;strong&gt;Here&amp;rsquo;s what you&amp;rsquo;ll learn in this tutorial:&lt;/strong&gt; You&amp;rsquo;ll cover the important characteristics of lists and tuples. You&amp;rsquo;ll learn how to define them and how to manipulate them.  When you&amp;rsquo;re finished, you&amp;rsquo;ll have a good feel for when and how to use these object types in a Python program.&lt;/p&gt;
0362 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
0363 &lt;p&gt;&lt;strong&gt;&lt;i class=&quot;fa fa-graduation-cap&quot; aria-hidden=&quot;true&quot;&gt;&lt;/i&gt; Take the Quiz:&lt;/strong&gt; Test your knowledge with our interactive โ€œPython Lists and Tuplesโ€ quiz. Upon completion you will receive a score so you can track your learning progress over time:&lt;/p&gt;&lt;p class=&quot;text-center my-2&quot;&gt;&lt;a class=&quot;btn btn-primary&quot; href=&quot;/quizzes/python-lists-tuples/&quot; target=&quot;_blank&quot;&gt;Take the Quiz ยป&lt;/a&gt;&lt;/p&gt;
0364 &lt;/div&gt;
0365         &lt;hr /&gt;
0366         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
0367       </content>
0368     </entry>
0369   
0370     <entry>
0371       <title>Natural Language Processing With spaCy in Python</title>
0372       <id>https://realpython.com/natural-language-processing-spacy-python/</id>
0373       <link href="https://realpython.com/natural-language-processing-spacy-python/"/>
0374       <updated>2019-09-02T14:00:00+00:00</updated>
0375       <summary>In this step-by-step tutorial, you&#39;ll learn how to use spaCy. This free and open-source library for Natural Language Processing (NLP) in Python has a lot of built-in capabilities and is becoming increasingly popular for processing and analyzing data in NLP.</summary>
0376       <content type="html">
0377         &lt;p&gt;&lt;strong&gt;spaCy&lt;/strong&gt; is a free and open-source library for &lt;strong&gt;Natural Language Processing&lt;/strong&gt; (NLP) in Python with a lot of in-built capabilities. It&amp;rsquo;s becoming increasingly popular for processing and analyzing data in NLP. Unstructured textual data is produced at a large scale, and it&amp;rsquo;s important to process and derive insights from unstructured data. To do that, you need to represent the data in a format that can be understood by computers. NLP can help you do that.&lt;/p&gt;
0378 &lt;p&gt;&lt;strong&gt;In this tutorial, you&amp;rsquo;ll learn:&lt;/strong&gt;&lt;/p&gt;
0379 &lt;ul&gt;
0380 &lt;li&gt;What the foundational terms and concepts in NLP are&lt;/li&gt;
0381 &lt;li&gt;How to implement those concepts in spaCy&lt;/li&gt;
0382 &lt;li&gt;How to customize and extend built-in functionalities in spaCy&lt;/li&gt;
0383 &lt;li&gt;How to perform basic statistical analysis on a text&lt;/li&gt;
0384 &lt;li&gt;How to create a pipeline to process unstructured text&lt;/li&gt;
0385 &lt;li&gt;How to parse a sentence and extract meaningful insights from it&lt;/li&gt;
0386 &lt;/ul&gt;
0387 &lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Free Bonus:&lt;/strong&gt; &lt;a href=&quot;&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-tricks-sample&quot; data-focus=&quot;false&quot;&gt;Click here to get access to a chapter from Python Tricks: The Book&lt;/a&gt; that shows you Python&#39;s best practices with simple examples you can apply instantly to write more beautiful + Pythonic code.&lt;/p&gt;&lt;/div&gt;
0388 
0389 &lt;h2 id=&quot;what-are-nlp-and-spacy&quot;&gt;What Are NLP and spaCy?&lt;/h2&gt;
0390 &lt;p&gt;&lt;strong&gt;NLP&lt;/strong&gt; is a subfield of &lt;strong&gt;Artificial Intelligence&lt;/strong&gt; and is concerned with interactions between computers and human languages. NLP is the process of analyzing, understanding, and deriving meaning from human languages for computers.&lt;/p&gt;
0391 &lt;p&gt;NLP helps you extract insights from unstructured text and has several use cases, such as:&lt;/p&gt;
0392 &lt;ul&gt;
0393 &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Automatic_summarization&quot;&gt;Automatic summarization&lt;/a&gt;&lt;/li&gt;
0394 &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Named-entity_recognition&quot;&gt;Named entity recognition&lt;/a&gt;&lt;/li&gt;
0395 &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Question_answering&quot;&gt;Question answering systems&lt;/a&gt;&lt;/li&gt;
0396 &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Sentiment_analysis&quot;&gt;Sentiment analysis&lt;/a&gt;&lt;/li&gt;
0397 &lt;/ul&gt;
0398 &lt;p&gt;spaCy is a free, open-source library for NLP in Python. It&amp;rsquo;s written in &lt;a href=&quot;https://cython.org/&quot;&gt;Cython&lt;/a&gt; and is designed to build information extraction or natural language understanding systems. It&amp;rsquo;s built for production use and provides a concise and user-friendly API.&lt;/p&gt;
0399 &lt;h2 id=&quot;installation&quot;&gt;Installation&lt;/h2&gt;
0400 &lt;p&gt;In this section, you&amp;rsquo;ll install spaCy and then download data and models for the English language.&lt;/p&gt;
0401 &lt;h3 id=&quot;how-to-install-spacy&quot;&gt;How to Install spaCy&lt;/h3&gt;
0402 &lt;p&gt;spaCy can be installed using &lt;strong&gt;&lt;code&gt;pip&lt;/code&gt;&lt;/strong&gt;, a Python package manager. You can use a &lt;strong&gt;virtual environment&lt;/strong&gt; to avoid depending on system-wide packages. To learn more about virtual environments and &lt;code&gt;pip&lt;/code&gt;, check out &lt;a href=&quot;https://realpython.com/what-is-pip/&quot;&gt;What Is Pip? A Guide for New Pythonistas&lt;/a&gt; and &lt;a href=&quot;https://realpython.com/python-virtual-environments-a-primer/&quot;&gt;Python Virtual Environments: A Primer&lt;/a&gt;.&lt;/p&gt;
0403 &lt;p&gt;Create a new virtual environment:&lt;/p&gt;
0404 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python3 -m venv env
0405 &lt;/pre&gt;&lt;/div&gt;
0406 
0407 &lt;p&gt;Activate this virtual environment and install spaCy:&lt;/p&gt;
0408 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; ./env/bin/activate
0409 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pip install spacy
0410 &lt;/pre&gt;&lt;/div&gt;
0411 
0412 &lt;h3 id=&quot;how-to-download-models-and-data&quot;&gt;How to Download Models and Data&lt;/h3&gt;
0413 &lt;p&gt;spaCy has &lt;a href=&quot;https://spaCy.io/models&quot;&gt;different types&lt;/a&gt; of models. The default model for the English language is &lt;code&gt;en_core_web_sm&lt;/code&gt;.&lt;/p&gt;
0414 &lt;p&gt;Activate the virtual environment created in the previous step and download models and data for the English language:&lt;/p&gt;
0415 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python -m spacy download en_core_web_sm
0416 &lt;/pre&gt;&lt;/div&gt;
0417 
0418 &lt;p&gt;Verify if the download was successful or not by loading it:&lt;/p&gt;
0419 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;spacy&lt;/span&gt;
0420 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;en_core_web_sm&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0421 &lt;/pre&gt;&lt;/div&gt;
0422 
0423 &lt;p&gt;If the &lt;code&gt;nlp&lt;/code&gt; object is created, then it means that spaCy was installed and that models and data were successfully downloaded.&lt;/p&gt;
0424 &lt;h2 id=&quot;using-spacy&quot;&gt;Using spaCy&lt;/h2&gt;
0425 &lt;p&gt;In this section, you&amp;rsquo;ll use spaCy for a given input string and a text file. Load the language model instance in spaCy:&lt;/p&gt;
0426 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;spacy&lt;/span&gt;
0427 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;en_core_web_sm&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0428 &lt;/pre&gt;&lt;/div&gt;
0429 
0430 &lt;p&gt;Here, the &lt;code&gt;nlp&lt;/code&gt; object is a language model instance. You can assume that, throughout this tutorial, &lt;code&gt;nlp&lt;/code&gt; refers to the language model loaded by &lt;code&gt;en_core_web_sm&lt;/code&gt;. Now you can use spaCy to read a string or a text file.&lt;/p&gt;
0431 &lt;h3 id=&quot;how-to-read-a-string&quot;&gt;How to Read a String&lt;/h3&gt;
0432 &lt;p&gt;You can use spaCy to create a processed &lt;a href=&quot;https://spaCy.io/api/doc&quot;&gt;Doc&lt;/a&gt; object, which is a container for accessing linguistic annotations, for a given input string:&lt;/p&gt;
0433 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;introduction_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;This tutorial is about Natural&amp;#39;&lt;/span&gt;
0434 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; Language Processing in Spacy.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0435 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;introduction_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;introduction_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0436 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Extract tokens for the given doc&lt;/span&gt;
0437 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;introduction_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
0438 &lt;span class=&quot;go&quot;&gt;[&amp;#39;This&amp;#39;, &amp;#39;tutorial&amp;#39;, &amp;#39;is&amp;#39;, &amp;#39;about&amp;#39;, &amp;#39;Natural&amp;#39;, &amp;#39;Language&amp;#39;,&lt;/span&gt;
0439 &lt;span class=&quot;go&quot;&gt;&amp;#39;Processing&amp;#39;, &amp;#39;in&amp;#39;, &amp;#39;Spacy&amp;#39;, &amp;#39;.&amp;#39;]&lt;/span&gt;
0440 &lt;/pre&gt;&lt;/div&gt;
0441 
0442 &lt;p&gt;In the above example, notice how the text is converted to an object that is understood by spaCy. You can use this method to convert any text into a processed &lt;code&gt;Doc&lt;/code&gt; object and deduce attributes, which will be covered in the coming sections.&lt;/p&gt;
0443 &lt;h3 id=&quot;how-to-read-a-text-file&quot;&gt;How to Read a Text File&lt;/h3&gt;
0444 &lt;p&gt;In this section, you&amp;rsquo;ll create a processed &lt;a href=&quot;https://spaCy.io/api/doc&quot;&gt;Doc&lt;/a&gt; object for a text file:&lt;/p&gt;
0445 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;introduction.txt&amp;#39;&lt;/span&gt;
0446 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;introduction_file_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
0447 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;introduction_file_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;introduction_file_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0448 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Extract tokens for the given doc&lt;/span&gt;
0449 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;introduction_file_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
0450 &lt;span class=&quot;go&quot;&gt;[&amp;#39;This&amp;#39;, &amp;#39;tutorial&amp;#39;, &amp;#39;is&amp;#39;, &amp;#39;about&amp;#39;, &amp;#39;Natural&amp;#39;, &amp;#39;Language&amp;#39;,&lt;/span&gt;
0451 &lt;span class=&quot;go&quot;&gt;&amp;#39;Processing&amp;#39;, &amp;#39;in&amp;#39;, &amp;#39;Spacy&amp;#39;, &amp;#39;.&amp;#39;, &amp;#39;\n&amp;#39;]&lt;/span&gt;
0452 &lt;/pre&gt;&lt;/div&gt;
0453 
0454 &lt;p&gt;This is how you can convert a text file into a processed &lt;code&gt;Doc&lt;/code&gt; object.&lt;/p&gt;
0455 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
0456 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;/p&gt;
0457 &lt;p&gt;You can assume that:&lt;/p&gt;
0458 &lt;ul&gt;
0459 &lt;li&gt;Variable names ending with the suffix &lt;strong&gt;&lt;code&gt;_text&lt;/code&gt;&lt;/strong&gt; are &lt;strong&gt;&lt;a href=&quot;https://realpython.com/python-encodings-guide/&quot;&gt;Unicode&lt;/a&gt; string objects&lt;/strong&gt;.&lt;/li&gt;
0460 &lt;li&gt;Variable name ending with the suffix &lt;strong&gt;&lt;code&gt;_doc&lt;/code&gt;&lt;/strong&gt; are &lt;strong&gt;spaCy&amp;rsquo;s language model objects&lt;/strong&gt;.&lt;/li&gt;
0461 &lt;/ul&gt;
0462 &lt;/div&gt;
0463 &lt;h2 id=&quot;sentence-detection&quot;&gt;Sentence Detection&lt;/h2&gt;
0464 &lt;p&gt;&lt;strong&gt;Sentence Detection&lt;/strong&gt; is the process of locating the start and end of sentences in a given text. This allows you to you divide a text into linguistically meaningful units. You&amp;rsquo;ll use these units when you&amp;rsquo;re processing your text to perform tasks such as &lt;strong&gt;part of speech tagging&lt;/strong&gt; and &lt;strong&gt;entity extraction&lt;/strong&gt;.&lt;/p&gt;
0465 &lt;p&gt;In spaCy, the &lt;code&gt;sents&lt;/code&gt; property is used to extract sentences. Here&amp;rsquo;s how you would extract the total number of sentences and the sentences for a given input text:&lt;/p&gt;
0466 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Gus Proto is a Python developer currently&amp;#39;&lt;/span&gt;
0467 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;              &lt;span class=&quot;s1&quot;&gt;&amp;#39; working for a London-based Fintech&amp;#39;&lt;/span&gt;
0468 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;              &lt;span class=&quot;s1&quot;&gt;&amp;#39; company. He is interested in learning&amp;#39;&lt;/span&gt;
0469 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;              &lt;span class=&quot;s1&quot;&gt;&amp;#39; Natural Language Processing.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0470 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0471 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentences&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_doc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0472 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentences&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0473 &lt;span class=&quot;go&quot;&gt;2&lt;/span&gt;
0474 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sentences&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0475 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0476 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
0477 &lt;span class=&quot;go&quot;&gt;&amp;#39;Gus Proto is a Python developer currently working for a&lt;/span&gt;
0478 &lt;span class=&quot;go&quot;&gt;London-based Fintech company.&amp;#39;&lt;/span&gt;
0479 &lt;span class=&quot;go&quot;&gt;&amp;#39;He is interested in learning Natural Language Processing.&amp;#39;&lt;/span&gt;
0480 &lt;/pre&gt;&lt;/div&gt;
0481 
0482 &lt;p&gt;In the above example, spaCy is correctly able to identify sentences in the English language, using a full stop(&lt;code&gt;.&lt;/code&gt;) as the sentence delimiter. You can also customize the sentence detection to detect sentences on custom delimiters.&lt;/p&gt;
0483 &lt;p&gt;Here&amp;rsquo;s an example, where an ellipsis(&lt;code&gt;...&lt;/code&gt;) is used as the delimiter:&lt;/p&gt;
0484 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;set_custom_boundaries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
0485 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Adds support to use `...` as the delimiter for sentence detection&lt;/span&gt;
0486 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
0487 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;...&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0488 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_sent_start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;
0489 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;doc&lt;/span&gt;
0490 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
0491 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ellipsis_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Gus, can you, ... never mind, I forgot&amp;#39;&lt;/span&gt;
0492 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                 &lt;span class=&quot;s1&quot;&gt;&amp;#39; what I was saying. So, do you think&amp;#39;&lt;/span&gt;
0493 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                 &lt;span class=&quot;s1&quot;&gt;&amp;#39; we should ...&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0494 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Load a new model instance&lt;/span&gt;
0495 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_nlp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;en_core_web_sm&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0496 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_nlp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_pipe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_custom_boundaries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;before&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;parser&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0497 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_ellipsis_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;custom_nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ellipsis_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0498 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_ellipsis_sentences&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_ellipsis_doc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0499 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;custom_ellipsis_sentences&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0500 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0501 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
0502 &lt;span class=&quot;go&quot;&gt;Gus, can you, ...&lt;/span&gt;
0503 &lt;span class=&quot;go&quot;&gt;never mind, I forgot what I was saying.&lt;/span&gt;
0504 &lt;span class=&quot;go&quot;&gt;So, do you think we should ...&lt;/span&gt;
0505 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Sentence Detection with no customization&lt;/span&gt;
0506 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ellipsis_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ellipsis_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0507 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ellipsis_sentences&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ellipsis_doc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0508 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ellipsis_sentences&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0509 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0510 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
0511 &lt;span class=&quot;go&quot;&gt;Gus, can you, ... never mind, I forgot what I was saying.&lt;/span&gt;
0512 &lt;span class=&quot;go&quot;&gt;So, do you think we should ...&lt;/span&gt;
0513 &lt;/pre&gt;&lt;/div&gt;
0514 
0515 &lt;p&gt;Note that &lt;code&gt;custom_ellipsis_sentences&lt;/code&gt; contain three sentences, whereas &lt;code&gt;ellipsis_sentences&lt;/code&gt; contains two sentences. These sentences are still obtained via the &lt;code&gt;sents&lt;/code&gt; attribute, as you saw before.&lt;/p&gt;
0516 &lt;h2 id=&quot;tokenization-in-spacy&quot;&gt;Tokenization in spaCy&lt;/h2&gt;
0517 &lt;p&gt;&lt;strong&gt;Tokenization&lt;/strong&gt; is the next step after sentence detection. It allows you to identify the basic units in your text. These basic units are called &lt;strong&gt;tokens&lt;/strong&gt;. Tokenization is useful because it breaks a text into meaningful units. These units are used for further analysis, like part of speech tagging.&lt;/p&gt;
0518 &lt;p&gt;In spaCy, you can print tokens by iterating on the &lt;code&gt;Doc&lt;/code&gt; object:&lt;/p&gt;
0519 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0520 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0521 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
0522 &lt;span class=&quot;go&quot;&gt;Gus 0&lt;/span&gt;
0523 &lt;span class=&quot;go&quot;&gt;Proto 4&lt;/span&gt;
0524 &lt;span class=&quot;go&quot;&gt;is 10&lt;/span&gt;
0525 &lt;span class=&quot;go&quot;&gt;a 13&lt;/span&gt;
0526 &lt;span class=&quot;go&quot;&gt;Python 15&lt;/span&gt;
0527 &lt;span class=&quot;go&quot;&gt;developer 22&lt;/span&gt;
0528 &lt;span class=&quot;go&quot;&gt;currently 32&lt;/span&gt;
0529 &lt;span class=&quot;go&quot;&gt;working 42&lt;/span&gt;
0530 &lt;span class=&quot;go&quot;&gt;for 50&lt;/span&gt;
0531 &lt;span class=&quot;go&quot;&gt;a 54&lt;/span&gt;
0532 &lt;span class=&quot;go&quot;&gt;London 56&lt;/span&gt;
0533 &lt;span class=&quot;go&quot;&gt;- 62&lt;/span&gt;
0534 &lt;span class=&quot;go&quot;&gt;based 63&lt;/span&gt;
0535 &lt;span class=&quot;go&quot;&gt;Fintech 69&lt;/span&gt;
0536 &lt;span class=&quot;go&quot;&gt;company 77&lt;/span&gt;
0537 &lt;span class=&quot;go&quot;&gt;. 84&lt;/span&gt;
0538 &lt;span class=&quot;go&quot;&gt;He 86&lt;/span&gt;
0539 &lt;span class=&quot;go&quot;&gt;is 89&lt;/span&gt;
0540 &lt;span class=&quot;go&quot;&gt;interested 92&lt;/span&gt;
0541 &lt;span class=&quot;go&quot;&gt;in 103&lt;/span&gt;
0542 &lt;span class=&quot;go&quot;&gt;learning 106&lt;/span&gt;
0543 &lt;span class=&quot;go&quot;&gt;Natural 115&lt;/span&gt;
0544 &lt;span class=&quot;go&quot;&gt;Language 123&lt;/span&gt;
0545 &lt;span class=&quot;go&quot;&gt;Processing 132&lt;/span&gt;
0546 &lt;span class=&quot;go&quot;&gt;. 142&lt;/span&gt;
0547 &lt;/pre&gt;&lt;/div&gt;
0548 
0549 &lt;p&gt;Note how spaCy preserves the &lt;strong&gt;starting index&lt;/strong&gt; of the tokens. It&amp;rsquo;s useful for in-place word replacement. spaCy provides &lt;a href=&quot;https://spacy.io/api/token#attributes&quot;&gt;various attributes&lt;/a&gt; for the &lt;code&gt;Token&lt;/code&gt; class:&lt;/p&gt;
0550 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0551 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text_with_ws&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
0552 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;           &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_alpha&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_punct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_space&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
0553 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;           &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0554 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
0555 &lt;span class=&quot;go&quot;&gt;Gus 0 Gus  True False False Xxx False&lt;/span&gt;
0556 &lt;span class=&quot;go&quot;&gt;Proto 4 Proto  True False False Xxxxx False&lt;/span&gt;
0557 &lt;span class=&quot;go&quot;&gt;is 10 is  True False False xx True&lt;/span&gt;
0558 &lt;span class=&quot;go&quot;&gt;a 13 a  True False False x True&lt;/span&gt;
0559 &lt;span class=&quot;go&quot;&gt;Python 15 Python  True False False Xxxxx False&lt;/span&gt;
0560 &lt;span class=&quot;go&quot;&gt;developer 22 developer  True False False xxxx False&lt;/span&gt;
0561 &lt;span class=&quot;go&quot;&gt;currently 32 currently  True False False xxxx False&lt;/span&gt;
0562 &lt;span class=&quot;go&quot;&gt;working 42 working  True False False xxxx False&lt;/span&gt;
0563 &lt;span class=&quot;go&quot;&gt;for 50 for  True False False xxx True&lt;/span&gt;
0564 &lt;span class=&quot;go&quot;&gt;a 54 a  True False False x True&lt;/span&gt;
0565 &lt;span class=&quot;go&quot;&gt;London 56 London True False False Xxxxx False&lt;/span&gt;
0566 &lt;span class=&quot;go&quot;&gt;- 62 - False True False - False&lt;/span&gt;
0567 &lt;span class=&quot;go&quot;&gt;based 63 based  True False False xxxx False&lt;/span&gt;
0568 &lt;span class=&quot;go&quot;&gt;Fintech 69 Fintech  True False False Xxxxx False&lt;/span&gt;
0569 &lt;span class=&quot;go&quot;&gt;company 77 company True False False xxxx False&lt;/span&gt;
0570 &lt;span class=&quot;go&quot;&gt;. 84 .  False True False . False&lt;/span&gt;
0571 &lt;span class=&quot;go&quot;&gt;He 86 He  True False False Xx True&lt;/span&gt;
0572 &lt;span class=&quot;go&quot;&gt;is 89 is  True False False xx True&lt;/span&gt;
0573 &lt;span class=&quot;go&quot;&gt;interested 92 interested  True False False xxxx False&lt;/span&gt;
0574 &lt;span class=&quot;go&quot;&gt;in 103 in  True False False xx True&lt;/span&gt;
0575 &lt;span class=&quot;go&quot;&gt;learning 106 learning  True False False xxxx False&lt;/span&gt;
0576 &lt;span class=&quot;go&quot;&gt;Natural 115 Natural  True False False Xxxxx False&lt;/span&gt;
0577 &lt;span class=&quot;go&quot;&gt;Language 123 Language  True False False Xxxxx False&lt;/span&gt;
0578 &lt;span class=&quot;go&quot;&gt;Processing 132 Processing True False False Xxxxx False&lt;/span&gt;
0579 &lt;span class=&quot;go&quot;&gt;. 142 . False True False . False&lt;/span&gt;
0580 &lt;/pre&gt;&lt;/div&gt;
0581 
0582 &lt;p&gt;In this example, some of the commonly required attributes are accessed:&lt;/p&gt;
0583 &lt;ul&gt;
0584 &lt;li&gt;&lt;strong&gt;&lt;code&gt;text_with_ws&lt;/code&gt;&lt;/strong&gt; prints token text with trailing space (if present).&lt;/li&gt;
0585 &lt;li&gt;&lt;strong&gt;&lt;code&gt;is_alpha&lt;/code&gt;&lt;/strong&gt; detects if the token consists of alphabetic characters or not.&lt;/li&gt;
0586 &lt;li&gt;&lt;strong&gt;&lt;code&gt;is_punct&lt;/code&gt;&lt;/strong&gt; detects if the token is a punctuation symbol or not.&lt;/li&gt;
0587 &lt;li&gt;&lt;strong&gt;&lt;code&gt;is_space&lt;/code&gt;&lt;/strong&gt; detects if the token is a space or not.&lt;/li&gt;
0588 &lt;li&gt;&lt;strong&gt;&lt;code&gt;shape_&lt;/code&gt;&lt;/strong&gt; prints out the shape of the word.&lt;/li&gt;
0589 &lt;li&gt;&lt;strong&gt;&lt;code&gt;is_stop&lt;/code&gt;&lt;/strong&gt; detects if the token is a stop word or not.&lt;/li&gt;
0590 &lt;/ul&gt;
0591 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
0592 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You&amp;rsquo;ll learn more about &lt;strong&gt;stop words&lt;/strong&gt; in the next section.&lt;/p&gt;
0593 &lt;/div&gt;
0594 &lt;p&gt;You can also customize the tokenization process to detect tokens on custom characters. This is often used for hyphenated words, which are words joined with hyphen. For example, &amp;ldquo;London-based&amp;rdquo; is a hyphenated word.&lt;/p&gt;
0595 &lt;p&gt;spaCy allows you to customize tokenization by updating the &lt;code&gt;tokenizer&lt;/code&gt; property on the &lt;code&gt;nlp&lt;/code&gt; object:&lt;/p&gt;
0596 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;re&lt;/span&gt;
0597 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;spacy&lt;/span&gt;
0598 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;spacy.tokenizer&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Tokenizer&lt;/span&gt;
0599 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_nlp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;en_core_web_sm&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0600 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prefix_re&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compile_prefix_regex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_nlp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Defaults&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prefixes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0601 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;suffix_re&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compile_suffix_regex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_nlp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Defaults&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;suffixes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0602 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;infix_re&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;re&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&amp;#39;[-~]&amp;#39;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0603 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;customize_tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
0604 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Adds support to use `-` as the delimiter for tokenization&lt;/span&gt;
0605 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prefix_search&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prefix_re&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;search&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
0606 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                     &lt;span class=&quot;n&quot;&gt;suffix_search&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;suffix_re&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;search&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
0607 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                     &lt;span class=&quot;n&quot;&gt;infix_finditer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;infix_re&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;finditer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
0608 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                     &lt;span class=&quot;n&quot;&gt;token_match&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;
0609 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                     &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0610 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
0611 
0612 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_nlp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;customize_tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0613 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_tokenizer_about_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;custom_nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0614 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;custom_tokenizer_about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
0615 &lt;span class=&quot;go&quot;&gt;[&amp;#39;Gus&amp;#39;, &amp;#39;Proto&amp;#39;, &amp;#39;is&amp;#39;, &amp;#39;a&amp;#39;, &amp;#39;Python&amp;#39;, &amp;#39;developer&amp;#39;, &amp;#39;currently&amp;#39;,&lt;/span&gt;
0616 &lt;span class=&quot;go&quot;&gt;&amp;#39;working&amp;#39;, &amp;#39;for&amp;#39;, &amp;#39;a&amp;#39;, &amp;#39;London&amp;#39;, &amp;#39;-&amp;#39;, &amp;#39;based&amp;#39;, &amp;#39;Fintech&amp;#39;,&lt;/span&gt;
0617 &lt;span class=&quot;go&quot;&gt;&amp;#39;company&amp;#39;, &amp;#39;.&amp;#39;, &amp;#39;He&amp;#39;, &amp;#39;is&amp;#39;, &amp;#39;interested&amp;#39;, &amp;#39;in&amp;#39;, &amp;#39;learning&amp;#39;,&lt;/span&gt;
0618 &lt;span class=&quot;go&quot;&gt;&amp;#39;Natural&amp;#39;, &amp;#39;Language&amp;#39;, &amp;#39;Processing&amp;#39;, &amp;#39;.&amp;#39;]&lt;/span&gt;
0619 &lt;/pre&gt;&lt;/div&gt;
0620 
0621 &lt;p&gt;In order for you to customize, you can pass various parameters to the &lt;code&gt;Tokenizer&lt;/code&gt; class:&lt;/p&gt;
0622 &lt;ul&gt;
0623 &lt;li&gt;&lt;strong&gt;&lt;code&gt;nlp.vocab&lt;/code&gt;&lt;/strong&gt; is a storage container for special cases and is used to handle cases like contractions and emoticons.&lt;/li&gt;
0624 &lt;li&gt;&lt;strong&gt;&lt;code&gt;prefix_search&lt;/code&gt;&lt;/strong&gt; is the function that is used to handle preceding punctuation, such as opening parentheses.&lt;/li&gt;
0625 &lt;li&gt;&lt;strong&gt;&lt;code&gt;infix_finditer&lt;/code&gt;&lt;/strong&gt; is the function that is used to handle non-whitespace separators, such as hyphens.&lt;/li&gt;
0626 &lt;li&gt;&lt;strong&gt;&lt;code&gt;suffix_search&lt;/code&gt;&lt;/strong&gt; is the function that is used to handle succeeding punctuation, such as closing parentheses.&lt;/li&gt;
0627 &lt;li&gt;&lt;strong&gt;&lt;code&gt;token_match&lt;/code&gt;&lt;/strong&gt; is an optional boolean function that is used to match strings that should never be split. It overrides the previous rules and is useful for entities like URLs or numbers.&lt;/li&gt;
0628 &lt;/ul&gt;
0629 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
0630 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; spaCy already detects hyphenated words as individual tokens. The above code is just an example to show how tokenization can be customized. It can be used for any other character.&lt;/p&gt;
0631 &lt;/div&gt;
0632 &lt;h2 id=&quot;stop-words&quot;&gt;Stop Words&lt;/h2&gt;
0633 &lt;p&gt;&lt;strong&gt;Stop words&lt;/strong&gt; are the most common words in a language. In the English language, some examples of stop words are &lt;code&gt;the&lt;/code&gt;, &lt;code&gt;are&lt;/code&gt;, &lt;code&gt;but&lt;/code&gt;, and &lt;code&gt;they&lt;/code&gt;. Most sentences need to contain stop words in order to be full sentences that make sense.&lt;/p&gt;
0634 &lt;p&gt;Generally, stop words are removed because they aren&amp;rsquo;t significant and distort the word frequency analysis. spaCy has a list of stop words for the English language:&lt;/p&gt;
0635 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;spacy&lt;/span&gt;
0636 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spacy_stopwords&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lang&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;en&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stop_words&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STOP_WORDS&lt;/span&gt;
0637 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spacy_stopwords&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0638 &lt;span class=&quot;go&quot;&gt;326&lt;/span&gt;
0639 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stop_word&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spacy_stopwords&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
0640 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stop_word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0641 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
0642 &lt;span class=&quot;go&quot;&gt;using&lt;/span&gt;
0643 &lt;span class=&quot;go&quot;&gt;becomes&lt;/span&gt;
0644 &lt;span class=&quot;go&quot;&gt;had&lt;/span&gt;
0645 &lt;span class=&quot;go&quot;&gt;itself&lt;/span&gt;
0646 &lt;span class=&quot;go&quot;&gt;once&lt;/span&gt;
0647 &lt;span class=&quot;go&quot;&gt;often&lt;/span&gt;
0648 &lt;span class=&quot;go&quot;&gt;is&lt;/span&gt;
0649 &lt;span class=&quot;go&quot;&gt;herein&lt;/span&gt;
0650 &lt;span class=&quot;go&quot;&gt;who&lt;/span&gt;
0651 &lt;span class=&quot;go&quot;&gt;too&lt;/span&gt;
0652 &lt;/pre&gt;&lt;/div&gt;
0653 
0654 &lt;p&gt;You can remove stop words from the input text:&lt;/p&gt;
0655 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0656 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0657 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0658 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
0659 &lt;span class=&quot;go&quot;&gt;Gus&lt;/span&gt;
0660 &lt;span class=&quot;go&quot;&gt;Proto&lt;/span&gt;
0661 &lt;span class=&quot;go&quot;&gt;Python&lt;/span&gt;
0662 &lt;span class=&quot;go&quot;&gt;developer&lt;/span&gt;
0663 &lt;span class=&quot;go&quot;&gt;currently&lt;/span&gt;
0664 &lt;span class=&quot;go&quot;&gt;working&lt;/span&gt;
0665 &lt;span class=&quot;go&quot;&gt;London&lt;/span&gt;
0666 &lt;span class=&quot;go&quot;&gt;-&lt;/span&gt;
0667 &lt;span class=&quot;go&quot;&gt;based&lt;/span&gt;
0668 &lt;span class=&quot;go&quot;&gt;Fintech&lt;/span&gt;
0669 &lt;span class=&quot;go&quot;&gt;company&lt;/span&gt;
0670 &lt;span class=&quot;go&quot;&gt;.&lt;/span&gt;
0671 &lt;span class=&quot;go&quot;&gt;interested&lt;/span&gt;
0672 &lt;span class=&quot;go&quot;&gt;learning&lt;/span&gt;
0673 &lt;span class=&quot;go&quot;&gt;Natural&lt;/span&gt;
0674 &lt;span class=&quot;go&quot;&gt;Language&lt;/span&gt;
0675 &lt;span class=&quot;go&quot;&gt;Processing&lt;/span&gt;
0676 &lt;span class=&quot;go&quot;&gt;.&lt;/span&gt;
0677 &lt;/pre&gt;&lt;/div&gt;
0678 
0679 &lt;p&gt;Stop words like &lt;code&gt;is&lt;/code&gt;, &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;for&lt;/code&gt;, &lt;code&gt;the&lt;/code&gt;, and &lt;code&gt;in&lt;/code&gt; are not printed in the output above. You can also create a list of tokens not containing stop words:&lt;/p&gt;
0680 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_no_stopword_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;about_doc&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0681 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_no_stopword_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0682 &lt;span class=&quot;go&quot;&gt;[Gus, Proto, Python, developer, currently, working, London,&lt;/span&gt;
0683 &lt;span class=&quot;go&quot;&gt;-, based, Fintech, company, ., interested, learning, Natural,&lt;/span&gt;
0684 &lt;span class=&quot;go&quot;&gt;Language, Processing, .]&lt;/span&gt;
0685 &lt;/pre&gt;&lt;/div&gt;
0686 
0687 &lt;p&gt;&lt;code&gt;about_no_stopword_doc&lt;/code&gt; can be joined with spaces to form a sentence with no stop words.&lt;/p&gt;
0688 &lt;h2 id=&quot;lemmatization&quot;&gt;Lemmatization&lt;/h2&gt;
0689 &lt;p&gt;&lt;strong&gt;Lemmatization&lt;/strong&gt; is the process of reducing inflected forms of a word while still ensuring that the reduced form belongs to the language. This reduced form or root word is called a &lt;strong&gt;lemma&lt;/strong&gt;.&lt;/p&gt;
0690 &lt;p&gt;For example, &lt;em&gt;organizes&lt;/em&gt;, &lt;em&gt;organized&lt;/em&gt; and &lt;em&gt;organizing&lt;/em&gt; are all forms of &lt;em&gt;organize&lt;/em&gt;. Here, &lt;em&gt;organize&lt;/em&gt; is the lemma. The inflection of a word allows you to express different grammatical categories like tense (&lt;em&gt;organized&lt;/em&gt; vs &lt;em&gt;organize&lt;/em&gt;), number (&lt;em&gt;trains&lt;/em&gt; vs &lt;em&gt;train&lt;/em&gt;), and so on. Lemmatization is necessary because it helps you reduce the inflected forms of a word so that they can be analyzed as a single item. It can also help you &lt;strong&gt;normalize&lt;/strong&gt; the text.&lt;/p&gt;
0691 &lt;p&gt;spaCy has the attribute &lt;code&gt;lemma_&lt;/code&gt; on the &lt;code&gt;Token&lt;/code&gt; class. This attribute has the lemmatized form of a token:&lt;/p&gt;
0692 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_help_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Gus is helping organize a developer&amp;#39;&lt;/span&gt;
0693 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39;conference on Applications of Natural Language&amp;#39;&lt;/span&gt;
0694 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; Processing. He keeps organizing local Python meetups&amp;#39;&lt;/span&gt;
0695 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; and several internal talks at his workplace.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0696 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_help_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_help_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0697 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conference_help_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0698 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lemma_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0699 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
0700 &lt;span class=&quot;go&quot;&gt;Gus Gus&lt;/span&gt;
0701 &lt;span class=&quot;go&quot;&gt;is be&lt;/span&gt;
0702 &lt;span class=&quot;go&quot;&gt;helping help&lt;/span&gt;
0703 &lt;span class=&quot;go&quot;&gt;organize organize&lt;/span&gt;
0704 &lt;span class=&quot;go&quot;&gt;a a&lt;/span&gt;
0705 &lt;span class=&quot;go&quot;&gt;developer developer&lt;/span&gt;
0706 &lt;span class=&quot;go&quot;&gt;conference conference&lt;/span&gt;
0707 &lt;span class=&quot;go&quot;&gt;on on&lt;/span&gt;
0708 &lt;span class=&quot;go&quot;&gt;Applications Applications&lt;/span&gt;
0709 &lt;span class=&quot;go&quot;&gt;of of&lt;/span&gt;
0710 &lt;span class=&quot;go&quot;&gt;Natural Natural&lt;/span&gt;
0711 &lt;span class=&quot;go&quot;&gt;Language Language&lt;/span&gt;
0712 &lt;span class=&quot;go&quot;&gt;Processing Processing&lt;/span&gt;
0713 &lt;span class=&quot;go&quot;&gt;. .&lt;/span&gt;
0714 &lt;span class=&quot;go&quot;&gt;He -PRON-&lt;/span&gt;
0715 &lt;span class=&quot;go&quot;&gt;keeps keep&lt;/span&gt;
0716 &lt;span class=&quot;go&quot;&gt;organizing organize&lt;/span&gt;
0717 &lt;span class=&quot;go&quot;&gt;local local&lt;/span&gt;
0718 &lt;span class=&quot;go&quot;&gt;Python Python&lt;/span&gt;
0719 &lt;span class=&quot;go&quot;&gt;meetups meetup&lt;/span&gt;
0720 &lt;span class=&quot;go&quot;&gt;and and&lt;/span&gt;
0721 &lt;span class=&quot;go&quot;&gt;several several&lt;/span&gt;
0722 &lt;span class=&quot;go&quot;&gt;internal internal&lt;/span&gt;
0723 &lt;span class=&quot;go&quot;&gt;talks talk&lt;/span&gt;
0724 &lt;span class=&quot;go&quot;&gt;at at&lt;/span&gt;
0725 &lt;span class=&quot;go&quot;&gt;his -PRON-&lt;/span&gt;
0726 &lt;span class=&quot;go&quot;&gt;workplace workplace&lt;/span&gt;
0727 &lt;span class=&quot;go&quot;&gt;. .&lt;/span&gt;
0728 &lt;/pre&gt;&lt;/div&gt;
0729 
0730 &lt;p&gt;In this example, &lt;code&gt;organizing&lt;/code&gt; reduces to its lemma form &lt;code&gt;organize&lt;/code&gt;. If you do not lemmatize the text, then &lt;code&gt;organize&lt;/code&gt; and &lt;code&gt;organizing&lt;/code&gt; will be counted as different tokens, even though they both have a similar meaning. Lemmatization helps you avoid duplicate words that have similar meanings.&lt;/p&gt;
0731 &lt;h2 id=&quot;word-frequency&quot;&gt;Word Frequency&lt;/h2&gt;
0732 &lt;p&gt;You can now convert a given text into tokens and perform statistical analysis over it. This analysis can give you various insights about word patterns, such as common words or unique words in the text:&lt;/p&gt;
0733 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;collections&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Counter&lt;/span&gt;
0734 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;complete_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Gus Proto is a Python developer currently&amp;#39;&lt;/span&gt;
0735 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39;working for a London-based Fintech company. He is&amp;#39;&lt;/span&gt;
0736 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; interested in learning Natural Language Processing.&amp;#39;&lt;/span&gt;
0737 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; There is a developer conference happening on 21 July&amp;#39;&lt;/span&gt;
0738 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; 2019 in London. It is titled &amp;quot;Applications of Natural&amp;#39;&lt;/span&gt;
0739 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; Language Processing&amp;quot;. There is a helpline number &amp;#39;&lt;/span&gt;
0740 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; available at +1-1234567891. Gus is helping organize it.&amp;#39;&lt;/span&gt;
0741 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; He keeps organizing local Python meetups and several&amp;#39;&lt;/span&gt;
0742 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; internal talks at his workplace. Gus is also presenting&amp;#39;&lt;/span&gt;
0743 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; a talk. The talk will introduce the reader about &amp;quot;Use&amp;#39;&lt;/span&gt;
0744 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; cases of Natural Language Processing in Fintech&amp;quot;.&amp;#39;&lt;/span&gt;
0745 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; Apart from his work, he is very passionate about music.&amp;#39;&lt;/span&gt;
0746 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; Gus is learning to play the Piano. He has enrolled &amp;#39;&lt;/span&gt;
0747 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; himself in the weekend batch of Great Piano Academy.&amp;#39;&lt;/span&gt;
0748 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; Great Piano Academy is situated in Mayfair or the City&amp;#39;&lt;/span&gt;
0749 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; of London and has world-class piano instructors.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0750 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
0751 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;complete_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;complete_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0752 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Remove stop words and punctuation symbols&lt;/span&gt;
0753 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;complete_doc&lt;/span&gt;
0754 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_stop&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_punct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0755 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word_freq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0756 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 5 commonly occurring words with their frequencies&lt;/span&gt;
0757 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;common_words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word_freq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;most_common&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0758 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;common_words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0759 &lt;span class=&quot;go&quot;&gt;[(&amp;#39;Gus&amp;#39;, 4), (&amp;#39;London&amp;#39;, 3), (&amp;#39;Natural&amp;#39;, 3), (&amp;#39;Language&amp;#39;, 3), (&amp;#39;Processing&amp;#39;, 3)]&lt;/span&gt;
0760 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Unique words&lt;/span&gt;
0761 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unique_words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;freq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word_freq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;freq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0762 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unique_words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0763 &lt;span class=&quot;go&quot;&gt;[&amp;#39;Proto&amp;#39;, &amp;#39;currently&amp;#39;, &amp;#39;working&amp;#39;, &amp;#39;based&amp;#39;, &amp;#39;company&amp;#39;,&lt;/span&gt;
0764 &lt;span class=&quot;go&quot;&gt;&amp;#39;interested&amp;#39;, &amp;#39;conference&amp;#39;, &amp;#39;happening&amp;#39;, &amp;#39;21&amp;#39;, &amp;#39;July&amp;#39;,&lt;/span&gt;
0765 &lt;span class=&quot;go&quot;&gt;&amp;#39;2019&amp;#39;, &amp;#39;titled&amp;#39;, &amp;#39;Applications&amp;#39;, &amp;#39;helpline&amp;#39;, &amp;#39;number&amp;#39;,&lt;/span&gt;
0766 &lt;span class=&quot;go&quot;&gt;&amp;#39;available&amp;#39;, &amp;#39;+1&amp;#39;, &amp;#39;1234567891&amp;#39;, &amp;#39;helping&amp;#39;, &amp;#39;organize&amp;#39;,&lt;/span&gt;
0767 &lt;span class=&quot;go&quot;&gt;&amp;#39;keeps&amp;#39;, &amp;#39;organizing&amp;#39;, &amp;#39;local&amp;#39;, &amp;#39;meetups&amp;#39;, &amp;#39;internal&amp;#39;,&lt;/span&gt;
0768 &lt;span class=&quot;go&quot;&gt;&amp;#39;talks&amp;#39;, &amp;#39;workplace&amp;#39;, &amp;#39;presenting&amp;#39;, &amp;#39;introduce&amp;#39;, &amp;#39;reader&amp;#39;,&lt;/span&gt;
0769 &lt;span class=&quot;go&quot;&gt;&amp;#39;Use&amp;#39;, &amp;#39;cases&amp;#39;, &amp;#39;Apart&amp;#39;, &amp;#39;work&amp;#39;, &amp;#39;passionate&amp;#39;, &amp;#39;music&amp;#39;, &amp;#39;play&amp;#39;,&lt;/span&gt;
0770 &lt;span class=&quot;go&quot;&gt;&amp;#39;enrolled&amp;#39;, &amp;#39;weekend&amp;#39;, &amp;#39;batch&amp;#39;, &amp;#39;situated&amp;#39;, &amp;#39;Mayfair&amp;#39;, &amp;#39;City&amp;#39;,&lt;/span&gt;
0771 &lt;span class=&quot;go&quot;&gt;&amp;#39;world&amp;#39;, &amp;#39;class&amp;#39;, &amp;#39;piano&amp;#39;, &amp;#39;instructors&amp;#39;]&lt;/span&gt;
0772 &lt;/pre&gt;&lt;/div&gt;
0773 
0774 &lt;p&gt;By looking at the common words, you can see that the text as a whole is probably about &lt;code&gt;Gus&lt;/code&gt;, &lt;code&gt;London&lt;/code&gt;, or &lt;code&gt;Natural Language Processing&lt;/code&gt;. This way, you can take any unstructured text and perform statistical analysis to know what it&amp;rsquo;s about.&lt;/p&gt;
0775 &lt;p&gt;Here&amp;rsquo;s another example of the same text with stop words:&lt;/p&gt;
0776 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words_all&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;complete_doc&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_punct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0777 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word_freq_all&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words_all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0778 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 5 commonly occurring words with their frequencies&lt;/span&gt;
0779 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;common_words_all&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word_freq_all&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;most_common&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0780 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;common_words_all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0781 &lt;span class=&quot;go&quot;&gt;[(&amp;#39;is&amp;#39;, 10), (&amp;#39;a&amp;#39;, 5), (&amp;#39;in&amp;#39;, 5), (&amp;#39;Gus&amp;#39;, 4), (&amp;#39;of&amp;#39;, 4)]&lt;/span&gt;
0782 &lt;/pre&gt;&lt;/div&gt;
0783 
0784 &lt;p&gt;Four out of five of the most common words are stop words, which don&amp;rsquo;t tell you much about the text. If you consider stop words while doing word frequency analysis, then you won&amp;rsquo;t be able to derive meaningful insights from the input text. This is why removing stop words is so important.&lt;/p&gt;
0785 &lt;h2 id=&quot;part-of-speech-tagging&quot;&gt;Part of Speech Tagging&lt;/h2&gt;
0786 &lt;p&gt;&lt;strong&gt;Part of speech&lt;/strong&gt; or &lt;strong&gt;POS&lt;/strong&gt; is a grammatical role that explains how a particular word is used in a sentence. There are eight parts of speech:&lt;/p&gt;
0787 &lt;ol&gt;
0788 &lt;li&gt;Noun&lt;/li&gt;
0789 &lt;li&gt;Pronoun&lt;/li&gt;
0790 &lt;li&gt;Adjective&lt;/li&gt;
0791 &lt;li&gt;Verb&lt;/li&gt;
0792 &lt;li&gt;Adverb&lt;/li&gt;
0793 &lt;li&gt;Preposition&lt;/li&gt;
0794 &lt;li&gt;Conjunction&lt;/li&gt;
0795 &lt;li&gt;Interjection&lt;/li&gt;
0796 &lt;/ol&gt;
0797 &lt;p&gt;&lt;strong&gt;Part of speech tagging&lt;/strong&gt; is the process of assigning a &lt;strong&gt;POS tag&lt;/strong&gt; to each token depending on its usage in the sentence. POS tags are useful for assigning a syntactic category like &lt;strong&gt;noun&lt;/strong&gt; or &lt;strong&gt;verb&lt;/strong&gt; to each word.&lt;/p&gt;
0798 &lt;p&gt;In spaCy, POS tags are available as an attribute on the &lt;code&gt;Token&lt;/code&gt; object:&lt;/p&gt;
0799 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0800 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;explain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
0801 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
0802 &lt;span class=&quot;go&quot;&gt;Gus NNP PROPN noun, proper singular&lt;/span&gt;
0803 &lt;span class=&quot;go&quot;&gt;Proto NNP PROPN noun, proper singular&lt;/span&gt;
0804 &lt;span class=&quot;go&quot;&gt;is VBZ VERB verb, 3rd person singular present&lt;/span&gt;
0805 &lt;span class=&quot;go&quot;&gt;a DT DET determiner&lt;/span&gt;
0806 &lt;span class=&quot;go&quot;&gt;Python NNP PROPN noun, proper singular&lt;/span&gt;
0807 &lt;span class=&quot;go&quot;&gt;developer NN NOUN noun, singular or mass&lt;/span&gt;
0808 &lt;span class=&quot;go&quot;&gt;currently RB ADV adverb&lt;/span&gt;
0809 &lt;span class=&quot;go&quot;&gt;working VBG VERB verb, gerund or present participle&lt;/span&gt;
0810 &lt;span class=&quot;go&quot;&gt;for IN ADP conjunction, subordinating or preposition&lt;/span&gt;
0811 &lt;span class=&quot;go&quot;&gt;a DT DET determiner&lt;/span&gt;
0812 &lt;span class=&quot;go&quot;&gt;London NNP PROPN noun, proper singular&lt;/span&gt;
0813 &lt;span class=&quot;go&quot;&gt;- HYPH PUNCT punctuation mark, hyphen&lt;/span&gt;
0814 &lt;span class=&quot;go&quot;&gt;based VBN VERB verb, past participle&lt;/span&gt;
0815 &lt;span class=&quot;go&quot;&gt;Fintech NNP PROPN noun, proper singular&lt;/span&gt;
0816 &lt;span class=&quot;go&quot;&gt;company NN NOUN noun, singular or mass&lt;/span&gt;
0817 &lt;span class=&quot;go&quot;&gt;. . PUNCT punctuation mark, sentence closer&lt;/span&gt;
0818 &lt;span class=&quot;go&quot;&gt;He PRP PRON pronoun, personal&lt;/span&gt;
0819 &lt;span class=&quot;go&quot;&gt;is VBZ VERB verb, 3rd person singular present&lt;/span&gt;
0820 &lt;span class=&quot;go&quot;&gt;interested JJ ADJ adjective&lt;/span&gt;
0821 &lt;span class=&quot;go&quot;&gt;in IN ADP conjunction, subordinating or preposition&lt;/span&gt;
0822 &lt;span class=&quot;go&quot;&gt;learning VBG VERB verb, gerund or present participle&lt;/span&gt;
0823 &lt;span class=&quot;go&quot;&gt;Natural NNP PROPN noun, proper singular&lt;/span&gt;
0824 &lt;span class=&quot;go&quot;&gt;Language NNP PROPN noun, proper singular&lt;/span&gt;
0825 &lt;span class=&quot;go&quot;&gt;Processing NNP PROPN noun, proper singular&lt;/span&gt;
0826 &lt;span class=&quot;go&quot;&gt;. . PUNCT punctuation mark, sentence closer&lt;/span&gt;
0827 &lt;/pre&gt;&lt;/div&gt;
0828 
0829 &lt;p&gt;Here, two attributes of the &lt;code&gt;Token&lt;/code&gt; class are accessed:&lt;/p&gt;
0830 &lt;ol&gt;
0831 &lt;li&gt;&lt;strong&gt;&lt;code&gt;tag_&lt;/code&gt;&lt;/strong&gt; lists the fine-grained part of speech.&lt;/li&gt;
0832 &lt;li&gt;&lt;strong&gt;&lt;code&gt;pos_&lt;/code&gt;&lt;/strong&gt; lists the coarse-grained part of speech.&lt;/li&gt;
0833 &lt;/ol&gt;
0834 &lt;p&gt;&lt;code&gt;spacy.explain&lt;/code&gt; gives descriptive details about a particular POS tag. spaCy provides a &lt;a href=&quot;https://spaCy.io/api/annotation#pos-tagging&quot;&gt;complete tag list&lt;/a&gt; along with an explanation for each tag.&lt;/p&gt;
0835 &lt;p&gt;Using POS tags, you can extract a particular category of words:&lt;/p&gt;
0836 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nouns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
0837 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;adjectives&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
0838 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0839 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;NOUN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0840 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;nouns&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0841 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;ADJ&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0842 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;adjectives&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0843 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
0844 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nouns&lt;/span&gt;
0845 &lt;span class=&quot;go&quot;&gt;[developer, company]&lt;/span&gt;
0846 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;adjectives&lt;/span&gt;
0847 &lt;span class=&quot;go&quot;&gt;[interested]&lt;/span&gt;
0848 &lt;/pre&gt;&lt;/div&gt;
0849 
0850 &lt;p&gt;You can use this to derive insights, remove the most common nouns, or see which adjectives are used for a particular noun.&lt;/p&gt;
0851 &lt;h2 id=&quot;visualization-using-displacy&quot;&gt;Visualization: Using displaCy&lt;/h2&gt;
0852 &lt;p&gt;spaCy comes with a built-in visualizer called &lt;strong&gt;displaCy&lt;/strong&gt;. You can use it to visualize a &lt;strong&gt;dependency parse&lt;/strong&gt; or &lt;strong&gt;named entities&lt;/strong&gt; in a browser or a &lt;a href=&quot;https://realpython.com/jupyter-notebook-introduction/&quot;&gt;Jupyter notebook&lt;/a&gt;.&lt;/p&gt;
0853 &lt;p&gt;You can use displaCy to find POS tags for tokens:&lt;/p&gt;
0854 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;spacy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;displacy&lt;/span&gt;
0855 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_interest_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;He is interested in learning&amp;#39;&lt;/span&gt;
0856 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; Natural Language Processing.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0857 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_interest_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_interest_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0858 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;displacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;serve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_interest_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;dep&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0859 &lt;/pre&gt;&lt;/div&gt;
0860 
0861 &lt;p&gt;The above code will spin a simple web server. You can see the visualization by opening &lt;a href=&quot;http://127.0.0.1:5000&quot;&gt;http://127.0.0.1:5000&lt;/a&gt; in your browser:&lt;/p&gt;
0862 &lt;figure class=&quot;figure mx-auto d-block&quot;&gt;&lt;a href=&quot;https://files.realpython.com/media/displacy_pos_tags.45059f2bf851.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/displacy_pos_tags.45059f2bf851.png&quot; width=&quot;2630&quot; height=&quot;600&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/displacy_pos_tags.45059f2bf851.png&amp;amp;w=657&amp;amp;sig=a49d6b5a0e5952aea59c0241f61fb09440bb326b 657w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/displacy_pos_tags.45059f2bf851.png&amp;amp;w=1315&amp;amp;sig=858218e45ae1e23a87ad42204154aeae77c9cc0c 1315w, https://files.realpython.com/media/displacy_pos_tags.45059f2bf851.png 2630w&quot; sizes=&quot;75vw&quot; alt=&quot;Displacy: Part of Speech Tagging Demo&quot;/&gt;&lt;/a&gt;&lt;figcaption class=&quot;figure-caption text-center&quot;&gt;displaCy: Part of Speech Tagging Demo&lt;/figcaption&gt;&lt;/figure&gt;
0863 
0864 &lt;p&gt;In the image above, each token is assigned a POS tag written just below the token.&lt;/p&gt;
0865 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
0866 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Here&amp;rsquo;s how you can use displaCy in a Jupyter notebook:&lt;/p&gt;
0867 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;displacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_interest_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;dep&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jupyter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0868 &lt;/pre&gt;&lt;/div&gt;
0869 
0870 &lt;/div&gt;
0871 &lt;h2 id=&quot;preprocessing-functions&quot;&gt;Preprocessing Functions&lt;/h2&gt;
0872 &lt;p&gt;You can create a &lt;strong&gt;preprocessing function&lt;/strong&gt; that takes text as input and applies the following operations:&lt;/p&gt;
0873 &lt;ul&gt;
0874 &lt;li&gt;Lowercases the text&lt;/li&gt;
0875 &lt;li&gt;Lemmatizes each token&lt;/li&gt;
0876 &lt;li&gt;Removes punctuation symbols&lt;/li&gt;
0877 &lt;li&gt;Removes stop words&lt;/li&gt;
0878 &lt;/ul&gt;
0879 &lt;p&gt;A preprocessing function converts text to an analyzable format. It&amp;rsquo;s necessary for most NLP tasks. Here&amp;rsquo;s an example:&lt;/p&gt;
0880 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;is_token_allowed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
0881 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;sd&quot;&gt;&amp;#39;&amp;#39;&amp;#39;&lt;/span&gt;
0882 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;        Only allow valid tokens which are not stop words&lt;/span&gt;
0883 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;        and punctuation symbols.&lt;/span&gt;
0884 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;    &amp;#39;&amp;#39;&amp;#39;&lt;/span&gt;
0885 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt;
0886 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_stop&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_punct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
0887 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
0888 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;
0889 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
0890 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;preprocess_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
0891 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Reduce token to its lowercase lemma form&lt;/span&gt;
0892 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lemma_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lower&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
0893 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
0894 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;complete_filtered_tokens&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;preprocess_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0895 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;complete_doc&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_token_allowed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
0896 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;complete_filtered_tokens&lt;/span&gt;
0897 &lt;span class=&quot;go&quot;&gt;[&amp;#39;gus&amp;#39;, &amp;#39;proto&amp;#39;, &amp;#39;python&amp;#39;, &amp;#39;developer&amp;#39;, &amp;#39;currently&amp;#39;, &amp;#39;work&amp;#39;,&lt;/span&gt;
0898 &lt;span class=&quot;go&quot;&gt;&amp;#39;london&amp;#39;, &amp;#39;base&amp;#39;, &amp;#39;fintech&amp;#39;, &amp;#39;company&amp;#39;, &amp;#39;interested&amp;#39;, &amp;#39;learn&amp;#39;,&lt;/span&gt;
0899 &lt;span class=&quot;go&quot;&gt;&amp;#39;natural&amp;#39;, &amp;#39;language&amp;#39;, &amp;#39;processing&amp;#39;, &amp;#39;developer&amp;#39;, &amp;#39;conference&amp;#39;,&lt;/span&gt;
0900 &lt;span class=&quot;go&quot;&gt;&amp;#39;happen&amp;#39;, &amp;#39;21&amp;#39;, &amp;#39;july&amp;#39;, &amp;#39;2019&amp;#39;, &amp;#39;london&amp;#39;, &amp;#39;title&amp;#39;,&lt;/span&gt;
0901 &lt;span class=&quot;go&quot;&gt;&amp;#39;applications&amp;#39;, &amp;#39;natural&amp;#39;, &amp;#39;language&amp;#39;, &amp;#39;processing&amp;#39;, &amp;#39;helpline&amp;#39;,&lt;/span&gt;
0902 &lt;span class=&quot;go&quot;&gt;&amp;#39;number&amp;#39;, &amp;#39;available&amp;#39;, &amp;#39;+1&amp;#39;, &amp;#39;1234567891&amp;#39;, &amp;#39;gus&amp;#39;, &amp;#39;help&amp;#39;,&lt;/span&gt;
0903 &lt;span class=&quot;go&quot;&gt;&amp;#39;organize&amp;#39;, &amp;#39;keep&amp;#39;, &amp;#39;organize&amp;#39;, &amp;#39;local&amp;#39;, &amp;#39;python&amp;#39;, &amp;#39;meetup&amp;#39;,&lt;/span&gt;
0904 &lt;span class=&quot;go&quot;&gt;&amp;#39;internal&amp;#39;, &amp;#39;talk&amp;#39;, &amp;#39;workplace&amp;#39;, &amp;#39;gus&amp;#39;, &amp;#39;present&amp;#39;, &amp;#39;talk&amp;#39;, &amp;#39;talk&amp;#39;,&lt;/span&gt;
0905 &lt;span class=&quot;go&quot;&gt;&amp;#39;introduce&amp;#39;, &amp;#39;reader&amp;#39;, &amp;#39;use&amp;#39;, &amp;#39;case&amp;#39;, &amp;#39;natural&amp;#39;, &amp;#39;language&amp;#39;,&lt;/span&gt;
0906 &lt;span class=&quot;go&quot;&gt;&amp;#39;processing&amp;#39;, &amp;#39;fintech&amp;#39;, &amp;#39;apart&amp;#39;, &amp;#39;work&amp;#39;, &amp;#39;passionate&amp;#39;, &amp;#39;music&amp;#39;,&lt;/span&gt;
0907 &lt;span class=&quot;go&quot;&gt;&amp;#39;gus&amp;#39;, &amp;#39;learn&amp;#39;, &amp;#39;play&amp;#39;, &amp;#39;piano&amp;#39;, &amp;#39;enrol&amp;#39;, &amp;#39;weekend&amp;#39;, &amp;#39;batch&amp;#39;,&lt;/span&gt;
0908 &lt;span class=&quot;go&quot;&gt;&amp;#39;great&amp;#39;, &amp;#39;piano&amp;#39;, &amp;#39;academy&amp;#39;, &amp;#39;great&amp;#39;, &amp;#39;piano&amp;#39;, &amp;#39;academy&amp;#39;,&lt;/span&gt;
0909 &lt;span class=&quot;go&quot;&gt;&amp;#39;situate&amp;#39;, &amp;#39;mayfair&amp;#39;, &amp;#39;city&amp;#39;, &amp;#39;london&amp;#39;, &amp;#39;world&amp;#39;, &amp;#39;class&amp;#39;,&lt;/span&gt;
0910 &lt;span class=&quot;go&quot;&gt;&amp;#39;piano&amp;#39;, &amp;#39;instructor&amp;#39;]&lt;/span&gt;
0911 &lt;/pre&gt;&lt;/div&gt;
0912 
0913 &lt;p&gt;Note that the &lt;code&gt;complete_filtered_tokens&lt;/code&gt; does not contain any stop word or punctuation symbols and consists of lemmatized lowercase tokens.&lt;/p&gt;
0914 &lt;h2 id=&quot;rule-based-matching-using-spacy&quot;&gt;Rule-Based Matching Using spaCy&lt;/h2&gt;
0915 &lt;p&gt;&lt;strong&gt;Rule-based matching&lt;/strong&gt; is one of the steps in extracting information from unstructured text. It&amp;rsquo;s used to identify and extract tokens and phrases according to patterns (such as lowercase) and grammatical features (such as part of speech).&lt;/p&gt;
0916 &lt;p&gt;Rule-based matching can use &lt;a href=&quot;https://en.wikipedia.org/wiki/Regular_expression&quot;&gt;regular expressions&lt;/a&gt; to extract entities (such as phone numbers) from an unstructured text. It&amp;rsquo;s different from extracting text using regular expressions only in the sense that regular expressions don&amp;rsquo;t consider the lexical and grammatical attributes of the text.&lt;/p&gt;
0917 &lt;p&gt;With rule-based matching, you can extract a first name and a last name, which are always &lt;strong&gt;proper nouns&lt;/strong&gt;:&lt;/p&gt;
0918 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;spacy.matcher&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Matcher&lt;/span&gt;
0919 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matcher&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Matcher&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0920 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;extract_full_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
0921 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;POS&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;PROPN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;POS&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;PROPN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}]&lt;/span&gt;
0922 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;matcher&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;FULL_NAME&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pattern&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0923 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;matches&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matcher&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0924 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;match_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matches&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0925 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0926 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;
0927 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
0928 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extract_full_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0929 &lt;span class=&quot;go&quot;&gt;&amp;#39;Gus Proto&amp;#39;&lt;/span&gt;
0930 &lt;/pre&gt;&lt;/div&gt;
0931 
0932 &lt;p&gt;In this example, &lt;code&gt;pattern&lt;/code&gt; is a list of objects that defines the combination of tokens to be matched. Both POS tags in it are &lt;code&gt;PROPN&lt;/code&gt; (proper noun). So, the &lt;code&gt;pattern&lt;/code&gt; consists of two objects in which the POS tags for both tokens should be &lt;code&gt;PROPN&lt;/code&gt;. This pattern is then added to &lt;code&gt;Matcher&lt;/code&gt; using &lt;code&gt;FULL_NAME&lt;/code&gt; and the the &lt;code&gt;match_id&lt;/code&gt;. Finally, matches are obtained with their starting and end indexes.&lt;/p&gt;
0933 &lt;p&gt;You can also use rule-based matching to extract phone numbers:&lt;/p&gt;
0934 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;spacy.matcher&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Matcher&lt;/span&gt;
0935 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matcher&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Matcher&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0936 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_org_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;There is a developer conference&amp;#39;&lt;/span&gt;
0937 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39;happening on 21 July 2019 in London. It is titled&amp;#39;&lt;/span&gt;
0938 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; &amp;quot;Applications of Natural Language Processing&amp;quot;.&amp;#39;&lt;/span&gt;
0939 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; There is a helpline number available&amp;#39;&lt;/span&gt;
0940 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; at (123) 456-789&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0941 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
0942 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;extract_phone_number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
0943 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;ORTH&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;(&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;SHAPE&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;ddd&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
0944 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;               &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;ORTH&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;)&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;SHAPE&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;ddd&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
0945 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;               &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;ORTH&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;-&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;OP&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;?&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
0946 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;               &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;SHAPE&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;ddd&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}]&lt;/span&gt;
0947 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;matcher&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;PHONE_NUMBER&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pattern&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0948 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;matches&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matcher&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0949 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;match_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matches&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0950 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
0951 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;
0952 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
0953 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_org_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_org_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0954 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extract_phone_number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_org_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0955 &lt;span class=&quot;go&quot;&gt;&amp;#39;(123) 456-789&amp;#39;&lt;/span&gt;
0956 &lt;/pre&gt;&lt;/div&gt;
0957 
0958 &lt;p&gt;In this example, only the pattern is updated in order to match phone numbers from the previous example. Here, some attributes of the token are also used:&lt;/p&gt;
0959 &lt;ul&gt;
0960 &lt;li&gt;&lt;strong&gt;&lt;code&gt;ORTH&lt;/code&gt;&lt;/strong&gt; gives the exact text of the token.&lt;/li&gt;
0961 &lt;li&gt;&lt;strong&gt;&lt;code&gt;SHAPE&lt;/code&gt;&lt;/strong&gt; transforms the token string to show orthographic features.&lt;/li&gt;
0962 &lt;li&gt;&lt;strong&gt;&lt;code&gt;OP&lt;/code&gt;&lt;/strong&gt; defines operators. Using &lt;code&gt;?&lt;/code&gt; as a value means that the pattern is optional, meaning it can match 0 or 1 times.&lt;/li&gt;
0963 &lt;/ul&gt;
0964 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
0965 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; For simplicity, phone numbers are assumed to be of a particular format: &lt;code&gt;(123) 456-789&lt;/code&gt;. You can change this depending on your use case.&lt;/p&gt;
0966 &lt;/div&gt;
0967 &lt;p&gt;Rule-based matching helps you identify and extract tokens and phrases according to lexical patterns (such as lowercase) and grammatical features(such as part of speech).&lt;/p&gt;
0968 &lt;h2 id=&quot;dependency-parsing-using-spacy&quot;&gt;Dependency Parsing Using spaCy&lt;/h2&gt;
0969 &lt;p&gt;&lt;strong&gt;Dependency parsing&lt;/strong&gt; is the process of extracting the dependency parse of a sentence to represent its grammatical structure. It defines the dependency relationship between &lt;strong&gt;headwords&lt;/strong&gt; and their &lt;strong&gt;dependents&lt;/strong&gt;. The head of a sentence has no dependency and is called the &lt;strong&gt;root of the sentence&lt;/strong&gt;. The &lt;strong&gt;verb&lt;/strong&gt; is usually the head of the sentence. All other words are linked to the headword.&lt;/p&gt;
0970 &lt;p&gt;The dependencies can be mapped in a directed graph representation: &lt;/p&gt;
0971 &lt;ul&gt;
0972 &lt;li&gt;Words are the nodes.&lt;/li&gt;
0973 &lt;li&gt;The grammatical relationships are the edges.&lt;/li&gt;
0974 &lt;/ul&gt;
0975 &lt;p&gt;Dependency parsing helps you know what role a word plays in the text and how different words relate to each other. It&amp;rsquo;s also used in &lt;strong&gt;shallow parsing&lt;/strong&gt; and named entity recognition.&lt;/p&gt;
0976 &lt;p&gt;Here&amp;rsquo;s how you can use dependency parsing to see the relationships between words:&lt;/p&gt;
0977 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;piano_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Gus is learning piano&amp;#39;&lt;/span&gt;
0978 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;piano_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;piano_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0979 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;piano_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
0980 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dep_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0981 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
0982 &lt;span class=&quot;go&quot;&gt;Gus NNP learning nsubj&lt;/span&gt;
0983 &lt;span class=&quot;go&quot;&gt;is VBZ learning aux&lt;/span&gt;
0984 &lt;span class=&quot;go&quot;&gt;learning VBG learning ROOT&lt;/span&gt;
0985 &lt;span class=&quot;go&quot;&gt;piano NN learning dobj&lt;/span&gt;
0986 &lt;/pre&gt;&lt;/div&gt;
0987 
0988 &lt;p&gt;In this example, the sentence contains three relationships:&lt;/p&gt;
0989 &lt;ol&gt;
0990 &lt;li&gt;&lt;strong&gt;&lt;code&gt;nsubj&lt;/code&gt;&lt;/strong&gt; is the subject of the word. Its headword is a verb.&lt;/li&gt;
0991 &lt;li&gt;&lt;strong&gt;&lt;code&gt;aux&lt;/code&gt;&lt;/strong&gt; is an auxiliary word. Its headword is a verb.&lt;/li&gt;
0992 &lt;li&gt;&lt;strong&gt;&lt;code&gt;dobj&lt;/code&gt;&lt;/strong&gt; is the direct object of the verb. Its headword is a verb.&lt;/li&gt;
0993 &lt;/ol&gt;
0994 &lt;p&gt;There is a detailed &lt;a href=&quot;https://nlp.stanford.edu/software/dependencies_manual.pdf&quot;&gt;list of relationships&lt;/a&gt; with descriptions. You can use displaCy to visualize the dependency tree:&lt;/p&gt;
0995 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;displacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;serve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;piano_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;dep&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
0996 &lt;/pre&gt;&lt;/div&gt;
0997 
0998 &lt;p&gt;This code will produce a visualization that can be accessed by opening &lt;a href=&quot;http://127.0.0.1:5000&quot;&gt;http://127.0.0.1:5000&lt;/a&gt; in your browser:&lt;/p&gt;
0999 &lt;figure class=&quot;figure mx-auto d-block&quot;&gt;&lt;a href=&quot;https://files.realpython.com/media/displacy_dependency_parse.de72f9b1d115.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/displacy_dependency_parse.de72f9b1d115.png&quot; width=&quot;1278&quot; height=&quot;596&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/displacy_dependency_parse.de72f9b1d115.png&amp;amp;w=319&amp;amp;sig=111728c07cf2e1f64b8419cfce8a5f880c244d03 319w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/displacy_dependency_parse.de72f9b1d115.png&amp;amp;w=639&amp;amp;sig=f90a72529d7bc2d2dd944af3c02bbf487b65aaf3 639w, https://files.realpython.com/media/displacy_dependency_parse.de72f9b1d115.png 1278w&quot; sizes=&quot;75vw&quot; alt=&quot;Displacy: Dependency Parse Demo&quot;/&gt;&lt;/a&gt;&lt;figcaption class=&quot;figure-caption text-center&quot;&gt;displaCy: Dependency Parse Demo&lt;/figcaption&gt;&lt;/figure&gt;
1000 
1001 &lt;p&gt;This image shows you that the subject of the sentence is the proper noun &lt;code&gt;Gus&lt;/code&gt; and that it has a &lt;code&gt;learn&lt;/code&gt; relationship with &lt;code&gt;piano&lt;/code&gt;.&lt;/p&gt;
1002 &lt;h2 id=&quot;navigating-the-tree-and-subtree&quot;&gt;Navigating the Tree and Subtree&lt;/h2&gt;
1003 &lt;p&gt;The dependency parse tree has all the properties of a &lt;a href=&quot;https://en.wikipedia.org/wiki/Tree_(data_structure)&quot;&gt;tree&lt;/a&gt;. This tree contains information about sentence structure and grammar and can be traversed in different ways to extract relationships.&lt;/p&gt;
1004 &lt;p&gt;spaCy provides attributes like &lt;code&gt;children&lt;/code&gt;, &lt;code&gt;lefts&lt;/code&gt;, &lt;code&gt;rights&lt;/code&gt;, and &lt;code&gt;subtree&lt;/code&gt; to navigate the parse tree:&lt;/p&gt;
1005 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;one_line_about_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Gus Proto is a Python developer&amp;#39;&lt;/span&gt;
1006 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; currently working for a London-based Fintech company&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1007 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;one_line_about_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;one_line_about_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1008 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Extract children of `developer`&lt;/span&gt;
1009 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;one_line_about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;children&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
1010 &lt;span class=&quot;go&quot;&gt;[&amp;#39;a&amp;#39;, &amp;#39;Python&amp;#39;, &amp;#39;working&amp;#39;]&lt;/span&gt;
1011 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Extract previous neighboring node of `developer`&lt;/span&gt;
1012 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;one_line_about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nbor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
1013 &lt;span class=&quot;go&quot;&gt;Python&lt;/span&gt;
1014 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Extract next neighboring node of `developer`&lt;/span&gt;
1015 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;one_line_about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nbor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
1016 &lt;span class=&quot;go&quot;&gt;currently&lt;/span&gt;
1017 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Extract all tokens on the left of `developer`&lt;/span&gt;
1018 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;one_line_about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lefts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
1019 &lt;span class=&quot;go&quot;&gt;[&amp;#39;a&amp;#39;, &amp;#39;Python&amp;#39;]&lt;/span&gt;
1020 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Extract tokens on the right of `developer`&lt;/span&gt;
1021 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;one_line_about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rights&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
1022 &lt;span class=&quot;go&quot;&gt;[&amp;#39;working&amp;#39;]&lt;/span&gt;
1023 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Print subtree of `developer`&lt;/span&gt;
1024 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;one_line_about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subtree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
1025 &lt;span class=&quot;go&quot;&gt;[a, Python, developer, currently, working, for, a, London, -,&lt;/span&gt;
1026 &lt;span class=&quot;go&quot;&gt;based, Fintech, company]&lt;/span&gt;
1027 &lt;/pre&gt;&lt;/div&gt;
1028 
1029 &lt;p&gt;You can construct a function that takes a subtree as an argument and returns a string by merging words in it:&lt;/p&gt;
1030 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;flatten_tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1031 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text_with_ws&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
1032 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
1033 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Print flattened subtree of `developer`&lt;/span&gt;
1034 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flatten_tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;one_line_about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subtree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
1035 &lt;span class=&quot;go&quot;&gt;a Python developer currently working for a London-based Fintech company&lt;/span&gt;
1036 &lt;/pre&gt;&lt;/div&gt;
1037 
1038 &lt;p&gt;You can use this function to print all the tokens in a subtree.&lt;/p&gt;
1039 &lt;h2 id=&quot;shallow-parsing&quot;&gt;Shallow Parsing&lt;/h2&gt;
1040 &lt;p&gt;&lt;strong&gt;Shallow parsing&lt;/strong&gt;, or &lt;strong&gt;chunking&lt;/strong&gt;, is the process of extracting phrases from unstructured text. Chunking groups adjacent tokens into phrases on the basis of their POS tags. There are some standard well-known chunks such as noun phrases, verb phrases, and prepositional phrases.&lt;/p&gt;
1041 &lt;h3 id=&quot;noun-phrase-detection&quot;&gt;Noun Phrase Detection&lt;/h3&gt;
1042 &lt;p&gt;A noun phrase is a phrase that has a noun as its head. It could also include other kinds of words, such as adjectives, ordinals, determiners. Noun phrases are useful for explaining the context of the sentence. They help you infer &lt;em&gt;what&lt;/em&gt; is being talked about in the sentence.&lt;/p&gt;
1043 &lt;p&gt;spaCy has the property &lt;code&gt;noun_chunks&lt;/code&gt; on &lt;code&gt;Doc&lt;/code&gt; object. You can use it to extract noun phrases:&lt;/p&gt;
1044 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;There is a developer conference&amp;#39;&lt;/span&gt;
1045 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; happening on 21 July 2019 in London.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1046 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1047 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Extract Noun Phrases&lt;/span&gt;
1048 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conference_doc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;noun_chunks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
1049 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1050 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
1051 &lt;span class=&quot;go&quot;&gt;a developer conference&lt;/span&gt;
1052 &lt;span class=&quot;go&quot;&gt;21 July&lt;/span&gt;
1053 &lt;span class=&quot;go&quot;&gt;London&lt;/span&gt;
1054 &lt;/pre&gt;&lt;/div&gt;
1055 
1056 &lt;p&gt;By looking at noun phrases, you can get information about your text. For example, &lt;code&gt;a developer conference&lt;/code&gt; indicates that the text mentions a conference, while the date &lt;code&gt;21 July&lt;/code&gt; lets you know that conference is scheduled for &lt;code&gt;21 July&lt;/code&gt;. You can figure out whether the conference is in the past or the future. &lt;code&gt;London&lt;/code&gt; tells you that the conference is in &lt;code&gt;London&lt;/code&gt;.&lt;/p&gt;
1057 &lt;h3 id=&quot;verb-phrase-detection&quot;&gt;Verb Phrase Detection&lt;/h3&gt;
1058 &lt;p&gt;A &lt;strong&gt;verb phrase&lt;/strong&gt; is a syntactic unit composed of at least one verb. This verb can be followed by other chunks, such as noun phrases. Verb phrases are useful for understanding the actions that nouns are involved in. &lt;/p&gt;
1059 &lt;p&gt;spaCy has no built-in functionality to extract verb phrases, so you&amp;rsquo;ll need a library called &lt;a href=&quot;https://chartbeat-labs.github.io/textacy/&quot;&gt;&lt;code&gt;textacy&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
1060 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
1061 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;/p&gt;
1062 &lt;p&gt;You can use &lt;code&gt;pip&lt;/code&gt; to install &lt;code&gt;textacy&lt;/code&gt;:&lt;/p&gt;
1063 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pip install textacy
1064 &lt;/pre&gt;&lt;/div&gt;
1065 
1066 &lt;/div&gt;
1067 &lt;p&gt;Now that you have &lt;code&gt;textacy&lt;/code&gt; installed, you can use it to extract verb phrases based on grammar rules:&lt;/p&gt;
1068 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;textacy&lt;/span&gt;
1069 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_talk_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;The talk will introduce reader about Use&amp;#39;&lt;/span&gt;
1070 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                   &lt;span class=&quot;s1&quot;&gt;&amp;#39; cases of Natural Language Processing in&amp;#39;&lt;/span&gt;
1071 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                   &lt;span class=&quot;s1&quot;&gt;&amp;#39; Fintech&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1072 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;(&amp;lt;VERB&amp;gt;?&amp;lt;ADV&amp;gt;*&amp;lt;VERB&amp;gt;+)&amp;#39;&lt;/span&gt;
1073 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_talk_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;textacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make_spacy_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_talk_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1074 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                        &lt;span class=&quot;n&quot;&gt;lang&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;en_core_web_sm&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1075 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;verb_phrases&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;textacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extract&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos_regex_matches&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_talk_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pattern&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1076 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Print all Verb Phrase&lt;/span&gt;
1077 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;verb_phrases&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
1078 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1079 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
1080 &lt;span class=&quot;go&quot;&gt;will introduce&lt;/span&gt;
1081 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Extract Noun Phrase to explain what nouns are involved&lt;/span&gt;
1082 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;about_talk_doc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;noun_chunks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
1083 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1084 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
1085 &lt;span class=&quot;go&quot;&gt;The talk&lt;/span&gt;
1086 &lt;span class=&quot;go&quot;&gt;reader&lt;/span&gt;
1087 &lt;span class=&quot;go&quot;&gt;Use cases&lt;/span&gt;
1088 &lt;span class=&quot;go&quot;&gt;Natural Language Processing&lt;/span&gt;
1089 &lt;span class=&quot;go&quot;&gt;Fintech&lt;/span&gt;
1090 &lt;/pre&gt;&lt;/div&gt;
1091 
1092 &lt;p&gt;In this example, the verb phrase &lt;code&gt;introduce&lt;/code&gt; indicates that something will be introduced. By looking at noun phrases, you can see that there is a &lt;code&gt;talk&lt;/code&gt; that will &lt;code&gt;introduce&lt;/code&gt; the &lt;code&gt;reader&lt;/code&gt; to &lt;code&gt;use cases&lt;/code&gt; of &lt;code&gt;Natural Language Processing&lt;/code&gt; or &lt;code&gt;Fintech&lt;/code&gt;.&lt;/p&gt;
1093 &lt;p&gt;The above code extracts all the verb phrases &lt;a href=&quot;https://chartbeat-labs.github.io/textacy/api_reference/information_extraction.html?highlight=pos#textacy.extract.pos_regex_matches&quot;&gt;using a regular expression pattern&lt;/a&gt; of POS tags. You can tweak the pattern for verb phrases depending upon your use case.&lt;/p&gt;
1094 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
1095 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In the previous example, you could have also done dependency parsing to see what the &lt;a href=&quot;https://nlp.stanford.edu/software/dependencies_manual.pdf&quot;&gt;relationships&lt;/a&gt; between the words were.&lt;/p&gt;
1096 &lt;/div&gt;
1097 &lt;h2 id=&quot;named-entity-recognition&quot;&gt;Named Entity Recognition&lt;/h2&gt;
1098 &lt;p&gt;&lt;strong&gt;Named Entity Recognition&lt;/strong&gt; (NER) is the process of locating &lt;strong&gt;named entities&lt;/strong&gt; in unstructured text and then classifying them into pre-defined categories, such as person names, organizations, locations, monetary values, percentages, time expressions, and so on.&lt;/p&gt;
1099 &lt;p&gt;You can use &lt;strong&gt;NER&lt;/strong&gt; to know more about the meaning of your text. For example, you could use it to populate tags for a set of documents in order to improve the keyword search. You could also use it to categorize customer support tickets into relevant categories.&lt;/p&gt;
1100 &lt;p&gt;spaCy has the property &lt;code&gt;ents&lt;/code&gt; on &lt;code&gt;Doc&lt;/code&gt; objects. You can use it to extract named entities:&lt;/p&gt;
1101 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;piano_class_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Great Piano Academy is situated&amp;#39;&lt;/span&gt;
1102 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; in Mayfair or the City of London and has&amp;#39;&lt;/span&gt;
1103 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; world-class piano instructors.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1104 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;piano_class_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;piano_class_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1105 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ent&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;piano_class_doc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
1106 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start_char&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end_char&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1107 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;          &lt;span class=&quot;n&quot;&gt;ent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;explain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
1108 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
1109 &lt;span class=&quot;go&quot;&gt;Great Piano Academy 0 19 ORG Companies, agencies, institutions, etc.&lt;/span&gt;
1110 &lt;span class=&quot;go&quot;&gt;Mayfair 35 42 GPE Countries, cities, states&lt;/span&gt;
1111 &lt;span class=&quot;go&quot;&gt;the City of London 46 64 GPE Countries, cities, states&lt;/span&gt;
1112 &lt;/pre&gt;&lt;/div&gt;
1113 
1114 &lt;p&gt;In the above example, &lt;code&gt;ent&lt;/code&gt; is a &lt;a href=&quot;https://spacy.io/api/span&quot;&gt;&lt;code&gt;Span&lt;/code&gt;&lt;/a&gt; object with various attributes:&lt;/p&gt;
1115 &lt;ul&gt;
1116 &lt;li&gt;&lt;strong&gt;&lt;code&gt;text&lt;/code&gt;&lt;/strong&gt; gives the Unicode text representation of the entity.&lt;/li&gt;
1117 &lt;li&gt;&lt;strong&gt;&lt;code&gt;start_char&lt;/code&gt;&lt;/strong&gt; denotes the character offset for the start of the entity.&lt;/li&gt;
1118 &lt;li&gt;&lt;strong&gt;&lt;code&gt;end_char&lt;/code&gt;&lt;/strong&gt; denotes the character offset for the end of the entity.&lt;/li&gt;
1119 &lt;li&gt;&lt;strong&gt;&lt;code&gt;label_&lt;/code&gt;&lt;/strong&gt; gives the label of the entity.&lt;/li&gt;
1120 &lt;/ul&gt;
1121 &lt;p&gt;&lt;code&gt;spacy.explain&lt;/code&gt; gives descriptive details about an entity label. The spaCy model has a pre-trained &lt;a href=&quot;https://spaCy.io/api/annotation#named-entities&quot;&gt;list of entity classes&lt;/a&gt;. You can use displaCy to visualize these entities:&lt;/p&gt;
1122 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;displacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;serve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;piano_class_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;ent&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1123 &lt;/pre&gt;&lt;/div&gt;
1124 
1125 &lt;p&gt;If you open &lt;a href=&quot;http://127.0.0.1:5000&quot;&gt;http://127.0.0.1:5000&lt;/a&gt; in your browser, then you can see the visualization:&lt;/p&gt;
1126 &lt;figure class=&quot;figure mx-auto d-block&quot;&gt;&lt;a href=&quot;https://files.realpython.com/media/displacy_ner.1fba6869638f.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/displacy_ner.1fba6869638f.png&quot; width=&quot;1930&quot; height=&quot;140&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/displacy_ner.1fba6869638f.png&amp;amp;w=482&amp;amp;sig=18b93b0aed61930a6eedd37dbd12fbbce22733d4 482w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/displacy_ner.1fba6869638f.png&amp;amp;w=965&amp;amp;sig=f6b3cfb460053397a23a0eb49ebc22cf05dd15ab 965w, https://files.realpython.com/media/displacy_ner.1fba6869638f.png 1930w&quot; sizes=&quot;75vw&quot; alt=&quot;Displacy: Named Entity Recognition Demo&quot;/&gt;&lt;/a&gt;&lt;figcaption class=&quot;figure-caption text-center&quot;&gt;displaCy: Named Entity Recognition Demo&lt;/figcaption&gt;&lt;/figure&gt;
1127 
1128 &lt;p&gt;You can use NER to redact people&amp;rsquo;s names from a text. For example, you might want to do this in order to hide personal information collected in a survey. You can use spaCy to do that:&lt;/p&gt;
1129 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;survey_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Out of 5 people surveyed, James Robert,&amp;#39;&lt;/span&gt;
1130 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;               &lt;span class=&quot;s1&quot;&gt;&amp;#39; Julie Fuller and Benjamin Brooks like&amp;#39;&lt;/span&gt;
1131 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;               &lt;span class=&quot;s1&quot;&gt;&amp;#39; apples. Kelly Cox and Matthew Evans&amp;#39;&lt;/span&gt;
1132 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;               &lt;span class=&quot;s1&quot;&gt;&amp;#39; like oranges.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1133 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
1134 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;replace_person_names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1135 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ent_iob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ent_type_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;PERSON&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
1136 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;[REDACTED] &amp;#39;&lt;/span&gt;
1137 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;
1138 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
1139 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;redact_names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1140 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ent&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp_doc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
1141 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;ent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;merge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
1142 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;replace_person_names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1143 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1144 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
1145 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;survey_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;survey_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1146 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;redact_names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;survey_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1147 &lt;span class=&quot;go&quot;&gt;&amp;#39;Out of 5 people surveyed, [REDACTED] , [REDACTED] and&amp;#39;&lt;/span&gt;
1148 &lt;span class=&quot;go&quot;&gt;&amp;#39; [REDACTED] like apples. [REDACTED] and [REDACTED]&amp;#39;&lt;/span&gt;
1149 &lt;span class=&quot;go&quot;&gt;&amp;#39; like oranges.&amp;#39;&lt;/span&gt;
1150 &lt;/pre&gt;&lt;/div&gt;
1151 
1152 &lt;p&gt;In this example, &lt;code&gt;replace_person_names()&lt;/code&gt; uses &lt;code&gt;ent_iob&lt;/code&gt;. It gives the IOB code of the named entity tag using &lt;a href=&quot;https://en.wikipedia.org/wiki/Inside%E2%80%93outside%E2%80%93beginning_(tagging)&quot;&gt;inside-outside-beginning (IOB) tagging&lt;/a&gt;. Here, it can assume a value other than zero, because zero means that no entity tag is set.&lt;/p&gt;
1153 &lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
1154 &lt;p&gt;spaCy is a powerful and advanced library that is gaining huge popularity for NLP applications due to its speed, ease of use, accuracy, and extensibility. Congratulations! You now know:&lt;/p&gt;
1155 &lt;ul&gt;
1156 &lt;li&gt;What the foundational terms and concepts in NLP are&lt;/li&gt;
1157 &lt;li&gt;How to implement those concepts in spaCy&lt;/li&gt;
1158 &lt;li&gt;How to customize and extend built-in functionalities in spaCy&lt;/li&gt;
1159 &lt;li&gt;How to perform basic statistical analysis on a text&lt;/li&gt;
1160 &lt;li&gt;How to create a pipeline to process unstructured text&lt;/li&gt;
1161 &lt;li&gt;How to parse a sentence and extract meaningful insights from it&lt;/li&gt;
1162 &lt;/ul&gt;
1163         &lt;hr /&gt;
1164         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
1165       </content>
1166     </entry>
1167   
1168     <entry>
1169       <title>PyCharm for Productive Python Development (Guide)</title>
1170       <id>https://realpython.com/pycharm-guide/</id>
1171       <link href="https://realpython.com/pycharm-guide/"/>
1172       <updated>2019-08-28T14:00:00+00:00</updated>
1173       <summary>In this step-by-step tutorial, you&#39;ll learn how you can use PyCharm to be a more productive Python developer. PyCharm makes debugging and visualization easy so you can focus on business logic and just get the job done.</summary>
1174       <content type="html">
1175         &lt;p&gt;As a programmer, you should be focused on the business logic and creating useful applications for your users. In doing that, &lt;a href=&quot;https://www.jetbrains.com/pycharm/&quot;&gt;PyCharm&lt;/a&gt; by &lt;a href=&quot;https://www.jetbrains.com/&quot;&gt;JetBrains&lt;/a&gt; saves you a lot of time by taking care of the routine and by making a number of other tasks such as debugging and visualization easy.   &lt;/p&gt;
1176 &lt;p&gt;&lt;strong&gt;In this article, you&amp;rsquo;ll learn about:&lt;/strong&gt;&lt;/p&gt;
1177 &lt;ul&gt;
1178 &lt;li&gt;Installing PyCharm&lt;/li&gt;
1179 &lt;li&gt;Writing code in PyCharm&lt;/li&gt;
1180 &lt;li&gt;Running your code in PyCharm&lt;/li&gt;
1181 &lt;li&gt;Debugging and testing your code in PyCharm&lt;/li&gt;
1182 &lt;li&gt;Editing an existing project in PyCharm&lt;/li&gt;
1183 &lt;li&gt;Searching and navigating in PyCharm&lt;/li&gt;
1184 &lt;li&gt;Using Version Control in PyCharm&lt;/li&gt;
1185 &lt;li&gt;Using Plugins and External Tools in PyCharm&lt;/li&gt;
1186 &lt;li&gt;Using PyCharm Professional features, such as Django support and Scientific mode&lt;/li&gt;
1187 &lt;/ul&gt;
1188 &lt;p&gt;This article assumes that you&amp;rsquo;re familiar with Python development and already have some form of Python installed on your system. Python 3.6 will be used for this tutorial. Screenshots and demos provided are for macOS. Because PyCharm runs on all major platforms, you may see slightly different UI elements and may need to modify certain commands.&lt;/p&gt;
1189 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
1190 &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: &lt;/p&gt;
1191 &lt;p&gt;PyCharm comes in three editions: &lt;/p&gt;
1192 &lt;ol&gt;
1193 &lt;li&gt;&lt;a href=&quot;https://www.jetbrains.com/pycharm-edu/&quot;&gt;PyCharm Edu&lt;/a&gt; is free and for educational purposes.  &lt;/li&gt;
1194 &lt;li&gt;&lt;a href=&quot;https://www.jetbrains.com/pycharm&quot;&gt;PyCharm Community&lt;/a&gt; is free as well and intended for pure Python development. &lt;/li&gt;
1195 &lt;li&gt;&lt;a href=&quot;https://www.jetbrains.com/pycharm&quot;&gt;PyCharm Professional&lt;/a&gt; is paid, has everything the Community edition has and also is very well suited for Web and Scientific development with support for such frameworks as Django and Flask, Database and SQL, and scientific tools such as Jupyter.&lt;/li&gt;
1196 &lt;/ol&gt;
1197 &lt;p&gt;For more details on their differences, check out the &lt;a href=&quot;https://www.jetbrains.com/pycharm/features/editions_comparison_matrix.html&quot;&gt;PyCharm Editions Comparison Matrix&lt;/a&gt; by JetBrains. The company also has &lt;a href=&quot;https://www.jetbrains.com/pycharm/buy/#edition=discounts&quot;&gt;special offers&lt;/a&gt; for students, teachers, open source projects, and other cases.&lt;/p&gt;
1198 &lt;/div&gt;
1199 &lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Clone Repo:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/alcazar-web-framework/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-alcazar-web-framework&quot; data-focus=&quot;false&quot;&gt;Click here to clone the repo you&#39;ll use&lt;/a&gt; to explore the project-focused features of PyCharm in this tutorial.&lt;/p&gt;&lt;/div&gt;
1200 
1201 &lt;h2 id=&quot;installing-pycharm&quot;&gt;Installing PyCharm&lt;/h2&gt;
1202 &lt;p&gt;This article will use PyCharm Community Edition 2019.1 as it&amp;rsquo;s free and available on every major platform. Only the section about the professional features will use PyCharm Professional Edition 2019.1.  &lt;/p&gt;
1203 &lt;p&gt;The recommended way of installing PyCharm is with the &lt;a href=&quot;https://www.jetbrains.com/toolbox/app/&quot;&gt;JetBrains Toolbox App&lt;/a&gt;. With its help, you&amp;rsquo;ll be able to install different JetBrains products or several versions of the same product, update, rollback, and easily remove any tool when necessary. You&amp;rsquo;ll also be able to quickly open any project in the right IDE and version.&lt;/p&gt;
1204 &lt;p&gt;To install the Toolbox App, refer to the &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/installation-guide.html#toolbox&quot;&gt;documentation&lt;/a&gt; by JetBrains. It will automatically give you the right instructions depending on your OS. In case it didn&amp;rsquo;t recognize your OS correctly, you can always find it from the drop down list on the top right section: &lt;/p&gt;
1205 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-jetbrains-os-list.231740335aaa.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-jetbrains-os-list.231740335aaa.png&quot; width=&quot;1010&quot; height=&quot;679&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-jetbrains-os-list.231740335aaa.png&amp;amp;w=252&amp;amp;sig=e331b2eb15a3c8b9396327dedc700bd2bcbbc9e3 252w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-jetbrains-os-list.231740335aaa.png&amp;amp;w=505&amp;amp;sig=4a0a0527b968050fb042a0565f5d6970d72ee1f9 505w, https://files.realpython.com/media/pycharm-jetbrains-os-list.231740335aaa.png 1010w&quot; sizes=&quot;75vw&quot; alt=&quot;List of OSes in the JetBrains website&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1206 &lt;p&gt;After installing, launch the app and accept the user agreement. Under the &lt;em&gt;Tools&lt;/em&gt; tab, you&amp;rsquo;ll see a list of available products. Find PyCharm Community there and click &lt;em&gt;Install&lt;/em&gt;:&lt;/p&gt;
1207 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-toolbox-installed-pycharm.cdcf1b52bc02.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border w-33&quot; src=&quot;https://files.realpython.com/media/pycharm-toolbox-installed-pycharm.cdcf1b52bc02.png&quot; width=&quot;337&quot; height=&quot;537&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-toolbox-installed-pycharm.cdcf1b52bc02.png&amp;amp;w=84&amp;amp;sig=5f1e571c6c7bed958efddaec87d6ac5168713217 84w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-toolbox-installed-pycharm.cdcf1b52bc02.png&amp;amp;w=168&amp;amp;sig=0a76111939657ae01eaa8203b24c0c2e4fff5ee6 168w, https://files.realpython.com/media/pycharm-toolbox-installed-pycharm.cdcf1b52bc02.png 337w&quot; sizes=&quot;75vw&quot; alt=&quot;PyCharm installed with the Toolbox app&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1208 &lt;p&gt;Voilร ! You have PyCharm available on your machine. If you don&amp;rsquo;t want to use the Toolbox app, then you can also do a &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/installation-guide.html#standalone&quot;&gt;stand-alone installation of PyCharm&lt;/a&gt;.&lt;/p&gt;
1209 &lt;p&gt;Launch PyCharm, and you&amp;rsquo;ll see the import settings popup:&lt;/p&gt;
1210 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-import-settings-popup.4e360260c697.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-50&quot; src=&quot;https://files.realpython.com/media/pycharm-import-settings-popup.4e360260c697.png&quot; width=&quot;416&quot; height=&quot;156&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-import-settings-popup.4e360260c697.png&amp;amp;w=104&amp;amp;sig=4920753cc035f162c505253937453e1aa7cc4d26 104w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-import-settings-popup.4e360260c697.png&amp;amp;w=208&amp;amp;sig=b65b1226bcc172a811d7cd1e00cd600408fba092 208w, https://files.realpython.com/media/pycharm-import-settings-popup.4e360260c697.png 416w&quot; sizes=&quot;75vw&quot; alt=&quot;PyCharm Import Settings Popup&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1211 &lt;p&gt;PyCharm will automatically detect that this is a fresh install and choose &lt;em&gt;Do not import settings&lt;/em&gt; for you. Click &lt;em&gt;OK&lt;/em&gt;, and PyCharm will ask you to select a keymap scheme. Leave the default and click &lt;em&gt;Next: UI Themes&lt;/em&gt; on the bottom right:&lt;/p&gt;
1212 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-keymap-scheme.c8115fda9bdd.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/pycharm-keymap-scheme.c8115fda9bdd.png&quot; width=&quot;805&quot; height=&quot;666&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-keymap-scheme.c8115fda9bdd.png&amp;amp;w=201&amp;amp;sig=644595a94c07780a552f76abbfc5fe526b3c9459 201w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-keymap-scheme.c8115fda9bdd.png&amp;amp;w=402&amp;amp;sig=64c348872c1519bc4148e3dbbac2550ed6c0fa30 402w, https://files.realpython.com/media/pycharm-keymap-scheme.c8115fda9bdd.png 805w&quot; sizes=&quot;75vw&quot; alt=&quot;PyCharm Keymap Scheme&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1213 &lt;p&gt;PyCharm will then ask you to choose a dark theme called Darcula or a light theme. Choose whichever you prefer and click &lt;em&gt;Next: Launcher Script&lt;/em&gt;:  &lt;/p&gt;
1214 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-set-ui-theme.c48aac8e3fe0.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-set-ui-theme.c48aac8e3fe0.png&quot; width=&quot;803&quot; height=&quot;666&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-set-ui-theme.c48aac8e3fe0.png&amp;amp;w=200&amp;amp;sig=6998b85afd9e2ca1503624ba55b904f4051f1ffe 200w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-set-ui-theme.c48aac8e3fe0.png&amp;amp;w=401&amp;amp;sig=a6480f0b0073f0680068828fc2f0f5c8ee55cbdb 401w, https://files.realpython.com/media/pycharm-set-ui-theme.c48aac8e3fe0.png 803w&quot; sizes=&quot;75vw&quot; alt=&quot;PyCharm Set UI Theme Page&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1215 &lt;p&gt;I&amp;rsquo;ll be using the dark theme Darcula throughout this tutorial. You can find and install other themes as &lt;a href=&quot;#using-plugins-and-external-tools-in-pycharm&quot;&gt;plugins&lt;/a&gt;, or you can also &lt;a href=&quot;https://blog.codota.com/5-best-intellij-themes/&quot;&gt;import them&lt;/a&gt;.&lt;/p&gt;
1216 &lt;p&gt;On the next page, leave the defaults and click &lt;em&gt;Next: Featured plugins&lt;/em&gt;. There, PyCharm will show you a list of plugins you may want to install because most users like to use them. Click &lt;em&gt;Start using PyCharm&lt;/em&gt;, and now you are ready to write some code!&lt;/p&gt;
1217 &lt;h2 id=&quot;writing-code-in-pycharm&quot;&gt;Writing Code in PyCharm&lt;/h2&gt;
1218 &lt;p&gt;In PyCharm, you do everything in the context of a &lt;strong&gt;project&lt;/strong&gt;. Thus, the first thing you need to do is create one.&lt;/p&gt;
1219 &lt;p&gt;After installing and opening PyCharm, you are on the welcome screen. Click &lt;em&gt;Create New Project&lt;/em&gt;, and you&amp;rsquo;ll see the &lt;em&gt;New Project&lt;/em&gt; popup:&lt;/p&gt;
1220 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-new-project.cc35f3aa1056.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-new-project.cc35f3aa1056.png&quot; width=&quot;664&quot; height=&quot;480&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-new-project.cc35f3aa1056.png&amp;amp;w=166&amp;amp;sig=6423b68127eae8ca93165323df4884844265f5e3 166w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-new-project.cc35f3aa1056.png&amp;amp;w=332&amp;amp;sig=50d660be9a42904b1161bd76df1ab9ddd77e2132 332w, https://files.realpython.com/media/pycharm-new-project.cc35f3aa1056.png 664w&quot; sizes=&quot;75vw&quot; alt=&quot;New Project in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1221 &lt;p&gt;Specify the project location and expand the &lt;em&gt;Project Interpreter&lt;/em&gt; drop down. Here, you have options to create a new project interpreter or reuse an existing one. Choose &lt;em&gt;New environment using&lt;/em&gt;. Right next to it, you have a drop down list to select one of &lt;em&gt;Virtualenv&lt;/em&gt;, &lt;em&gt;Pipenv&lt;/em&gt;, or &lt;em&gt;Conda&lt;/em&gt;, which are the tools that help to keep dependencies required by different projects separate by creating isolated Python environments for them. &lt;/p&gt;
1222 &lt;p&gt;You are free to select whichever you like, but &lt;em&gt;Virtualenv&lt;/em&gt; is used for this tutorial. If you choose to, you can specify the environment location and choose the base interpreter from the list, which is a list of Python interpreters (such as Python2.7 and Python3.6) installed on your system. Usually, the defaults are fine. Then you have to select boxes to inherit global site-packages to your new environment and make it available to all other projects. Leave them unselected.  &lt;/p&gt;
1223 &lt;p&gt;Click &lt;em&gt;Create&lt;/em&gt; on the bottom right and you will see the new project created:&lt;/p&gt;
1224 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-project-created.99dffd1d4e9a.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-project-created.99dffd1d4e9a.png&quot; width=&quot;1174&quot; height=&quot;734&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-project-created.99dffd1d4e9a.png&amp;amp;w=293&amp;amp;sig=d6394f174acab8ee63eb6ce0360d0174857f7afb 293w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-project-created.99dffd1d4e9a.png&amp;amp;w=587&amp;amp;sig=2fd8ea44cc0ad15f015aab687018ec7ad8861a53 587w, https://files.realpython.com/media/pycharm-project-created.99dffd1d4e9a.png 1174w&quot; sizes=&quot;75vw&quot; alt=&quot;Project created in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1225 &lt;p&gt;You will also see a small &lt;em&gt;Tip of the Day&lt;/em&gt; popup where PyCharm gives you one trick to learn at each startup. Go ahead and close this popup.&lt;/p&gt;
1226 &lt;p&gt;It is now time to start a new Python program. Type &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-n&quot;&gt;N&lt;/kbd&gt;&lt;/span&gt; if you are on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-alt&quot;&gt;Alt&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-insert&quot;&gt;Ins&lt;/kbd&gt;&lt;/span&gt; if you are on Windows or Linux. Then, choose &lt;em&gt;Python File&lt;/em&gt;. You can also select &lt;em&gt;File โ†’ New&lt;/em&gt; from the menu. Name the new file &lt;code&gt;guess_game.py&lt;/code&gt; and click &lt;em&gt;OK&lt;/em&gt;. You will see a PyCharm window similar to the following:&lt;/p&gt;
1227 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-new-file.7ea9902d73ea.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-new-file.7ea9902d73ea.png&quot; width=&quot;1172&quot; height=&quot;734&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-new-file.7ea9902d73ea.png&amp;amp;w=293&amp;amp;sig=b1ee432e97d642aea67818cc7280971247196a62 293w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-new-file.7ea9902d73ea.png&amp;amp;w=586&amp;amp;sig=3563b00140a3edd3f1df0e522d5069f6efcb62d3 586w, https://files.realpython.com/media/pycharm-new-file.7ea9902d73ea.png 1172w&quot; sizes=&quot;75vw&quot; alt=&quot;PyCharm New File&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1228 &lt;p&gt;For our test code, let&amp;rsquo;s quickly code up a simple guessing game in which the program chooses a number that the user has to guess. For every guess, the program will tell if the user&amp;rsquo;s guess was smaller or bigger than the secret number. The game ends when the user guesses the number. Here&amp;rsquo;s the code for the game:&lt;/p&gt;
1229 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;
1230 &lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
1231 &lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
1232 &lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;random_int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1233 &lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;
1234 &lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
1235 &lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;user_guess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;What number did we guess (0-100)?&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
1236 &lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;
1237 &lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_guess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
1238 &lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;            &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;You found the number (&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{random_int}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;). Congrats!&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1239 &lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
1240 &lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;
1241 &lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_guess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random_int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
1242 &lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;            &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Your number is less than the number we guessed.&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1243 &lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
1244 &lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;
1245 &lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_guess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random_int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
1246 &lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;            &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Your number is more than the number we guessed.&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1247 &lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
1248 &lt;span class=&quot;lineno&quot;&gt;20 &lt;/span&gt;
1249 &lt;span class=&quot;lineno&quot;&gt;21 &lt;/span&gt;
1250 &lt;span class=&quot;lineno&quot;&gt;22 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vm&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;__main__&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
1251 &lt;span class=&quot;lineno&quot;&gt;23 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
1252 &lt;/pre&gt;&lt;/div&gt;
1253 
1254 &lt;p&gt;Type this code directly rather than copying and pasting. You&amp;rsquo;ll see something like this:&lt;/p&gt;
1255 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/typing-guess-game.fcaedeb8ece2.gif&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/typing-guess-game.fcaedeb8ece2.gif&quot; width=&quot;528&quot; height=&quot;480&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/typing-guess-game.fcaedeb8ece2.gif&amp;amp;w=132&amp;amp;sig=7e5eb20fb9ae97b1cea80380f9ad00f35dd76707 132w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/typing-guess-game.fcaedeb8ece2.gif&amp;amp;w=264&amp;amp;sig=eb242bca301203a38741a986d7847cf0f3ef4cff 264w, https://files.realpython.com/media/typing-guess-game.fcaedeb8ece2.gif 528w&quot; sizes=&quot;75vw&quot; alt=&quot;Typing Guessing Game&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1256 &lt;p&gt;As you can see, PyCharm provides &lt;a href=&quot;https://www.jetbrains.com/pycharm/features/coding_assistance.html&quot;&gt;Intelligent Coding Assistance&lt;/a&gt; with code completion, code inspections, on-the-fly error highlighting, and quick-fix suggestions. In particular, note how when you typed &lt;code&gt;main&lt;/code&gt; and then hit tab, PyCharm auto-completed the whole &lt;code&gt;main&lt;/code&gt; clause for you. &lt;/p&gt;
1257 &lt;p&gt;Also note how, if you forget to type &lt;code&gt;if&lt;/code&gt; before the condition, append &lt;code&gt;.if&lt;/code&gt;, and then hit &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-tab&quot;&gt;Tab&lt;/kbd&gt;&lt;/span&gt;, PyCharm fixes the &lt;code&gt;if&lt;/code&gt; clause for you. The same is true with &lt;code&gt;True.while&lt;/code&gt;. That&amp;rsquo;s &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/settings-postfix-completion.html&quot;&gt;PyCharm&amp;rsquo;s Postfix completions&lt;/a&gt; working for you to help reduce backward caret jumps.&lt;/p&gt;
1258 &lt;h2 id=&quot;running-code-in-pycharm&quot;&gt;Running Code in PyCharm&lt;/h2&gt;
1259 &lt;p&gt;Now that you&amp;rsquo;ve coded up the game, it&amp;rsquo;s time for you to run it.&lt;/p&gt;
1260 &lt;p&gt;You have three ways of running this program:&lt;/p&gt;
1261 &lt;ol&gt;
1262 &lt;li&gt;Use the shortcut &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-r&quot;&gt;R&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-f10&quot;&gt;F10&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux.&lt;/li&gt;
1263 &lt;li&gt;Right-click the background and choose &lt;em&gt;Run &amp;lsquo;guess_game&amp;rsquo;&lt;/em&gt; from the menu.&lt;/li&gt;
1264 &lt;li&gt;Since this program has the &lt;code&gt;__main__&lt;/code&gt; clause, you can click on the little green arrow to the left of the &lt;code&gt;__main__&lt;/code&gt; clause and choose &lt;em&gt;Run &amp;lsquo;guess_game&amp;rsquo;&lt;/em&gt; from there.&lt;/li&gt;
1265 &lt;/ol&gt;
1266 &lt;p&gt;Use any one of the options above to run the program, and you&amp;rsquo;ll see the Run Tool pane appear at the bottom of the window, with your code output showing:&lt;/p&gt;
1267 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-running-script.33fb830f45b4.gif&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-running-script.33fb830f45b4.gif&quot; width=&quot;1068&quot; height=&quot;720&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-running-script.33fb830f45b4.gif&amp;amp;w=267&amp;amp;sig=44be962297881f8ae66557c19905a55202ee14de 267w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-running-script.33fb830f45b4.gif&amp;amp;w=534&amp;amp;sig=000552999cea7a9ce79eeec2f9db7361e9585d77 534w, https://files.realpython.com/media/pycharm-running-script.33fb830f45b4.gif 1068w&quot; sizes=&quot;75vw&quot; alt=&quot;Running a script in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1268 &lt;p&gt;Play the game for a little bit to see if you can find the number guessed. Pro tip: start with 50.   &lt;/p&gt;
1269 &lt;h2 id=&quot;debugging-in-pycharm&quot;&gt;Debugging in PyCharm&lt;/h2&gt;
1270 &lt;p&gt;Did you find the number? If so, you may have seen something weird after you found the number. Instead of printing the congratulations message and exiting, the program seems to start over. That&amp;rsquo;s a bug right there. To discover why the program starts over, you&amp;rsquo;ll now debug the program.&lt;/p&gt;
1271 &lt;p&gt;First, place a breakpoint by clicking on the blank space to the left of line number 8:&lt;/p&gt;
1272 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-debug-breakpoint.55cf93c49859.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-debug-breakpoint.55cf93c49859.png&quot; width=&quot;1042&quot; height=&quot;710&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-debug-breakpoint.55cf93c49859.png&amp;amp;w=260&amp;amp;sig=e714eeae34fad6c0e5889bee0f236f9c30e100a0 260w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-debug-breakpoint.55cf93c49859.png&amp;amp;w=521&amp;amp;sig=7c4692a273d49a627785a715fd0a08d4c8120649 521w, https://files.realpython.com/media/pycharm-debug-breakpoint.55cf93c49859.png 1042w&quot; sizes=&quot;75vw&quot; alt=&quot;Debug breakpoint in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1273 &lt;p&gt;This will be the point where the program will be suspended, and you can start exploring what went wrong from there on. Next, choose one of the following three ways to start debugging:&lt;/p&gt;
1274 &lt;ol&gt;
1275 &lt;li&gt;Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-d&quot;&gt;D&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-alt&quot;&gt;Alt&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-f9&quot;&gt;F9&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux.&lt;/li&gt;
1276 &lt;li&gt;Right-click the background and choose &lt;em&gt;Debug &amp;lsquo;guess_game&amp;rsquo;&lt;/em&gt;.&lt;/li&gt;
1277 &lt;li&gt;Click on the little green arrow to the left of the &lt;code&gt;__main__&lt;/code&gt; clause and choose &lt;em&gt;Debug &amp;lsquo;guess_game&lt;/em&gt; from there.&lt;/li&gt;
1278 &lt;/ol&gt;
1279 &lt;p&gt;Afterwards, you&amp;rsquo;ll see a &lt;em&gt;Debug&lt;/em&gt; window open at the bottom:&lt;/p&gt;
1280 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-debugging-start.04246b743469.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-debugging-start.04246b743469.png&quot; width=&quot;1043&quot; height=&quot;711&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-debugging-start.04246b743469.png&amp;amp;w=260&amp;amp;sig=cea78f8df9a7f183330e3610c90a2abeab879923 260w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-debugging-start.04246b743469.png&amp;amp;w=521&amp;amp;sig=6ae0fe4515cf7f0d52cbcda986eb9c52d0a602ee 521w, https://files.realpython.com/media/pycharm-debugging-start.04246b743469.png 1043w&quot; sizes=&quot;75vw&quot; alt=&quot;Start of debugging in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1281 &lt;p&gt;Follow the steps below to debug the program:&lt;/p&gt;
1282 &lt;ol&gt;
1283 &lt;li&gt;
1284 &lt;p&gt;Notice that the current line is highlighted in blue.&lt;/p&gt;
1285 &lt;/li&gt;
1286 &lt;li&gt;
1287 &lt;p&gt;See that &lt;code&gt;random_int&lt;/code&gt; and its value are listed in the Debug window. Make a note of this number. (In the picture, the number is 85.)&lt;/p&gt;
1288 &lt;/li&gt;
1289 &lt;li&gt;
1290 &lt;p&gt;Hit &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-f8&quot;&gt;F8&lt;/kbd&gt;&lt;/span&gt; to execute the current line and step &lt;em&gt;over&lt;/em&gt; to the next one. You can also use &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-f7&quot;&gt;F7&lt;/kbd&gt;&lt;/span&gt; to step &lt;em&gt;into&lt;/em&gt; the function in the current line, if necessary. As you continue executing the statements, the changes in the variables will be automatically reflected in the Debugger window.&lt;/p&gt;
1291 &lt;/li&gt;
1292 &lt;li&gt;
1293 &lt;p&gt;Notice that there is the Console tab right next to the Debugger tab that opened. This Console tab and the Debugger tab are mutually exclusive. In the Console tab, you will be interacting with your program, and in the Debugger tab you will do the debugging actions.&lt;/p&gt;
1294 &lt;/li&gt;
1295 &lt;li&gt;
1296 &lt;p&gt;Switch to the Console tab to enter your guess.&lt;/p&gt;
1297 &lt;/li&gt;
1298 &lt;li&gt;
1299 &lt;p&gt;Type the number shown, and then hit &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-enter&quot;&gt;Enter&lt;/kbd&gt;&lt;/span&gt;.&lt;/p&gt;
1300 &lt;/li&gt;
1301 &lt;li&gt;
1302 &lt;p&gt;Switch back to the Debugger tab.&lt;/p&gt;
1303 &lt;/li&gt;
1304 &lt;li&gt;
1305 &lt;p&gt;Hit &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-f8&quot;&gt;F8&lt;/kbd&gt;&lt;/span&gt; again to evaluate the &lt;code&gt;if&lt;/code&gt; statement. Notice that you are now on line 14. But wait a minute! Why didn&amp;rsquo;t it go to the line 11? The reason is that the &lt;code&gt;if&lt;/code&gt; statement on line 10 evaluated to &lt;code&gt;False&lt;/code&gt;. But why did it evaluate to &lt;code&gt;False&lt;/code&gt; when you entered the number that was chosen?&lt;/p&gt;
1306 &lt;/li&gt;
1307 &lt;li&gt;
1308 &lt;p&gt;Look carefully at line 10 and notice that we are comparing &lt;code&gt;user_guess&lt;/code&gt; with the wrong thing. Instead of comparing it with &lt;code&gt;random_int&lt;/code&gt;, we are comparing it with &lt;code&gt;randint&lt;/code&gt;, the function that was imported from the &lt;code&gt;random&lt;/code&gt; package.&lt;/p&gt;
1309 &lt;/li&gt;
1310 &lt;li&gt;
1311 &lt;p&gt;Change it to &lt;code&gt;random_int&lt;/code&gt;, restart the debugging, and follow the same steps again. You will see that, this time, it will go to line 11, and line 10 will evaluate to &lt;code&gt;True&lt;/code&gt;:&lt;/p&gt;
1312 &lt;/li&gt;
1313 &lt;/ol&gt;
1314 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-debugging-scripts.bb5a077da438.gif&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-debugging-scripts.bb5a077da438.gif&quot; width=&quot;1092&quot; height=&quot;720&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-debugging-scripts.bb5a077da438.gif&amp;amp;w=273&amp;amp;sig=fc5de269fbc13ea5c1d8be4ca7f525e04a4bb68c 273w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-debugging-scripts.bb5a077da438.gif&amp;amp;w=546&amp;amp;sig=4719f69fd6e78ed3e68f16a9355d00d0edd85a26 546w, https://files.realpython.com/media/pycharm-debugging-scripts.bb5a077da438.gif 1092w&quot; sizes=&quot;75vw&quot; alt=&quot;Debugging Script in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1315 &lt;p&gt;Congratulations! You fixed the bug.&lt;/p&gt;
1316 &lt;h2 id=&quot;testing-in-pycharm&quot;&gt;Testing in PyCharm&lt;/h2&gt;
1317 &lt;p&gt;No application is reliable without unit tests. PyCharm helps you write and run them very quickly and comfortably. By default, &lt;a href=&quot;https://docs.python.org/3/library/unittest.html&quot;&gt;&lt;code&gt;unittest&lt;/code&gt;&lt;/a&gt; is used as the test runner, but PyCharm also supports other testing frameworks such as &lt;a href=&quot;http://www.pytest.org/en/latest/&quot;&gt;&lt;code&gt;pytest&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://nose.readthedocs.io/en/latest/&quot;&gt;&lt;code&gt;nose&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://docs.python.org/3/library/doctest.html&quot;&gt;&lt;code&gt;doctest&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/tox-support.html&quot;&gt;&lt;code&gt;tox&lt;/code&gt;&lt;/a&gt;, and &lt;a href=&quot;https://twistedmatrix.com/trac/wiki/TwistedTrial&quot;&gt;&lt;code&gt;trial&lt;/code&gt;&lt;/a&gt;. You can, for example, enable &lt;code&gt;pytest&lt;/code&gt; for your project like this:&lt;/p&gt;
1318 &lt;ol&gt;
1319 &lt;li&gt;Open the &lt;em&gt;Settings/Preferences โ†’ Tools โ†’ Python Integrated Tools&lt;/em&gt; settings dialog.&lt;/li&gt;
1320 &lt;li&gt;Select &lt;code&gt;pytest&lt;/code&gt; in the Default test runner field.&lt;/li&gt;
1321 &lt;li&gt;Click &lt;em&gt;OK&lt;/em&gt; to save the settings. &lt;/li&gt;
1322 &lt;/ol&gt;
1323 &lt;p&gt;For this example, we&amp;rsquo;ll be using the default test runner &lt;code&gt;unittest&lt;/code&gt;. &lt;/p&gt;
1324 &lt;p&gt;In the same project, create a file called &lt;code&gt;calculator.py&lt;/code&gt; and put the following &lt;code&gt;Calculator&lt;/code&gt; class in it:&lt;/p&gt;
1325 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Calculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
1326 &lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1327 &lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
1328 &lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;
1329 &lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1330 &lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
1331 &lt;/pre&gt;&lt;/div&gt;
1332 
1333 &lt;p&gt;PyCharm makes it very easy to create tests for your existing code. With the &lt;code&gt;calculator.py&lt;/code&gt; file open, execute any one of the following that you like:&lt;/p&gt;
1334 &lt;ul&gt;
1335 &lt;li&gt;Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-t&quot;&gt;T&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-t&quot;&gt;T&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux.&lt;/li&gt;
1336 &lt;li&gt;Right-click in the background of the class and then choose &lt;em&gt;Go To&lt;/em&gt; and &lt;em&gt;Test&lt;/em&gt;.&lt;/li&gt;
1337 &lt;li&gt;On the main menu, choose &lt;em&gt;Navigate โ†’ Test&lt;/em&gt;.&lt;/li&gt;
1338 &lt;/ul&gt;
1339 &lt;p&gt;Choose &lt;em&gt;Create New Test&amp;hellip;&lt;/em&gt;, and you will see the following window:&lt;/p&gt;
1340 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-create-tests.9a6cea78f9c6.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-create-tests.9a6cea78f9c6.png&quot; width=&quot;500&quot; height=&quot;402&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-create-tests.9a6cea78f9c6.png&amp;amp;w=125&amp;amp;sig=0c50b83f35578fd8004dce9e7d55fcd3b09a1967 125w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-create-tests.9a6cea78f9c6.png&amp;amp;w=250&amp;amp;sig=f6740142e71d2d2f6196ffbe341ae09bc9a64453 250w, https://files.realpython.com/media/pycharm-create-tests.9a6cea78f9c6.png 500w&quot; sizes=&quot;75vw&quot; alt=&quot;Create tests in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1341 &lt;p&gt;Leave the defaults of &lt;em&gt;Target directory&lt;/em&gt;, &lt;em&gt;Test file name&lt;/em&gt;, and &lt;em&gt;Test class name&lt;/em&gt;. Select both of the methods and click &lt;em&gt;OK&lt;/em&gt;. Voila! PyCharm automatically created a file called &lt;code&gt;test_calculator.py&lt;/code&gt; and created the following stub tests for you in it:&lt;/p&gt;
1342 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;unittest&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TestCase&lt;/span&gt;
1343 &lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
1344 &lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TestCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TestCase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1345 &lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1346 &lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
1347 &lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;
1348 &lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1349 &lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
1350 &lt;/pre&gt;&lt;/div&gt;
1351 
1352 &lt;p&gt;Run the tests using one of the methods below:&lt;/p&gt;
1353 &lt;ul&gt;
1354 &lt;li&gt;Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-r&quot;&gt;R&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-f10&quot;&gt;F10&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux.&lt;/li&gt;
1355 &lt;li&gt;Right-click the background and choose &lt;em&gt;Run &amp;lsquo;Unittests for test_calculator.py&amp;rsquo;&lt;/em&gt;.&lt;/li&gt;
1356 &lt;li&gt;Click on the little green arrow to the left of the test class name and choose &lt;em&gt;Run &amp;lsquo;Unittests for test_calculator.py&amp;rsquo;&lt;/em&gt;.&lt;/li&gt;
1357 &lt;/ul&gt;
1358 &lt;p&gt;You&amp;rsquo;ll see the tests window open on the bottom with all the tests failing:&lt;/p&gt;
1359 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-failed-tests.810aa9c365cb.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-failed-tests.810aa9c365cb.png&quot; width=&quot;972&quot; height=&quot;645&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-failed-tests.810aa9c365cb.png&amp;amp;w=243&amp;amp;sig=cb7ef285c20ed83b9771a91cc38d77342e4d3745 243w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-failed-tests.810aa9c365cb.png&amp;amp;w=486&amp;amp;sig=aaec55471da2d168048783b9c4e573f5ce876de4 486w, https://files.realpython.com/media/pycharm-failed-tests.810aa9c365cb.png 972w&quot; sizes=&quot;75vw&quot; alt=&quot;Failed tests in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1360 &lt;p&gt;Notice that you have the hierarchy of the test results on the left and the output of the terminal on the right. &lt;/p&gt;
1361 &lt;p&gt;Now, implement &lt;code&gt;test_add&lt;/code&gt; by changing the code to the following:&lt;/p&gt;
1362 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;unittest&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TestCase&lt;/span&gt;
1363 &lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
1364 &lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;calculator&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Calculator&lt;/span&gt;
1365 &lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;
1366 &lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TestCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TestCase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1367 &lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1368 &lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Calculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
1369 &lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assertEqual&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1370 &lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;
1371 &lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1372 &lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
1373 &lt;/pre&gt;&lt;/div&gt;
1374 
1375 &lt;p&gt;Run the tests again, and you&amp;rsquo;ll see that one test passed and the other failed. Explore the options to show passed tests, to show ignored tests, to sort tests alphabetically, and to sort tests by duration:&lt;/p&gt;
1376 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-running-tests.6077562207ba.gif&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-running-tests.6077562207ba.gif&quot; width=&quot;1092&quot; height=&quot;720&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-running-tests.6077562207ba.gif&amp;amp;w=273&amp;amp;sig=e2238425e1cfb9a9a298741244f3021f3984dbf8 273w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-running-tests.6077562207ba.gif&amp;amp;w=546&amp;amp;sig=85203afd5ff2189fdfadad0960da514b03063953 546w, https://files.realpython.com/media/pycharm-running-tests.6077562207ba.gif 1092w&quot; sizes=&quot;75vw&quot; alt=&quot;Running tests in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1377 &lt;p&gt;Note that the &lt;code&gt;sleep(0.1)&lt;/code&gt; method that you see in the GIF above is intentionally used to make one of the tests slower so that sorting by duration works. &lt;/p&gt;
1378 &lt;h2 id=&quot;editing-an-existing-project-in-pycharm&quot;&gt;Editing an Existing Project in PyCharm&lt;/h2&gt;
1379 &lt;p&gt;These single file projects are great for examples, but you&amp;rsquo;ll often work on much larger projects over a longer period of time. In this section, you&amp;rsquo;ll take a look at how PyCharm works with a larger project. &lt;/p&gt;
1380 &lt;p&gt;To explore the project-focused features of PyCharm, you&amp;rsquo;ll use the Alcazar web framework that was built for learning purposes. To continue following along, clone the repo locally:&lt;/p&gt;
1381 &lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Clone Repo:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/alcazar-web-framework/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-alcazar-web-framework&quot; data-focus=&quot;false&quot;&gt;Click here to clone the repo you&#39;ll use&lt;/a&gt; to explore the project-focused features of PyCharm in this tutorial.&lt;/p&gt;&lt;/div&gt;
1382 
1383 &lt;p&gt;Once you have a project locally, open it in PyCharm using one of the following methods:&lt;/p&gt;
1384 &lt;ul&gt;
1385 &lt;li&gt;Click &lt;em&gt;File โ†’ Open&lt;/em&gt; on the main menu.&lt;/li&gt;
1386 &lt;li&gt;Click &lt;em&gt;Open&lt;/em&gt; on the &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/welcome-screen.html&quot;&gt;Welcome Screen&lt;/a&gt; if you are there.&lt;/li&gt;
1387 &lt;/ul&gt;
1388 &lt;p&gt;After either of these steps, find the folder containing the project on your computer and open it.&lt;/p&gt;
1389 &lt;p&gt;If this project contains a &lt;a href=&quot;https://realpython.com/python-virtual-environments-a-primer/&quot;&gt;virtual environment&lt;/a&gt;, then PyCharm will automatically use this virtual environment and make it the project interpreter.&lt;/p&gt;
1390 &lt;p&gt;If you need to configure a different &lt;code&gt;virtualenv&lt;/code&gt;, then open &lt;em&gt;Preferences&lt;/em&gt; on Mac by pressing &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-comma&quot;&gt;,&lt;/kbd&gt;&lt;/span&gt; or &lt;em&gt;Settings&lt;/em&gt; on Windows or Linux by pressing &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-alt&quot;&gt;Alt&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-s&quot;&gt;S&lt;/kbd&gt;&lt;/span&gt; and find the &lt;em&gt;Project: ProjectName&lt;/em&gt; section. Open the drop-down and choose &lt;em&gt;Project Interpreter&lt;/em&gt;:&lt;/p&gt;
1391 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-project-interpreter.57282306555a.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-project-interpreter.57282306555a.png&quot; width=&quot;1083&quot; height=&quot;723&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-project-interpreter.57282306555a.png&amp;amp;w=270&amp;amp;sig=286643bc473f648bbcce27338c980eb023746ac2 270w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-project-interpreter.57282306555a.png&amp;amp;w=541&amp;amp;sig=6d33c5adf0380e8c5b01ae9430436983580b4b49 541w, https://files.realpython.com/media/pycharm-project-interpreter.57282306555a.png 1083w&quot; sizes=&quot;75vw&quot; alt=&quot;Project interpreter in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1392 &lt;p&gt;Choose the &lt;code&gt;virtualenv&lt;/code&gt; from the drop-down list. If it&amp;rsquo;s not there, then click on the settings button to the right of the drop-down list and then choose &lt;em&gt;Add&amp;hellip;&lt;/em&gt;. The rest of the steps should be the same as when we were &lt;a href=&quot;#writing-code-in-pycharm&quot;&gt;creating a new project&lt;/a&gt;.&lt;/p&gt;
1393 &lt;h2 id=&quot;searching-and-navigating-in-pycharm&quot;&gt;Searching and Navigating in PyCharm&lt;/h2&gt;
1394 &lt;p&gt;In a big project where it&amp;rsquo;s difficult for a single person to remember where everything is located, it&amp;rsquo;s very important to be able to quickly navigate and find what you looking for. PyCharm has you covered here as well. Use the project you opened in the section above to practice these shortcuts: &lt;/p&gt;
1395 &lt;ul&gt;
1396 &lt;li&gt;&lt;strong&gt;Searching for a fragment in the current file:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-f&quot;&gt;F&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-f&quot;&gt;F&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux.&lt;/li&gt;
1397 &lt;li&gt;&lt;strong&gt;Searching for a fragment in the entire project:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-f&quot;&gt;F&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-f&quot;&gt;F&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux.&lt;/li&gt;
1398 &lt;li&gt;&lt;strong&gt;Searching for a class:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-o&quot;&gt;O&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-n&quot;&gt;N&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux.&lt;/li&gt;
1399 &lt;li&gt;&lt;strong&gt;Searching for a file:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-o&quot;&gt;O&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-n&quot;&gt;N&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux.&lt;/li&gt;
1400 &lt;li&gt;&lt;strong&gt;Searching all if you don&amp;rsquo;t know whether it&amp;rsquo;s a file, class, or a code fragment that you are looking for:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;/span&gt; twice.&lt;/li&gt;
1401 &lt;/ul&gt;
1402 &lt;p&gt;As for the navigation, the following shortcuts may save you a lot of time:&lt;/p&gt;
1403 &lt;ul&gt;
1404 &lt;li&gt;&lt;strong&gt;Going to the declaration of a variable:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux, and click on the variable.&lt;/li&gt;
1405 &lt;li&gt;&lt;strong&gt;Finding usages of a class, a method, or any symbol:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-alt&quot;&gt;Alt&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-f7&quot;&gt;F7&lt;/kbd&gt;&lt;/span&gt;.&lt;/li&gt;
1406 &lt;li&gt;&lt;strong&gt;Seeing your recent changes:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-alt&quot;&gt;Alt&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-c&quot;&gt;C&lt;/kbd&gt;&lt;/span&gt; or go to &lt;em&gt;View โ†’ Recent Changes&lt;/em&gt; on the main menu.&lt;/li&gt;
1407 &lt;li&gt;&lt;strong&gt;Seeing your recent files:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-e&quot;&gt;E&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-e&quot;&gt;E&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux, or go to &lt;em&gt;View โ†’ Recent Files&lt;/em&gt; on the main menu.&lt;/li&gt;
1408 &lt;li&gt;&lt;strong&gt;Going backward and forward through your history of navigation after you jumped around:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-bracket-left&quot;&gt;[&lt;/kbd&gt;&lt;/span&gt; / &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-bracket-right&quot;&gt;]&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-alt&quot;&gt;Alt&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-arrow-left&quot;&gt;Left&lt;/kbd&gt;&lt;/span&gt; / &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-alt&quot;&gt;Alt&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-arrow-right&quot;&gt;Right&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux.&lt;/li&gt;
1409 &lt;/ul&gt;
1410 &lt;p&gt;For more details, see the &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/tutorial-exploring-navigation-and-search.html&quot;&gt;official documentation&lt;/a&gt;. &lt;/p&gt;
1411 &lt;h2 id=&quot;using-version-control-in-pycharm&quot;&gt;Using Version Control in PyCharm&lt;/h2&gt;
1412 &lt;p&gt;Version control systems such as &lt;a href=&quot;https://git-scm.com/&quot;&gt;Git&lt;/a&gt; and &lt;a href=&quot;https://www.mercurial-scm.org/&quot;&gt;Mercurial&lt;/a&gt; are some of the most important tools in the modern software development world. So, it is essential for an IDE to support them. PyCharm does that very well by integrating with a lot of popular VC systems such as Git (and &lt;a href=&quot;https://github.com/&quot;&gt;Github&lt;/a&gt;), Mercurial, &lt;a href=&quot;https://www.perforce.com/solutions/version-control&quot;&gt;Perforce&lt;/a&gt; and, &lt;a href=&quot;https://subversion.apache.org/&quot;&gt;Subversion&lt;/a&gt;.&lt;/p&gt;
1413 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
1414 &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: &lt;a href=&quot;https://realpython.com/python-git-github-intro/&quot;&gt;Git&lt;/a&gt; is used for the following examples.&lt;/p&gt;
1415 &lt;/div&gt;
1416 &lt;h3 id=&quot;configuring-vcs&quot;&gt;Configuring VCS&lt;/h3&gt;
1417 &lt;p&gt;To enable VCS integration. Go to &lt;em&gt;VCS โ†’ VCS Operations Popup&amp;hellip;&lt;/em&gt; from the menu on the top or press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-v&quot;&gt;V&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-alt&quot;&gt;Alt&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-grave&quot;&gt;`&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux. Choose &lt;em&gt;Enable Version Control Integration&amp;hellip;&lt;/em&gt;. You&amp;rsquo;ll see the following window open:&lt;/p&gt;
1418 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-enable-vc-integration.b30ec94c1246.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-enable-vc-integration.b30ec94c1246.png&quot; width=&quot;715&quot; height=&quot;147&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-enable-vc-integration.b30ec94c1246.png&amp;amp;w=178&amp;amp;sig=7ef55e4ed6068c86d831adaefc1af11f4c083763 178w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-enable-vc-integration.b30ec94c1246.png&amp;amp;w=357&amp;amp;sig=c701d809b49e9837477c14e8dfe34dc4cfa66c33 357w, https://files.realpython.com/media/pycharm-enable-vc-integration.b30ec94c1246.png 715w&quot; sizes=&quot;75vw&quot; alt=&quot;Enable Version Control Integration in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1419 &lt;p&gt;Choose &lt;em&gt;Git&lt;/em&gt; from the drop down list, click &lt;em&gt;OK&lt;/em&gt;, and you have VCS enabled for your project. Note that if you opened an existing project that has version control enabled, then PyCharm will see that and automatically enable it.&lt;/p&gt;
1420 &lt;p&gt;Now, if you go to the &lt;em&gt;VCS Operations Popup&amp;hellip;&lt;/em&gt;, you&amp;rsquo;ll see a different popup with the options to do &lt;code&gt;git add&lt;/code&gt;, &lt;code&gt;git stash&lt;/code&gt;, &lt;code&gt;git branch&lt;/code&gt;, &lt;code&gt;git commit&lt;/code&gt;, &lt;code&gt;git push&lt;/code&gt; and more:&lt;/p&gt;
1421 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-vcs-operations.70dbafcb983a.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border w-50&quot; src=&quot;https://files.realpython.com/media/pycharm-vcs-operations.70dbafcb983a.png&quot; width=&quot;392&quot; height=&quot;379&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-vcs-operations.70dbafcb983a.png&amp;amp;w=98&amp;amp;sig=f285b015c957936448441c4ec8b03cf8627cdffc 98w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-vcs-operations.70dbafcb983a.png&amp;amp;w=196&amp;amp;sig=a16c449a21dde2a2b0f2f7533c92e71574cd3d60 196w, https://files.realpython.com/media/pycharm-vcs-operations.70dbafcb983a.png 392w&quot; sizes=&quot;75vw&quot; alt=&quot;VCS operations in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1422 &lt;p&gt;If you can&amp;rsquo;t find what you need, you can most probably find it by going to &lt;em&gt;VCS&lt;/em&gt; from the top menu and choosing &lt;em&gt;Git&lt;/em&gt;, where you can even create and view pull requests.&lt;/p&gt;
1423 &lt;h3 id=&quot;committing-and-conflict-resolution&quot;&gt;Committing and Conflict Resolution&lt;/h3&gt;
1424 &lt;p&gt;These are two features of VCS integration in PyCharm that I personally use and enjoy a lot! Let&amp;rsquo;s say you have finished your work and want to commit it. Go to &lt;em&gt;VCS โ†’ VCS Operations Popup&amp;hellip; โ†’ Commit&amp;hellip;&lt;/em&gt; or press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-k&quot;&gt;K&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-k&quot;&gt;K&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux. You&amp;rsquo;ll see the following window open:&lt;/p&gt;
1425 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-commit-window.a4ceff16c2d3.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-commit-window.a4ceff16c2d3.png&quot; width=&quot;929&quot; height=&quot;682&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-commit-window.a4ceff16c2d3.png&amp;amp;w=232&amp;amp;sig=935dabf7a28cf757a5c87165e3da494540c3e4a6 232w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-commit-window.a4ceff16c2d3.png&amp;amp;w=464&amp;amp;sig=7a05fe8a5cbfb1c3524c24515f9a8d7fa3211591 464w, https://files.realpython.com/media/pycharm-commit-window.a4ceff16c2d3.png 929w&quot; sizes=&quot;75vw&quot; alt=&quot;Commit window in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1426 &lt;p&gt;In this window, you can do the following:&lt;/p&gt;
1427 &lt;ol&gt;
1428 &lt;li&gt;Choose which files to commit&lt;/li&gt;
1429 &lt;li&gt;Write your commit message&lt;/li&gt;
1430 &lt;li&gt;Do all kinds of checks and cleanup &lt;a href=&quot;https://www.jetbrains.com/help/idea/commit-changes-dialog.html#before_commit&quot;&gt;before commit&lt;/a&gt;&lt;/li&gt;
1431 &lt;li&gt;See the difference of changes&lt;/li&gt;
1432 &lt;li&gt;Commit and push at once by pressing the arrow to the right of the &lt;em&gt;Commit&lt;/em&gt; button on the right bottom and choosing &lt;em&gt;Commit and Push&amp;hellip;&lt;/em&gt;&lt;/li&gt;
1433 &lt;/ol&gt;
1434 &lt;p&gt;It can feel magical and fast, especially if you&amp;rsquo;re used to doing everything manually on the command line.&lt;/p&gt;
1435 &lt;p&gt;When you work in a team, &lt;strong&gt;merge conflicts&lt;/strong&gt; do happen. When somebody commits changes to a file that you&amp;rsquo;re working on, but their changes overlap with yours because both of you changed the same lines, then VCS will not be able to figure out if it should choose your changes or those of your teammate. So you&amp;rsquo;ll get these unfortunate arrows and symbols:&lt;/p&gt;
1436 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-conflicts.74b23b9ec798.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-conflicts.74b23b9ec798.png&quot; width=&quot;996&quot; height=&quot;691&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-conflicts.74b23b9ec798.png&amp;amp;w=249&amp;amp;sig=d02221be0ce12dbc9a8ea7514e047bb608b16c08 249w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-conflicts.74b23b9ec798.png&amp;amp;w=498&amp;amp;sig=1286c7aa0f62795e01c6a2f0607096b8c0d67b20 498w, https://files.realpython.com/media/pycharm-conflicts.74b23b9ec798.png 996w&quot; sizes=&quot;75vw&quot; alt=&quot;Conflicts in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1437 &lt;p&gt;This looks strange, and it&amp;rsquo;s difficult to figure out which changes should be deleted and which ones should stay. PyCharm to the rescue! It has a much nicer and cleaner way of resolving conflicts. Go to &lt;em&gt;VCS&lt;/em&gt; in the top menu, choose &lt;em&gt;Git&lt;/em&gt; and then &lt;em&gt;Resolve conflicts&amp;hellip;&lt;/em&gt;. Choose the file whose conflicts you want to resolve and click on &lt;em&gt;Merge&lt;/em&gt;. You will see the following window open:&lt;/p&gt;
1438 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-conflict-resolving-window.eea8f79a12b2.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-conflict-resolving-window.eea8f79a12b2.png&quot; width=&quot;1174&quot; height=&quot;709&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-conflict-resolving-window.eea8f79a12b2.png&amp;amp;w=293&amp;amp;sig=ef195e8acbb5ec9fa55fca46a43486995c2efca7 293w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-conflict-resolving-window.eea8f79a12b2.png&amp;amp;w=587&amp;amp;sig=067731ba579736c92c747eb128b3fb02b0d68417 587w, https://files.realpython.com/media/pycharm-conflict-resolving-window.eea8f79a12b2.png 1174w&quot; sizes=&quot;75vw&quot; alt=&quot;Conflict resolving windown in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1439 &lt;p&gt;On the left column, you will see your changes. On the right one, the changes made by your teammate. Finally, in the middle column, you will see the result. The conflicting lines are highlighted, and you can see a little &lt;em&gt;X&lt;/em&gt; and &lt;em&gt;&amp;gt;&amp;gt;&lt;/em&gt;/&lt;em&gt;&amp;lt;&amp;lt;&lt;/em&gt; right beside those lines. Press the arrows to accept the changes and the &lt;em&gt;X&lt;/em&gt; to decline. After you resolve all those conflicts, click the &lt;em&gt;Apply&lt;/em&gt; button: &lt;/p&gt;
1440 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-resolving-conflicts.d3128ce78c45.gif&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-resolving-conflicts.d3128ce78c45.gif&quot; width=&quot;1200&quot; height=&quot;720&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-resolving-conflicts.d3128ce78c45.gif&amp;amp;w=300&amp;amp;sig=099dcca659431f9d2a1315b1fea5d7cbe246425c 300w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-resolving-conflicts.d3128ce78c45.gif&amp;amp;w=600&amp;amp;sig=6b98751300d8750c93d80cf7b65e96fb57d81714 600w, https://files.realpython.com/media/pycharm-resolving-conflicts.d3128ce78c45.gif 1200w&quot; sizes=&quot;75vw&quot; alt=&quot;Resolving Conflicts in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1441 &lt;p&gt;In the GIF above, for the first conflicting line, the author declined his own changes and accepted those of his teammates. Conversely, the author accepted his own changes and declined his teammates&amp;rsquo; for the second conflicting line.&lt;/p&gt;
1442 &lt;p&gt;There&amp;rsquo;s a lot more that you can do with the VCS integration in PyCharm. For more details, see &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/version-control-integration.html&quot;&gt;this documentation&lt;/a&gt;.&lt;/p&gt;
1443 &lt;h2 id=&quot;using-plugins-and-external-tools-in-pycharm&quot;&gt;Using Plugins and External Tools in PyCharm&lt;/h2&gt;
1444 &lt;p&gt;You can find almost everything you need for development in PyCharm. If you can&amp;rsquo;t, there is most probably a &lt;a href=&quot;https://plugins.jetbrains.com/&quot;&gt;plugin&lt;/a&gt; that adds that functionality you need to PyCharm. For example, they can:&lt;/p&gt;
1445 &lt;ul&gt;
1446 &lt;li&gt;Add support for various languages and frameworks &lt;/li&gt;
1447 &lt;li&gt;Boost your productivity with shortcut hints, file watchers, and so on &lt;/li&gt;
1448 &lt;li&gt;Help you learn a new programming language with coding exercises&lt;/li&gt;
1449 &lt;/ul&gt;
1450 &lt;p&gt;For instance, &lt;a href=&quot;https://plugins.jetbrains.com/plugin/164-ideavim&quot;&gt;IdeaVim&lt;/a&gt; adds Vim emulation to PyCharm. If you like Vim, this can be a pretty good combination. &lt;/p&gt;
1451 &lt;p&gt;&lt;a href=&quot;https://plugins.jetbrains.com/plugin/8006-material-theme-ui&quot;&gt;Material Theme UI&lt;/a&gt; changes the appearance of PyCharm to a Material Design look and feel: &lt;/p&gt;
1452 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-material-theme.178175815adc.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/pycharm-material-theme.178175815adc.png&quot; width=&quot;1110&quot; height=&quot;743&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-material-theme.178175815adc.png&amp;amp;w=277&amp;amp;sig=60ec4c8b5f6a89af345a230518e21ee8a33d174b 277w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-material-theme.178175815adc.png&amp;amp;w=555&amp;amp;sig=fdac8603145ed77eb443abcbbe8ebdb9811bdff4 555w, https://files.realpython.com/media/pycharm-material-theme.178175815adc.png 1110w&quot; sizes=&quot;75vw&quot; alt=&quot;Material Theme in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1453 &lt;p&gt;&lt;a href=&quot;https://plugins.jetbrains.com/plugin/9442-vue-js&quot;&gt;Vue.js&lt;/a&gt; adds support for &lt;a href=&quot;https://vuejs.org/&quot;&gt;Vue.js&lt;/a&gt; projects. &lt;a href=&quot;https://plugins.jetbrains.com/plugin/7793-markdown&quot;&gt;Markdown&lt;/a&gt; provides the capability to edit Markdown files within the IDE and see the rendered HTML in a live preview. You can find and install all of the available plugins by going to the &lt;em&gt;Preferences โ†’ Plugins&lt;/em&gt; on Mac or &lt;em&gt;Settings โ†’ Plugins&lt;/em&gt; on Windows or Linux, under the &lt;em&gt;Marketplace&lt;/em&gt; tab:&lt;/p&gt;
1454 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-plugin-marketplace.7d1cecfdc8b3.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-plugin-marketplace.7d1cecfdc8b3.png&quot; width=&quot;1047&quot; height=&quot;687&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-plugin-marketplace.7d1cecfdc8b3.png&amp;amp;w=261&amp;amp;sig=8d4a9ba35b5eb27b5604f86108b79f28e40f3cc9 261w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-plugin-marketplace.7d1cecfdc8b3.png&amp;amp;w=523&amp;amp;sig=ec5e20ae96bf9ee37a1011bcc2203e7d1599b4a4 523w, https://files.realpython.com/media/pycharm-plugin-marketplace.7d1cecfdc8b3.png 1047w&quot; sizes=&quot;75vw&quot; alt=&quot;Plugin Marketplace in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1455 &lt;p&gt;If you can&amp;rsquo;t find what you need, you can even &lt;a href=&quot;http://www.jetbrains.org/intellij/sdk/docs/basics.html&quot;&gt;develop your own plugin&lt;/a&gt;.&lt;/p&gt;
1456 &lt;p&gt;If you can&amp;rsquo;t find the right plugin and don&amp;rsquo;t want to develop your own because there&amp;rsquo;s already a package in PyPI, then you can add it to PyCharm as an external tool. Take &lt;a href=&quot;http://flake8.pycqa.org/en/latest/&quot;&gt;&lt;code&gt;Flake8&lt;/code&gt;&lt;/a&gt;, the code analyzer, as an example. &lt;/p&gt;
1457 &lt;p&gt;First, install &lt;code&gt;flake8&lt;/code&gt; in your virtualenv with &lt;code&gt;pip install flake8&lt;/code&gt; in the Terminal app of your choice. You can also use the one integrated into PyCharm:&lt;/p&gt;
1458 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-terminal.bb20cae6697e.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/pycharm-terminal.bb20cae6697e.png&quot; width=&quot;972&quot; height=&quot;646&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-terminal.bb20cae6697e.png&amp;amp;w=243&amp;amp;sig=860217f31e60a4bb574e169ee05b6788cacaa388 243w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-terminal.bb20cae6697e.png&amp;amp;w=486&amp;amp;sig=26a30f328e42cc19e7e652c44767093480dbf352 486w, https://files.realpython.com/media/pycharm-terminal.bb20cae6697e.png 972w&quot; sizes=&quot;75vw&quot; alt=&quot;Terminal in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1459 &lt;p&gt;Then, go to &lt;em&gt;Preferences โ†’ Tools&lt;/em&gt; on Mac or &lt;em&gt;Settings โ†’ Tools&lt;/em&gt; on Windows/Linux, and then choose &lt;em&gt;External Tools&lt;/em&gt;. Then click on the little &lt;em&gt;+&lt;/em&gt; button at the bottom (1). In the new popup window, insert the details as shown below and click &lt;em&gt;OK&lt;/em&gt; for both windows:&lt;/p&gt;
1460 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-flake8-tool.3963506224b4.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/pycharm-flake8-tool.3963506224b4.png&quot; width=&quot;1082&quot; height=&quot;720&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-flake8-tool.3963506224b4.png&amp;amp;w=270&amp;amp;sig=152f2ccf0a75a950b6a5cd6b5087507b66288595 270w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-flake8-tool.3963506224b4.png&amp;amp;w=541&amp;amp;sig=ee7221ee226076dfeaedfd5d2756263e351d8d14 541w, https://files.realpython.com/media/pycharm-flake8-tool.3963506224b4.png 1082w&quot; sizes=&quot;75vw&quot; alt=&quot;Flake8 tool in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1461 &lt;p&gt;Here, &lt;em&gt;Program&lt;/em&gt; (2) refers to the Flake8 executable that can be found in the folder &lt;em&gt;/bin&lt;/em&gt; of your virtual environment. &lt;em&gt;Arguments&lt;/em&gt; (3) refers to which file you want to analyze with the help of Flake8. &lt;em&gt;Working directory&lt;/em&gt; is the directory of your project.&lt;/p&gt;
1462 &lt;p&gt;You could hardcode the absolute paths for everything here, but that would mean that you couldn&amp;rsquo;t use this external tool in other projects. You would be able to use it only inside one project for one file. &lt;/p&gt;
1463 &lt;p&gt;So you need to use something called &lt;em&gt;Macros&lt;/em&gt;. Macros are basically variables in the format of &lt;code&gt;$name$&lt;/code&gt; that change according to your context. For example, &lt;code&gt;$FileName$&lt;/code&gt; is &lt;code&gt;first.py&lt;/code&gt; when you&amp;rsquo;re editing &lt;code&gt;first.py&lt;/code&gt;, and it is &lt;code&gt;second.py&lt;/code&gt; when you&amp;rsquo;re editing &lt;code&gt;second.py&lt;/code&gt;. You can see their list and insert any of them by clicking on the &lt;em&gt;Insert Macro&amp;hellip;&lt;/em&gt; buttons. Because you used macros here, the values will change according to the project you&amp;rsquo;re currently working on, and Flake8 will continue to do its job properly.   &lt;/p&gt;
1464 &lt;p&gt;In order to use it, create a file &lt;code&gt;example.py&lt;/code&gt; and put the following code in it:&lt;/p&gt;
1465 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CONSTANT_VAR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
1466 &lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
1467 &lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;
1468 &lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;
1469 &lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1470 &lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;
1471 &lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
1472 &lt;/pre&gt;&lt;/div&gt;
1473 
1474 &lt;p&gt;It deliberately breaks some of the Flake8 rules. Right-click the background of this file. Choose &lt;em&gt;External Tools&lt;/em&gt; and then &lt;em&gt;Flake8&lt;/em&gt;. Voilร ! The output of the Flake8 analysis will appear at the bottom: &lt;/p&gt;
1475 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-flake8-output.5b78e911e6d3.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-flake8-output.5b78e911e6d3.png&quot; width=&quot;997&quot; height=&quot;634&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-flake8-output.5b78e911e6d3.png&amp;amp;w=249&amp;amp;sig=8fecbaaf9d4e2daa2bbe443be4b6dee2634f2a46 249w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-flake8-output.5b78e911e6d3.png&amp;amp;w=498&amp;amp;sig=4e41c6dbac28fbe23c18716f49c3cadf63767500 498w, https://files.realpython.com/media/pycharm-flake8-output.5b78e911e6d3.png 997w&quot; sizes=&quot;75vw&quot; alt=&quot;Flake8 Output in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1476 &lt;p&gt;In order to make it even better, you can add a shortcut for it. Go to &lt;em&gt;Preferences&lt;/em&gt; on Mac or to &lt;em&gt;Settings&lt;/em&gt; on Windows or Linux. Then, go to &lt;em&gt;Keymap โ†’ External Tools โ†’ External Tools&lt;/em&gt;. Double-click &lt;em&gt;Flake8&lt;/em&gt; and choose &lt;em&gt;Add Keyboard Shortcut&lt;/em&gt;. You&amp;rsquo;ll see this window:&lt;/p&gt;
1477 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-add-shortcut.8c66b2bd12c0.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/pycharm-add-shortcut.8c66b2bd12c0.png&quot; width=&quot;1084&quot; height=&quot;724&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-add-shortcut.8c66b2bd12c0.png&amp;amp;w=271&amp;amp;sig=e95cc32634af125588e6881ec6992dace79ec667 271w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-add-shortcut.8c66b2bd12c0.png&amp;amp;w=542&amp;amp;sig=836cbeec5256df8e8048aa6a0c3e7d89b2bac444 542w, https://files.realpython.com/media/pycharm-add-shortcut.8c66b2bd12c0.png 1084w&quot; sizes=&quot;75vw&quot; alt=&quot;Add shortcut in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1478 &lt;p&gt;In the image above, the shortcut is &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-alt&quot;&gt;Alt&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-a&quot;&gt;A&lt;/kbd&gt;&lt;/span&gt; for this tool. Add your preferred shortcut in the textbox and click &lt;em&gt;OK&lt;/em&gt; for both windows. Now you can now use that shortcut to analyze the file you&amp;rsquo;re currently working on with Flake8.&lt;/p&gt;
1479 &lt;h2 id=&quot;pycharm-professional-features&quot;&gt;PyCharm Professional Features&lt;/h2&gt;
1480 &lt;p&gt;PyCharm Professional is a paid version of PyCharm with more out-of-the-box features and integrations. In this section, you&amp;rsquo;ll mainly be presented with overviews of its main features and links to the official documentation, where each feature is discussed in detail. Remember that none of the following features is available in the Community edition. &lt;/p&gt;
1481 &lt;h3 id=&quot;django-support&quot;&gt;Django Support&lt;/h3&gt;
1482 &lt;p&gt;PyCharm has extensive support for &lt;a href=&quot;https://www.djangoproject.com/&quot;&gt;Django&lt;/a&gt;, one of the most popular and beloved &lt;a href=&quot;https://realpython.com/learning-paths/become-python-web-developer/&quot;&gt;Python web frameworks&lt;/a&gt;. To make sure that it&amp;rsquo;s enabled, do the following:&lt;/p&gt;
1483 &lt;ol&gt;
1484 &lt;li&gt;Open &lt;em&gt;Preferences&lt;/em&gt; on Mac or &lt;em&gt;Settings&lt;/em&gt; on Windows or Linux.&lt;/li&gt;
1485 &lt;li&gt;Choose &lt;em&gt;Languages and Frameworks&lt;/em&gt;.&lt;/li&gt;
1486 &lt;li&gt;Choose &lt;em&gt;Django&lt;/em&gt;.&lt;/li&gt;
1487 &lt;li&gt;Check the checkbox &lt;em&gt;Enable Django support&lt;/em&gt;.&lt;/li&gt;
1488 &lt;li&gt;Apply changes.&lt;/li&gt;
1489 &lt;/ol&gt;
1490 &lt;p&gt;Now that you&amp;rsquo;ve enabled Django support, your Django development journey will be a lot easier in PyCharm:&lt;/p&gt;
1491 &lt;ul&gt;
1492 &lt;li&gt;When creating a project, you&amp;rsquo;ll have a dedicated Django project type. This means that, when you choose this type, you&amp;rsquo;ll have all the necessary files and settings. This is the equivalent of using &lt;code&gt;django-admin startproject mysite&lt;/code&gt;.&lt;/li&gt;
1493 &lt;li&gt;You can run &lt;code&gt;manage.py&lt;/code&gt; commands directly inside PyCharm. &lt;/li&gt;
1494 &lt;li&gt;Django templates are supported, including:&lt;ul&gt;
1495 &lt;li&gt;Syntax and error highlighting&lt;/li&gt;
1496 &lt;li&gt;Code completion&lt;/li&gt;
1497 &lt;li&gt;Navigation&lt;/li&gt;
1498 &lt;li&gt;Completion for block names&lt;/li&gt;
1499 &lt;li&gt;Completion for custom tags and filters&lt;/li&gt;
1500 &lt;li&gt;Quick documentation for tags and filters&lt;/li&gt;
1501 &lt;li&gt;Capability to debug them&lt;/li&gt;
1502 &lt;/ul&gt;
1503 &lt;/li&gt;
1504 &lt;li&gt;Code completion in all other Django parts such as views, URLs and models, and code insight support for Django ORM.&lt;/li&gt;
1505 &lt;li&gt;Model dependency diagrams for Django models.&lt;/li&gt;
1506 &lt;/ul&gt;
1507 &lt;p&gt;For more details on Django support, see the &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/django-support7.html&quot;&gt;official documentation&lt;/a&gt;.&lt;/p&gt;
1508 &lt;h3 id=&quot;database-support&quot;&gt;Database Support&lt;/h3&gt;
1509 &lt;p&gt;Modern database development is a complex task with many supporting systems and workflows. That&amp;rsquo;s why JetBrains, the company behind PyCharm, developed a standalone IDE called &lt;a href=&quot;https://www.jetbrains.com/datagrip/&quot;&gt;DataGrip&lt;/a&gt; for that. It&amp;rsquo;s a separate product from PyCharm with a separate license. &lt;/p&gt;
1510 &lt;p&gt;Luckily, PyCharm supports all the features that are available in DataGrip through a plugin called &lt;em&gt;Database tools and SQL&lt;/em&gt;, which is enabled by default. With the help of it, you can query, create and manage databases whether they&amp;rsquo;re working locally, on a server, or in the cloud. The plugin supports MySQL, PostgreSQL, Microsoft SQL Server, SQLite, MariaDB, Oracle, Apache Cassandra, and others. For more information on what you can do with this plugin, check out &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/relational-databases.html&quot;&gt;the comprehensive documentation on the database support&lt;/a&gt;.&lt;/p&gt;
1511 &lt;h3 id=&quot;thread-concurrency-visualization&quot;&gt;Thread Concurrency Visualization&lt;/h3&gt;
1512 &lt;p&gt;&lt;a href=&quot;https://channels.readthedocs.io/en/latest/&quot;&gt;&lt;code&gt;Django Channels&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://realpython.com/async-io-python/&quot;&gt;&lt;code&gt;asyncio&lt;/code&gt;&lt;/a&gt;, and the recent frameworks like &lt;a href=&quot;https://www.starlette.io/&quot;&gt;&lt;code&gt;Starlette&lt;/code&gt;&lt;/a&gt; are examples of a growing trend in asynchronous Python programming. While it&amp;rsquo;s true that asynchronous programs do bring a lot of benefits to the table, it&amp;rsquo;s also notoriously hard to write and debug them. In such cases, &lt;em&gt;Thread Concurrency Visualization&lt;/em&gt; can be just what the doctor ordered because it helps you take full control over your multi-threaded applications and optimize them.&lt;/p&gt;
1513 &lt;p&gt;Check out &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/thread-concurrency-visualization.html&quot;&gt;the comprehensive documentation of this feature&lt;/a&gt; for more details.&lt;/p&gt;
1514 &lt;h3 id=&quot;profiler&quot;&gt;Profiler&lt;/h3&gt;
1515 &lt;p&gt;Speaking of optimization, profiling is another technique that you can use to optimize your code. With its help, you can see which parts of your code are taking most of the execution time. A profiler runs in the following order of priority: &lt;/p&gt;
1516 &lt;ol&gt;
1517 &lt;li&gt;&lt;a href=&quot;https://vmprof.readthedocs.io/en/latest/&quot;&gt;&lt;code&gt;vmprof&lt;/code&gt;&lt;/a&gt; &lt;/li&gt;
1518 &lt;li&gt;&lt;a href=&quot;https://github.com/sumerc/yappi&quot;&gt;&lt;code&gt;yappi&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
1519 &lt;li&gt;&lt;a href=&quot;https://docs.python.org/3/library/profile.html&quot;&gt;&lt;code&gt;cProfile&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
1520 &lt;/ol&gt;
1521 &lt;p&gt;If you don&amp;rsquo;t have &lt;code&gt;vmprof&lt;/code&gt; or &lt;code&gt;yappi&lt;/code&gt; installed, then it&amp;rsquo;ll fall back to the standard &lt;code&gt;cProfile&lt;/code&gt;. It&amp;rsquo;s &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/profiler.html&quot;&gt;well-documented&lt;/a&gt;, so I won&amp;rsquo;t rehash it here. &lt;/p&gt;
1522 &lt;h3 id=&quot;scientific-mode&quot;&gt;Scientific Mode&lt;/h3&gt;
1523 &lt;p&gt;Python is not only a language for general and web programming. It also emerged as the best tool for data science and machine learning over these last years thanks to libraries and tools like &lt;a href=&quot;http://www.numpy.org/&quot;&gt;NumPy&lt;/a&gt;, &lt;a href=&quot;https://www.scipy.org/&quot;&gt;SciPy&lt;/a&gt;, &lt;a href=&quot;https://scikit-learn.org/&quot;&gt;scikit-learn&lt;/a&gt;, &lt;a href=&quot;https://matplotlib.org/&quot;&gt;Matplotlib&lt;/a&gt;, &lt;a href=&quot;https://jupyter.org/&quot;&gt;Jupyter&lt;/a&gt;, and more. With such powerful libraries available, you need a powerful IDE to support all the functions such as graphing and analyzing those libraries have. PyCharm provides everything you need as &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/matplotlib-support.html&quot;&gt;thoroughly documented here&lt;/a&gt;.  &lt;/p&gt;
1524 &lt;h3 id=&quot;remote-development&quot;&gt;Remote Development&lt;/h3&gt;
1525 &lt;p&gt;One common cause of bugs in many applications is that development and production environments differ. Although, in most cases, it&amp;rsquo;s not possible to provide an exact copy of the production environment for development, pursuing it is a worthy goal.&lt;/p&gt;
1526 &lt;p&gt;With PyCharm, you can debug your application using an interpreter that is located on the other computer, such as a Linux VM. As a result, you can have the same interpreter as your production environment to fix and avoid many bugs resulting from the difference between development and production environments. Make sure to check out the &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html&quot;&gt;official documentation&lt;/a&gt; to learn more.&lt;/p&gt;
1527 &lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
1528 &lt;p&gt;PyCharm is one of best, if not the best, full-featured, dedicated, and versatile IDEs for Python development. It offers a ton of benefits, saving you a lot of time by helping you with routine tasks. Now you know how to be productive with it!&lt;/p&gt;
1529 &lt;p&gt;In this article, you learned about a lot, including:&lt;/p&gt;
1530 &lt;ul&gt;
1531 &lt;li&gt;Installing PyCharm&lt;/li&gt;
1532 &lt;li&gt;Writing code in PyCharm&lt;/li&gt;
1533 &lt;li&gt;Running your code in PyCharm&lt;/li&gt;
1534 &lt;li&gt;Debugging and testing your code in PyCharm&lt;/li&gt;
1535 &lt;li&gt;Editing an existing project in PyCharm&lt;/li&gt;
1536 &lt;li&gt;Searching and navigating in PyCharm&lt;/li&gt;
1537 &lt;li&gt;Using Version Control in PyCharm&lt;/li&gt;
1538 &lt;li&gt;Using Plugins and External Tools in PyCharm&lt;/li&gt;
1539 &lt;li&gt;Using PyCharm Professional features, such as Django support and Scientific mode&lt;/li&gt;
1540 &lt;/ul&gt;
1541 &lt;p&gt;If there&amp;rsquo;s anything you&amp;rsquo;d like to ask or share, please reach out in the comments below. There&amp;rsquo;s also a lot more information at the &lt;a href=&quot;https://www.jetbrains.com/pycharm/documentation/&quot;&gt;PyCharm website&lt;/a&gt; for you to explore.&lt;/p&gt;
1542 &lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Clone Repo:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/alcazar-web-framework/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-alcazar-web-framework&quot; data-focus=&quot;false&quot;&gt;Click here to clone the repo you&#39;ll use&lt;/a&gt; to explore the project-focused features of PyCharm in this tutorial.&lt;/p&gt;&lt;/div&gt;
1543         &lt;hr /&gt;
1544         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
1545       </content>
1546     </entry>
1547   
1548     <entry>
1549       <title>How to Use Python Lambda Functions</title>
1550       <id>https://realpython.com/courses/python-lambda-functions/</id>
1551       <link href="https://realpython.com/courses/python-lambda-functions/"/>
1552       <updated>2019-08-27T14:00:00+00:00</updated>
1553       <summary>In this step-by-step course, you&#39;ll learn about Python lambda functions. You&#39;ll see how they compare with regular functions and how you can use them in accordance with best practices.</summary>
1554       <content type="html">
1555         &lt;p&gt;Python and other languages like Java, C#, and even C++ have had lambda functions added to their syntax, whereas languages like LISP or the ML family of languages, Haskell, OCaml, and F#, use lambdas as a core concept. Python lambdas are little, anonymous functions, subject to a more restrictive but more concise syntax than regular Python functions.&lt;/p&gt;
1556 &lt;p&gt;&lt;strong&gt;By the end of this course, you&amp;rsquo;ll know:&lt;/strong&gt;&lt;/p&gt;
1557 &lt;ul&gt;
1558 &lt;li&gt;How Python lambdas came to be &lt;/li&gt;
1559 &lt;li&gt;How lambdas compare with regular function objects&lt;/li&gt;
1560 &lt;li&gt;How to write lambda functions&lt;/li&gt;
1561 &lt;li&gt;Which functions in the Python standard library leverage lambdas&lt;/li&gt;
1562 &lt;li&gt;When to use or avoid Python lambda functions&lt;/li&gt;
1563 &lt;/ul&gt;
1564 &lt;p&gt;This course is mainly for intermediate to experienced Python programmers, but it is accessible to any curious minds with interest in programming. All the examples included in this tutorial have been tested with Python 3.7.&lt;/p&gt;
1565         &lt;hr /&gt;
1566         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
1567       </content>
1568     </entry>
1569   
1570     <entry>
1571       <title>A Guide to Excel Spreadsheets in Python With openpyxl</title>
1572       <id>https://realpython.com/openpyxl-excel-spreadsheets-python/</id>
1573       <link href="https://realpython.com/openpyxl-excel-spreadsheets-python/"/>
1574       <updated>2019-08-26T14:00:00+00:00</updated>
1575       <summary>In this step-by-step tutorial, you&#39;ll learn how to handle spreadsheets in Python using the openpyxl package. You&#39;ll learn how to manipulate Excel spreadsheets, extract information from spreadsheets, create simple or more complex spreadsheets, including adding styles, charts, and so on.</summary>
1576       <content type="html">
1577         &lt;p&gt;Excel spreadsheets are one of those things you might have to deal with at some point. Either it&amp;rsquo;s because your boss loves them or because marketing needs them, you might have to learn how to work with spreadsheets, and that&amp;rsquo;s when knowing &lt;code&gt;openpyxl&lt;/code&gt; comes in handy!&lt;/p&gt;
1578 &lt;p&gt;Spreadsheets are a very intuitive and user-friendly way to manipulate large datasets without any prior technical background. That&amp;rsquo;s why they&amp;rsquo;re still so commonly used today.&lt;/p&gt;
1579 &lt;p&gt;&lt;strong&gt;In this article, you&amp;rsquo;ll learn how to use openpyxl to:&lt;/strong&gt;&lt;/p&gt;
1580 &lt;ul&gt;
1581 &lt;li&gt;Manipulate Excel spreadsheets with confidence&lt;/li&gt;
1582 &lt;li&gt;Extract information from spreadsheets&lt;/li&gt;
1583 &lt;li&gt;Create simple or more complex spreadsheets, including adding styles, charts, and so on&lt;/li&gt;
1584 &lt;/ul&gt;
1585 &lt;p&gt;This article is written for intermediate developers who have a pretty good knowledge of Python data structures, such as &lt;a href=&quot;https://realpython.com/python-dicts/&quot;&gt;dicts&lt;/a&gt; and &lt;a href=&quot;https://realpython.com/python-lists-tuples/&quot;&gt;lists&lt;/a&gt;, but also feel comfortable around &lt;a href=&quot;https://realpython.com/python3-object-oriented-programming/&quot;&gt;OOP&lt;/a&gt; and more intermediate level topics.&lt;/p&gt;
1586 &lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Download Dataset:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/openpyxl-sample-dataset/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-openpyxl-sample-dataset&quot; data-focus=&quot;false&quot;&gt;Click here to download the dataset for the openpyxl exercise you&#39;ll be following in this tutorial.&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;
1587 
1588 &lt;h2 id=&quot;before-you-begin&quot;&gt;Before You Begin&lt;/h2&gt;
1589 &lt;p&gt;If you ever get asked to extract some data from a database or log file into an Excel spreadsheet, or if you often have to convert an Excel spreadsheet into some more usable programmatic form, then this tutorial is perfect for you. Let&amp;rsquo;s jump into the &lt;code&gt;openpyxl&lt;/code&gt; caravan!&lt;/p&gt;
1590 &lt;h3 id=&quot;practical-use-cases&quot;&gt;Practical Use Cases&lt;/h3&gt;
1591 &lt;p&gt;First things first, when would you need to use a package like &lt;code&gt;openpyxl&lt;/code&gt; in a real-world scenario? You&amp;rsquo;ll see a few examples below, but really, there are hundreds of possible scenarios where this knowledge could come in handy.&lt;/p&gt;
1592 &lt;h4 id=&quot;importing-new-products-into-a-database&quot;&gt;Importing New Products Into a Database&lt;/h4&gt;
1593 &lt;p&gt;You are responsible for tech in an online store company, and your boss doesn&amp;rsquo;t want to pay for a cool and expensive CMS system.&lt;/p&gt;
1594 &lt;p&gt;Every time they want to add new products to the online store, they come to you with an Excel spreadsheet with a few hundred rows and, for each of them, you have the product name, description, price, and so forth.&lt;/p&gt;
1595 &lt;p&gt;Now, to import the data, you&amp;rsquo;ll have to iterate over each spreadsheet row and add each product to the online store.&lt;/p&gt;
1596 &lt;h4 id=&quot;exporting-database-data-into-a-spreadsheet&quot;&gt;Exporting Database Data Into a Spreadsheet&lt;/h4&gt;
1597 &lt;p&gt;Say you have a Database table where you record all your users&amp;rsquo; information, including name, phone number, email address, and so forth.&lt;/p&gt;
1598 &lt;p&gt;Now, the Marketing team wants to contact all users to give them some discounted offer or promotion. However, they don&amp;rsquo;t have access to the Database, or they don&amp;rsquo;t know how to use SQL to extract that information easily.&lt;/p&gt;
1599 &lt;p&gt;What can you do to help? Well, you can make a quick script using &lt;code&gt;openpyxl&lt;/code&gt; that iterates over every single User record and puts all the essential information into an Excel spreadsheet.&lt;/p&gt;
1600 &lt;p&gt;That&amp;rsquo;s gonna earn you an extra slice of cake at your company&amp;rsquo;s next birthday party!&lt;/p&gt;
1601 &lt;h4 id=&quot;appending-information-to-an-existing-spreadsheet&quot;&gt;Appending Information to an Existing Spreadsheet&lt;/h4&gt;
1602 &lt;p&gt;You may also have to open a spreadsheet, read the information in it and, according to some business logic, append more data to it.&lt;/p&gt;
1603 &lt;p&gt;For example, using the online store scenario again, say you get an Excel spreadsheet with a list of users and you need to append to each row the total amount they&amp;rsquo;ve spent in your store.&lt;/p&gt;
1604 &lt;p&gt;This data is in the Database and, in order to do this, you have to read the spreadsheet, iterate through each row, fetch the total amount spent from the Database and then write back to the spreadsheet.&lt;/p&gt;
1605 &lt;p&gt;Not a problem for &lt;code&gt;openpyxl&lt;/code&gt;!&lt;/p&gt;
1606 &lt;h3 id=&quot;learning-some-basic-excel-terminology&quot;&gt;Learning Some Basic Excel Terminology&lt;/h3&gt;
1607 &lt;p&gt;Here&amp;rsquo;s a quick list of basic terms you&amp;rsquo;ll see when you&amp;rsquo;re working with Excel spreadsheets:&lt;/p&gt;
1608 &lt;div class=&quot;table-responsive&quot;&gt;
1609 &lt;table class=&quot;table table-hover&quot;&gt;
1610 &lt;thead&gt;
1611 &lt;tr&gt;
1612 &lt;th&gt;Term&lt;/th&gt;
1613 &lt;th&gt;Explanation&lt;/th&gt;
1614 &lt;/tr&gt;
1615 &lt;/thead&gt;
1616 &lt;tbody&gt;
1617 &lt;tr&gt;
1618 &lt;td&gt;Spreadsheet or Workbook&lt;/td&gt;
1619 &lt;td&gt;A &lt;strong&gt;Spreadsheet&lt;/strong&gt; is the main file you are creating or working with.&lt;/td&gt;
1620 &lt;/tr&gt;
1621 &lt;tr&gt;
1622 &lt;td&gt;Worksheet or Sheet&lt;/td&gt;
1623 &lt;td&gt;A &lt;strong&gt;Sheet&lt;/strong&gt; is used to split different kinds of content within the same spreadsheet. A &lt;strong&gt;Spreadsheet&lt;/strong&gt; can have one or more &lt;strong&gt;Sheets&lt;/strong&gt;.&lt;/td&gt;
1624 &lt;/tr&gt;
1625 &lt;tr&gt;
1626 &lt;td&gt;Column&lt;/td&gt;
1627 &lt;td&gt;A &lt;strong&gt;Column&lt;/strong&gt; is a vertical line, and it&amp;rsquo;s represented by an uppercase letter: &lt;em&gt;A&lt;/em&gt;.&lt;/td&gt;
1628 &lt;/tr&gt;
1629 &lt;tr&gt;
1630 &lt;td&gt;Row&lt;/td&gt;
1631 &lt;td&gt;A &lt;strong&gt;Row&lt;/strong&gt; is a horizontal line, and it&amp;rsquo;s represented by a number: &lt;em&gt;1&lt;/em&gt;.&lt;/td&gt;
1632 &lt;/tr&gt;
1633 &lt;tr&gt;
1634 &lt;td&gt;Cell&lt;/td&gt;
1635 &lt;td&gt;A &lt;strong&gt;Cell&lt;/strong&gt; is a combination of &lt;strong&gt;Column&lt;/strong&gt; and &lt;strong&gt;Row&lt;/strong&gt;, represented by both an uppercase letter and a number: &lt;em&gt;A1&lt;/em&gt;.&lt;/td&gt;
1636 &lt;/tr&gt;
1637 &lt;/tbody&gt;
1638 &lt;/table&gt;
1639 &lt;/div&gt;
1640 &lt;h3 id=&quot;getting-started-with-openpyxl&quot;&gt;Getting Started With openpyxl&lt;/h3&gt;
1641 &lt;p&gt;Now that you&amp;rsquo;re aware of the benefits of a tool like &lt;code&gt;openpyxl&lt;/code&gt;, let&amp;rsquo;s get down to it and start by installing the package. For this tutorial, you should use Python 3.7 and openpyxl 2.6.2. To install the package, you can do the following:&lt;/p&gt;
1642 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pip install openpyxl
1643 &lt;/pre&gt;&lt;/div&gt;
1644 
1645 &lt;p&gt;After you install the package, you should be able to create a super simple spreadsheet with the following code:&lt;/p&gt;
1646 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;
1647 
1648 &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
1649 &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
1650 
1651 &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;
1652 &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;B1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;world!&amp;quot;&lt;/span&gt;
1653 
1654 &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;hello_world.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1655 &lt;/pre&gt;&lt;/div&gt;
1656 
1657 &lt;p&gt;The code above should create a file called &lt;code&gt;hello_world.xlsx&lt;/code&gt; in the folder you are using to run the code. If you open that file with Excel you should see something like this:&lt;/p&gt;
1658 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_16.54.45.e646867e4dbb.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_16.54.45.e646867e4dbb.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_16.54.45.e646867e4dbb.png&amp;amp;w=540&amp;amp;sig=4c3acdcf35f528b6ed0cf6e299c2575781934414 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_16.54.45.e646867e4dbb.png&amp;amp;w=1080&amp;amp;sig=328d4ff12cec767d684f5b7666380d9f23a2a548 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_16.54.45.e646867e4dbb.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;A Simple Hello World Spreadsheet&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
1659 &lt;p&gt;&lt;em&gt;Woohoo&lt;/em&gt;, your first spreadsheet created!&lt;/p&gt;
1660 &lt;h2 id=&quot;reading-excel-spreadsheets-with-openpyxl&quot;&gt;Reading Excel Spreadsheets With openpyxl&lt;/h2&gt;
1661 &lt;p&gt;Let&amp;rsquo;s start with the most essential thing one can do with a spreadsheet: read it.&lt;/p&gt;
1662 &lt;p&gt;You&amp;rsquo;ll go from a straightforward approach to reading a spreadsheet to more complex examples where you read the data and convert it into more useful Python structures.&lt;/p&gt;
1663 &lt;h3 id=&quot;dataset-for-this-tutorial&quot;&gt;Dataset for This Tutorial&lt;/h3&gt;
1664 &lt;p&gt;Before you dive deep into some code examples, you should &lt;strong&gt;download this sample dataset&lt;/strong&gt; and store it somewhere as &lt;code&gt;sample.xlsx&lt;/code&gt;:&lt;/p&gt;
1665 &lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Download Dataset:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/openpyxl-sample-dataset/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-openpyxl-sample-dataset&quot; data-focus=&quot;false&quot;&gt;Click here to download the dataset for the openpyxl exercise you&#39;ll be following in this tutorial.&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;
1666 
1667 &lt;p&gt;This is one of the datasets you&amp;rsquo;ll be using throughout this tutorial, and it&amp;rsquo;s a spreadsheet with a sample of real data from Amazon&amp;rsquo;s online product reviews. This dataset is only a tiny fraction of what Amazon &lt;a href=&quot;https://registry.opendata.aws/amazon-reviews/&quot;&gt;provides&lt;/a&gt;, but for testing purposes, it&amp;rsquo;s more than enough.&lt;/p&gt;
1668 &lt;h3 id=&quot;a-simple-approach-to-reading-an-excel-spreadsheet&quot;&gt;A Simple Approach to Reading an Excel Spreadsheet&lt;/h3&gt;
1669 &lt;p&gt;Finally, let&amp;rsquo;s start reading some spreadsheets! To begin with, open our sample spreadsheet:&lt;/p&gt;
1670 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;
1671 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1672 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
1673 &lt;span class=&quot;go&quot;&gt;[&amp;#39;Sheet 1&amp;#39;]&lt;/span&gt;
1674 
1675 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
1676 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;
1677 &lt;span class=&quot;go&quot;&gt;&amp;lt;Worksheet &amp;quot;Sheet 1&amp;quot;&amp;gt;&lt;/span&gt;
1678 
1679 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;
1680 &lt;span class=&quot;go&quot;&gt;&amp;#39;Sheet 1&amp;#39;&lt;/span&gt;
1681 &lt;/pre&gt;&lt;/div&gt;
1682 
1683 &lt;p&gt;In the code above, you first open the spreadsheet &lt;code&gt;sample.xlsx&lt;/code&gt; using &lt;code&gt;load_workbook()&lt;/code&gt;, and then you can use &lt;code&gt;workbook.sheetnames&lt;/code&gt; to see all the sheets you have available to work with. After that,  &lt;code&gt;workbook.active&lt;/code&gt; selects the first available sheet and, in this case, you can see that it selects &lt;strong&gt;Sheet 1&lt;/strong&gt; automatically. Using these methods is the default way of opening a spreadsheet, and you&amp;rsquo;ll see it many times during this tutorial.&lt;/p&gt;
1684 &lt;p&gt;Now, after opening a spreadsheet, you can easily retrieve data from it like this:&lt;/p&gt;
1685 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
1686 &lt;span class=&quot;go&quot;&gt;&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A1&amp;gt;&lt;/span&gt;
1687 
1688 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
1689 &lt;span class=&quot;go&quot;&gt;&amp;#39;marketplace&amp;#39;&lt;/span&gt;
1690 
1691 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;F10&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
1692 &lt;span class=&quot;go&quot;&gt;&amp;quot;G-Shock Men&amp;#39;s Grey Sport Watch&amp;quot;&lt;/span&gt;
1693 &lt;/pre&gt;&lt;/div&gt;
1694 
1695 &lt;p&gt;To return the actual value of a cell, you need to do &lt;code&gt;.value&lt;/code&gt;. Otherwise, you&amp;rsquo;ll get the main &lt;code&gt;Cell&lt;/code&gt; object. You can also use the method &lt;code&gt;.cell()&lt;/code&gt; to retrieve a cell using index notation. Remember to add &lt;code&gt;.value&lt;/code&gt; to get the actual value and not a &lt;code&gt;Cell&lt;/code&gt; object:&lt;/p&gt;
1696 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;column&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1697 &lt;span class=&quot;go&quot;&gt;&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.F10&amp;gt;&lt;/span&gt;
1698 
1699 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;column&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
1700 &lt;span class=&quot;go&quot;&gt;&amp;quot;G-Shock Men&amp;#39;s Grey Sport Watch&amp;quot;&lt;/span&gt;
1701 &lt;/pre&gt;&lt;/div&gt;
1702 
1703 &lt;p&gt;You can see that the results returned are the same, no matter which way you decide to go with. However, in this tutorial, you&amp;rsquo;ll be mostly using the first approach: &lt;code&gt;[&quot;A1&quot;]&lt;/code&gt;.&lt;/p&gt;
1704 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
1705 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Even though in Python you&amp;rsquo;re used to a zero-indexed notation, with spreadsheets you&amp;rsquo;ll always use a one-indexed notation where the first row or column always has index &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;
1706 &lt;/div&gt;
1707 &lt;p&gt;The above shows you the quickest way to open a spreadsheet. However, you can pass additional parameters to change the way a spreadsheet is loaded.&lt;/p&gt;
1708 &lt;h4 id=&quot;additional-reading-options&quot;&gt;Additional Reading Options&lt;/h4&gt;
1709 &lt;p&gt;There are a few arguments you can pass to &lt;code&gt;load_workbook()&lt;/code&gt; that change the way a spreadsheet is loaded. The most important ones are the following two Booleans:&lt;/p&gt;
1710 &lt;ol&gt;
1711 &lt;li&gt;&lt;strong&gt;read_only&lt;/strong&gt; loads a spreadsheet in read-only mode allowing you to open very large Excel files.&lt;/li&gt;
1712 &lt;li&gt;&lt;strong&gt;data_only&lt;/strong&gt; ignores loading formulas and instead loads only the resulting values.&lt;/li&gt;
1713 &lt;/ol&gt;
1714 &lt;h3 id=&quot;importing-data-from-a-spreadsheet&quot;&gt;Importing Data From a Spreadsheet&lt;/h3&gt;
1715 &lt;p&gt;Now that you&amp;rsquo;ve learned the basics about loading a spreadsheet, it&amp;rsquo;s about time you get to the fun part: &lt;strong&gt;the iteration and actual usage of the values within the spreadsheet&lt;/strong&gt;.&lt;/p&gt;
1716 &lt;p&gt;This section is where you&amp;rsquo;ll learn all the different ways you can iterate through the data, but also how to convert that data into something usable and, more importantly, how to do it in a Pythonic way.&lt;/p&gt;
1717 &lt;h4 id=&quot;iterating-through-the-data&quot;&gt;Iterating Through the Data&lt;/h4&gt;
1718 &lt;p&gt;There are a few different ways you can iterate through the data depending on your needs.&lt;/p&gt;
1719 &lt;p&gt;You can slice the data with a combination of columns and rows:&lt;/p&gt;
1720 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A1:C2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
1721 &lt;span class=&quot;go&quot;&gt;((&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A1&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B1&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.C1&amp;gt;),&lt;/span&gt;
1722 &lt;span class=&quot;go&quot;&gt; (&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A2&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B2&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.C2&amp;gt;))&lt;/span&gt;
1723 &lt;/pre&gt;&lt;/div&gt;
1724 
1725 &lt;p&gt;You can get ranges of rows or columns:&lt;/p&gt;
1726 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Get all cells from column A&lt;/span&gt;
1727 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
1728 &lt;span class=&quot;go&quot;&gt;(&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A1&amp;gt;,&lt;/span&gt;
1729 &lt;span class=&quot;go&quot;&gt; &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A2&amp;gt;,&lt;/span&gt;
1730 &lt;span class=&quot;go&quot;&gt; ...&lt;/span&gt;
1731 &lt;span class=&quot;go&quot;&gt; &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A99&amp;gt;,&lt;/span&gt;
1732 &lt;span class=&quot;go&quot;&gt; &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A100&amp;gt;)&lt;/span&gt;
1733 
1734 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Get all cells for a range of columns&lt;/span&gt;
1735 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A:B&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
1736 &lt;span class=&quot;go&quot;&gt;((&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A1&amp;gt;,&lt;/span&gt;
1737 &lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A2&amp;gt;,&lt;/span&gt;
1738 &lt;span class=&quot;go&quot;&gt;  ...&lt;/span&gt;
1739 &lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A99&amp;gt;,&lt;/span&gt;
1740 &lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A100&amp;gt;),&lt;/span&gt;
1741 &lt;span class=&quot;go&quot;&gt; (&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B1&amp;gt;,&lt;/span&gt;
1742 &lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B2&amp;gt;,&lt;/span&gt;
1743 &lt;span class=&quot;go&quot;&gt;  ...&lt;/span&gt;
1744 &lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B99&amp;gt;,&lt;/span&gt;
1745 &lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B100&amp;gt;))&lt;/span&gt;
1746 
1747 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Get all cells from row 5&lt;/span&gt;
1748 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
1749 &lt;span class=&quot;go&quot;&gt;(&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A5&amp;gt;,&lt;/span&gt;
1750 &lt;span class=&quot;go&quot;&gt; &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B5&amp;gt;,&lt;/span&gt;
1751 &lt;span class=&quot;go&quot;&gt; ...&lt;/span&gt;
1752 &lt;span class=&quot;go&quot;&gt; &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.N5&amp;gt;,&lt;/span&gt;
1753 &lt;span class=&quot;go&quot;&gt; &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.O5&amp;gt;)&lt;/span&gt;
1754 
1755 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Get all cells for a range of rows&lt;/span&gt;
1756 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
1757 &lt;span class=&quot;go&quot;&gt;((&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A5&amp;gt;,&lt;/span&gt;
1758 &lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B5&amp;gt;,&lt;/span&gt;
1759 &lt;span class=&quot;go&quot;&gt;  ...&lt;/span&gt;
1760 &lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.N5&amp;gt;,&lt;/span&gt;
1761 &lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.O5&amp;gt;),&lt;/span&gt;
1762 &lt;span class=&quot;go&quot;&gt; (&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A6&amp;gt;,&lt;/span&gt;
1763 &lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B6&amp;gt;,&lt;/span&gt;
1764 &lt;span class=&quot;go&quot;&gt;  ...&lt;/span&gt;
1765 &lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.N6&amp;gt;,&lt;/span&gt;
1766 &lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.O6&amp;gt;))&lt;/span&gt;
1767 &lt;/pre&gt;&lt;/div&gt;
1768 
1769 &lt;p&gt;You&amp;rsquo;ll notice that all of the above examples return a &lt;code&gt;tuple&lt;/code&gt;. If you want to refresh your memory on how to handle &lt;code&gt;tuples&lt;/code&gt; in Python, check out the article on &lt;a href=&quot;https://realpython.com/python-lists-tuples/#python-tuples&quot;&gt;Lists and Tuples in Python&lt;/a&gt;.&lt;/p&gt;
1770 &lt;p&gt;There are also multiple ways of using normal Python &lt;a href=&quot;https://realpython.com/introduction-to-python-generators/&quot;&gt;generators&lt;/a&gt; to go through the data. The main methods you can use to achieve this are:&lt;/p&gt;
1771 &lt;ul&gt;
1772 &lt;li&gt;&lt;code&gt;.iter_rows()&lt;/code&gt;&lt;/li&gt;
1773 &lt;li&gt;&lt;code&gt;.iter_cols()&lt;/code&gt;&lt;/li&gt;
1774 &lt;/ul&gt;
1775 &lt;p&gt;Both methods can receive the following arguments:&lt;/p&gt;
1776 &lt;ul&gt;
1777 &lt;li&gt;&lt;code&gt;min_row&lt;/code&gt;&lt;/li&gt;
1778 &lt;li&gt;&lt;code&gt;max_row&lt;/code&gt;&lt;/li&gt;
1779 &lt;li&gt;&lt;code&gt;min_col&lt;/code&gt;&lt;/li&gt;
1780 &lt;li&gt;&lt;code&gt;max_col&lt;/code&gt;&lt;/li&gt;
1781 &lt;/ul&gt;
1782 &lt;p&gt;These arguments are used to set boundaries for the iteration:&lt;/p&gt;
1783 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1784 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                           &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1785 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                           &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1786 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                           &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1787 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1788 &lt;span class=&quot;go&quot;&gt;(&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A1&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B1&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.C1&amp;gt;)&lt;/span&gt;
1789 &lt;span class=&quot;go&quot;&gt;(&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A2&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B2&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.C2&amp;gt;)&lt;/span&gt;
1790 
1791 
1792 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;column&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1793 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                              &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1794 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                              &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1795 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                              &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1796 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1797 &lt;span class=&quot;go&quot;&gt;(&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A1&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A2&amp;gt;)&lt;/span&gt;
1798 &lt;span class=&quot;go&quot;&gt;(&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B1&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B2&amp;gt;)&lt;/span&gt;
1799 &lt;span class=&quot;go&quot;&gt;(&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.C1&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.C2&amp;gt;)&lt;/span&gt;
1800 &lt;/pre&gt;&lt;/div&gt;
1801 
1802 &lt;p&gt;You&amp;rsquo;ll notice that in the first example, when iterating through the rows using &lt;code&gt;.iter_rows()&lt;/code&gt;, you get one &lt;code&gt;tuple&lt;/code&gt; element per row selected. While when using &lt;code&gt;.iter_cols()&lt;/code&gt; and iterating through columns, you&amp;rsquo;ll get one &lt;code&gt;tuple&lt;/code&gt; per column instead.&lt;/p&gt;
1803 &lt;p&gt;One additional argument you can pass to both methods is the Boolean &lt;code&gt;values_only&lt;/code&gt;. When it&amp;rsquo;s set to &lt;code&gt;True&lt;/code&gt;, the values of the cell are returned, instead of the &lt;code&gt;Cell&lt;/code&gt; object:&lt;/p&gt;
1804 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1805 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1806 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1807 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1808 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;values_only&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1809 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1810 &lt;span class=&quot;go&quot;&gt;(&amp;#39;marketplace&amp;#39;, &amp;#39;customer_id&amp;#39;, &amp;#39;review_id&amp;#39;)&lt;/span&gt;
1811 &lt;span class=&quot;go&quot;&gt;(&amp;#39;US&amp;#39;, 3653882, &amp;#39;R3O9SGZBVQBV76&amp;#39;)&lt;/span&gt;
1812 &lt;/pre&gt;&lt;/div&gt;
1813 
1814 &lt;p&gt;If you want to iterate through the whole dataset, then you can also use the attributes &lt;code&gt;.rows&lt;/code&gt; or &lt;code&gt;.columns&lt;/code&gt; directly, which are shortcuts to using &lt;code&gt;.iter_rows()&lt;/code&gt; and &lt;code&gt;.iter_cols()&lt;/code&gt; without any arguments:&lt;/p&gt;
1815 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
1816 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1817 &lt;span class=&quot;go&quot;&gt;(&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A1&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B1&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.C1&amp;gt;&lt;/span&gt;
1818 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
1819 &lt;span class=&quot;go&quot;&gt;&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.M100&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.N100&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.O100&amp;gt;)&lt;/span&gt;
1820 &lt;/pre&gt;&lt;/div&gt;
1821 
1822 &lt;p&gt;These shortcuts are very useful when you&amp;rsquo;re iterating through the whole dataset.&lt;/p&gt;
1823 &lt;h4 id=&quot;manipulate-data-using-pythons-default-data-structures&quot;&gt;Manipulate Data Using Python&amp;rsquo;s Default Data Structures&lt;/h4&gt;
1824 &lt;p&gt;Now that you know the basics of iterating through the data in a workbook, let&amp;rsquo;s look at smart ways of converting that data into Python structures.&lt;/p&gt;
1825 &lt;p&gt;As you saw earlier, the result from all iterations comes in the form of &lt;code&gt;tuples&lt;/code&gt;. However, since a &lt;code&gt;tuple&lt;/code&gt; is nothing more than an immutable &lt;code&gt;list&lt;/code&gt;, you can easily access its data and transform it into other structures.&lt;/p&gt;
1826 &lt;p&gt;For example, say you want to extract product information from the &lt;code&gt;sample.xlsx&lt;/code&gt; spreadsheet and into a dictionary where each key is a product ID.&lt;/p&gt;
1827 &lt;p&gt;A straightforward way to do this is to iterate over all the rows, pick the columns you know are related to product information, and then store that in a dictionary. Let&amp;rsquo;s code this out!&lt;/p&gt;
1828 &lt;p&gt;First of all, have a look at the headers and see what information you care most about:&lt;/p&gt;
1829 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1830 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1831 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;values_only&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1832 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1833 &lt;span class=&quot;go&quot;&gt;(&amp;#39;marketplace&amp;#39;, &amp;#39;customer_id&amp;#39;, &amp;#39;review_id&amp;#39;, &amp;#39;product_id&amp;#39;, ...)&lt;/span&gt;
1834 &lt;/pre&gt;&lt;/div&gt;
1835 
1836 &lt;p&gt;This code returns a list of all the column names you have in the spreadsheet. To start, grab the columns with names:&lt;/p&gt;
1837 &lt;ul&gt;
1838 &lt;li&gt;&lt;code&gt;product_id&lt;/code&gt;&lt;/li&gt;
1839 &lt;li&gt;&lt;code&gt;product_parent&lt;/code&gt;&lt;/li&gt;
1840 &lt;li&gt;&lt;code&gt;product_title&lt;/code&gt;&lt;/li&gt;
1841 &lt;li&gt;&lt;code&gt;product_category&lt;/code&gt;&lt;/li&gt;
1842 &lt;/ul&gt;
1843 &lt;p&gt;Lucky for you, the columns you need are all next to each other so you can use the &lt;code&gt;min_column&lt;/code&gt; and &lt;code&gt;max_column&lt;/code&gt; to easily get the data you want:&lt;/p&gt;
1844 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1845 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1846 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1847 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;values_only&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1848 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1849 &lt;span class=&quot;go&quot;&gt;(&amp;#39;B00FALQ1ZC&amp;#39;, 937001370, &amp;#39;Invicta Women\&amp;#39;s 15150 &amp;quot;Angel&amp;quot; 18k Yellow...)&lt;/span&gt;
1850 &lt;span class=&quot;go&quot;&gt;(&amp;#39;B00D3RGO20&amp;#39;, 484010722, &amp;quot;Kenneth Cole New York Women&amp;#39;s KC4944...)&lt;/span&gt;
1851 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
1852 &lt;/pre&gt;&lt;/div&gt;
1853 
1854 &lt;p&gt;Nice! Now that you know how to get all the important product information you need, let&amp;rsquo;s put that data into a dictionary:&lt;/p&gt;
1855 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;json&lt;/span&gt;
1856 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;
1857 
1858 &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1859 &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
1860 
1861 &lt;span class=&quot;n&quot;&gt;products&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
1862 
1863 &lt;span class=&quot;c1&quot;&gt;# Using the values_only because you want to return the cells&amp;#39; values&lt;/span&gt;
1864 &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1865                            &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1866                            &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1867                            &lt;span class=&quot;n&quot;&gt;values_only&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1868     &lt;span class=&quot;n&quot;&gt;product_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
1869     &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
1870         &lt;span class=&quot;s2&quot;&gt;&amp;quot;parent&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
1871         &lt;span class=&quot;s2&quot;&gt;&amp;quot;title&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
1872         &lt;span class=&quot;s2&quot;&gt;&amp;quot;category&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
1873     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
1874     &lt;span class=&quot;n&quot;&gt;products&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;
1875 
1876 &lt;span class=&quot;c1&quot;&gt;# Using json here to be able to format the output for displaying later&lt;/span&gt;
1877 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dumps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;products&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
1878 &lt;/pre&gt;&lt;/div&gt;
1879 
1880 &lt;p&gt;The code above returns a JSON similar to this:&lt;/p&gt;
1881 &lt;div class=&quot;highlight json&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
1882   &lt;span class=&quot;nt&quot;&gt;&amp;quot;B00FALQ1ZC&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
1883     &lt;span class=&quot;nt&quot;&gt;&amp;quot;parent&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;937001370&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1884     &lt;span class=&quot;nt&quot;&gt;&amp;quot;title&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Invicta Women&amp;#39;s 15150 ...&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1885     &lt;span class=&quot;nt&quot;&gt;&amp;quot;category&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Watches&amp;quot;&lt;/span&gt;
1886   &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
1887   &lt;span class=&quot;nt&quot;&gt;&amp;quot;B00D3RGO20&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
1888     &lt;span class=&quot;nt&quot;&gt;&amp;quot;parent&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;484010722&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1889     &lt;span class=&quot;nt&quot;&gt;&amp;quot;title&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Kenneth Cole New York ...&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1890     &lt;span class=&quot;nt&quot;&gt;&amp;quot;category&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Watches&amp;quot;&lt;/span&gt;
1891   &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
1892 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
1893 &lt;/pre&gt;&lt;/div&gt;
1894 
1895 &lt;p&gt;Here you can see that the output is trimmed to 2 products only, but if you run the script as it is, then you should get 98 products.&lt;/p&gt;
1896 &lt;h4 id=&quot;convert-data-into-python-classes&quot;&gt;Convert Data Into Python Classes&lt;/h4&gt;
1897 &lt;p&gt;To finalize the reading section of this tutorial, let&amp;rsquo;s dive into Python classes and see how you could improve on the example above and better structure the data.&lt;/p&gt;
1898 &lt;p&gt;For this, you&amp;rsquo;ll be using the new Python &lt;a href=&quot;https://realpython.com/python-data-classes/&quot;&gt;Data Classes&lt;/a&gt; that are available from Python 3.7. If you&amp;rsquo;re using an older version of Python, then you can use the default &lt;a href=&quot;https://realpython.com/python3-object-oriented-programming/#classes-in-python&quot;&gt;Classes&lt;/a&gt; instead.&lt;/p&gt;
1899 &lt;p&gt;So, first things first, let&amp;rsquo;s look at the data you have and decide what you want to store and how you want to store it.&lt;/p&gt;
1900 &lt;p&gt;As you saw right at the start, this data comes from Amazon, and it&amp;rsquo;s a list of product reviews. You can check the &lt;a href=&quot;https://s3.amazonaws.com/amazon-reviews-pds/tsv/index.txt&quot;&gt;list of all the columns and their meaning&lt;/a&gt; on Amazon.&lt;/p&gt;
1901 &lt;p&gt;There are two significant elements you can extract from the data available:&lt;/p&gt;
1902 &lt;ol&gt;
1903 &lt;li&gt;Products&lt;/li&gt;
1904 &lt;li&gt;Reviews&lt;/li&gt;
1905 &lt;/ol&gt;
1906 &lt;p&gt;A &lt;strong&gt;Product&lt;/strong&gt; has:&lt;/p&gt;
1907 &lt;ul&gt;
1908 &lt;li&gt;ID&lt;/li&gt;
1909 &lt;li&gt;Title&lt;/li&gt;
1910 &lt;li&gt;Parent&lt;/li&gt;
1911 &lt;li&gt;Category&lt;/li&gt;
1912 &lt;/ul&gt;
1913 &lt;p&gt;The &lt;strong&gt;Review&lt;/strong&gt; has a few more fields:&lt;/p&gt;
1914 &lt;ul&gt;
1915 &lt;li&gt;ID&lt;/li&gt;
1916 &lt;li&gt;Customer ID&lt;/li&gt;
1917 &lt;li&gt;Stars&lt;/li&gt;
1918 &lt;li&gt;Headline&lt;/li&gt;
1919 &lt;li&gt;Body&lt;/li&gt;
1920 &lt;li&gt;Date&lt;/li&gt;
1921 &lt;/ul&gt;
1922 &lt;p&gt;You can ignore a few of the review fields to make things a bit simpler.&lt;/p&gt;
1923 &lt;p&gt;So, a straightforward implementation of these two classes could be written in a separate file &lt;code&gt;classes.py&lt;/code&gt;:&lt;/p&gt;
1924 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;datetime&lt;/span&gt;
1925 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dataclasses&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataclass&lt;/span&gt;
1926 
1927 &lt;span class=&quot;nd&quot;&gt;@dataclass&lt;/span&gt;
1928 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
1929     &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
1930     &lt;span class=&quot;n&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
1931     &lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
1932     &lt;span class=&quot;n&quot;&gt;category&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
1933 
1934 &lt;span class=&quot;nd&quot;&gt;@dataclass&lt;/span&gt;
1935 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Review&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
1936     &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
1937     &lt;span class=&quot;n&quot;&gt;customer_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
1938     &lt;span class=&quot;n&quot;&gt;stars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;
1939     &lt;span class=&quot;n&quot;&gt;headline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
1940     &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
1941     &lt;span class=&quot;n&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;
1942 &lt;/pre&gt;&lt;/div&gt;
1943 
1944 &lt;p&gt;After defining your data classes, you need to convert the data from the spreadsheet into these new structures.&lt;/p&gt;
1945 &lt;p&gt;Before doing the conversion, it&amp;rsquo;s worth looking at our header again and creating a mapping between columns and the fields you need:&lt;/p&gt;
1946 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1947 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
1948 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;values_only&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1949 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1950 &lt;span class=&quot;go&quot;&gt;(&amp;#39;marketplace&amp;#39;, &amp;#39;customer_id&amp;#39;, &amp;#39;review_id&amp;#39;, &amp;#39;product_id&amp;#39;, ...)&lt;/span&gt;
1951 
1952 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Or an alternative&lt;/span&gt;
1953 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
1954 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1955 &lt;span class=&quot;go&quot;&gt;marketplace&lt;/span&gt;
1956 &lt;span class=&quot;go&quot;&gt;customer_id&lt;/span&gt;
1957 &lt;span class=&quot;go&quot;&gt;review_id&lt;/span&gt;
1958 &lt;span class=&quot;go&quot;&gt;product_id&lt;/span&gt;
1959 &lt;span class=&quot;go&quot;&gt;product_parent&lt;/span&gt;
1960 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
1961 &lt;/pre&gt;&lt;/div&gt;
1962 
1963 &lt;p&gt;Let&amp;rsquo;s create a file &lt;code&gt;mapping.py&lt;/code&gt; where you have a list of all the field names and their column location (zero-indexed) on the spreadsheet:&lt;/p&gt;
1964 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Product fields&lt;/span&gt;
1965 &lt;span class=&quot;n&quot;&gt;PRODUCT_ID&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
1966 &lt;span class=&quot;n&quot;&gt;PRODUCT_PARENT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;
1967 &lt;span class=&quot;n&quot;&gt;PRODUCT_TITLE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;
1968 &lt;span class=&quot;n&quot;&gt;PRODUCT_CATEGORY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;
1969 
1970 &lt;span class=&quot;c1&quot;&gt;# Review fields&lt;/span&gt;
1971 &lt;span class=&quot;n&quot;&gt;REVIEW_ID&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
1972 &lt;span class=&quot;n&quot;&gt;REVIEW_CUSTOMER&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
1973 &lt;span class=&quot;n&quot;&gt;REVIEW_STARS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;
1974 &lt;span class=&quot;n&quot;&gt;REVIEW_HEADLINE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;
1975 &lt;span class=&quot;n&quot;&gt;REVIEW_BODY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;
1976 &lt;span class=&quot;n&quot;&gt;REVIEW_DATE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;14&lt;/span&gt;
1977 &lt;/pre&gt;&lt;/div&gt;
1978 
1979 &lt;p&gt;You don&amp;rsquo;t necessarily have to do the mapping above. It&amp;rsquo;s more for readability when parsing the row data, so you don&amp;rsquo;t end up with a lot of magic numbers lying around.&lt;/p&gt;
1980 &lt;p&gt;Finally, let&amp;rsquo;s look at the code needed to parse the spreadsheet data into a list of product and review objects:&lt;/p&gt;
1981 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;datetime&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;
1982 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;
1983 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;classes&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Review&lt;/span&gt;
1984 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;mapping&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PRODUCT_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PRODUCT_PARENT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PRODUCT_TITLE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; \
1985     &lt;span class=&quot;n&quot;&gt;PRODUCT_CATEGORY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;REVIEW_DATE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;REVIEW_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;REVIEW_CUSTOMER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; \
1986     &lt;span class=&quot;n&quot;&gt;REVIEW_STARS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;REVIEW_HEADLINE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;REVIEW_BODY&lt;/span&gt;
1987 
1988 &lt;span class=&quot;c1&quot;&gt;# Using the read_only method since you&amp;#39;re not gonna be editing the spreadsheet&lt;/span&gt;
1989 &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;read_only&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
1990 &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
1991 
1992 &lt;span class=&quot;n&quot;&gt;products&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
1993 &lt;span class=&quot;n&quot;&gt;reviews&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
1994 
1995 &lt;span class=&quot;c1&quot;&gt;# Using the values_only because you just want to return the cell value&lt;/span&gt;
1996 &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;values_only&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
1997     &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
1998                       &lt;span class=&quot;n&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_PARENT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
1999                       &lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_TITLE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2000                       &lt;span class=&quot;n&quot;&gt;category&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_CATEGORY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
2001     &lt;span class=&quot;n&quot;&gt;products&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2002 
2003     &lt;span class=&quot;c1&quot;&gt;# You need to parse the date from the spreadsheet into a datetime format&lt;/span&gt;
2004     &lt;span class=&quot;n&quot;&gt;spread_date&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;REVIEW_DATE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
2005     &lt;span class=&quot;n&quot;&gt;parsed_date&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strptime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spread_date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;%Y-%m-&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%d&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2006 
2007     &lt;span class=&quot;n&quot;&gt;review&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Review&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;REVIEW_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2008                     &lt;span class=&quot;n&quot;&gt;customer_id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;REVIEW_CUSTOMER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2009                     &lt;span class=&quot;n&quot;&gt;stars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;REVIEW_STARS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2010                     &lt;span class=&quot;n&quot;&gt;headline&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;REVIEW_HEADLINE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2011                     &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;REVIEW_BODY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2012                     &lt;span class=&quot;n&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parsed_date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2013     &lt;span class=&quot;n&quot;&gt;reviews&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;review&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2014 
2015 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;products&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
2016 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reviews&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
2017 &lt;/pre&gt;&lt;/div&gt;
2018 
2019 &lt;p&gt;After you run the code above, you should get some output like this:&lt;/p&gt;
2020 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;B00FALQ1ZC&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;937001370&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2021 &lt;span class=&quot;n&quot;&gt;Review&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;R3O9SGZBVQBV76&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;customer_id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3653882&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2022 &lt;/pre&gt;&lt;/div&gt;
2023 
2024 &lt;p&gt;That&amp;rsquo;s it! Now you should have the data in a very simple and digestible class format, and you can start thinking of storing this in a &lt;a href=&quot;https://realpython.com/tutorials/databases/&quot;&gt;Database&lt;/a&gt; or any other type of data storage you like.&lt;/p&gt;
2025 &lt;p&gt;Using this kind of OOP strategy to parse spreadsheets makes handling the data much simpler later on.&lt;/p&gt;
2026 &lt;h3 id=&quot;appending-new-data&quot;&gt;Appending New Data&lt;/h3&gt;
2027 &lt;p&gt;Before you start creating very complex spreadsheets, have a quick look at an example of how to append data to an existing spreadsheet.&lt;/p&gt;
2028 &lt;p&gt;Go back to the first example spreadsheet you created (&lt;code&gt;hello_world.xlsx&lt;/code&gt;) and try opening it and appending some data to it, like this:&lt;/p&gt;
2029 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;
2030 
2031 &lt;span class=&quot;c1&quot;&gt;# Start by opening the spreadsheet and selecting the main sheet&lt;/span&gt;
2032 &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;hello_world.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2033 &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
2034 
2035 &lt;span class=&quot;c1&quot;&gt;# Write what you want into a specific cell&lt;/span&gt;
2036 &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;C1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;writing ;)&amp;quot;&lt;/span&gt;
2037 
2038 &lt;span class=&quot;c1&quot;&gt;# Save the spreadsheet&lt;/span&gt;
2039 &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;hello_world_append.xlsx&amp;quot;&lt;/span&gt;
2040 &lt;/pre&gt;&lt;/div&gt;
2041 
2042 &lt;p&gt;&lt;em&gt;Et voilร &lt;/em&gt;, if you open the new &lt;code&gt;hello_world_append.xlsx&lt;/code&gt; spreadsheet, you&amp;rsquo;ll see the following change:&lt;/p&gt;
2043 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_17.44.22.e4f18e5abc42.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_17.44.22.e4f18e5abc42.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_17.44.22.e4f18e5abc42.png&amp;amp;w=540&amp;amp;sig=098886279b90048004feb6dcdbe1c66ac3e231ce 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_17.44.22.e4f18e5abc42.png&amp;amp;w=1080&amp;amp;sig=8619e04c109779499f96dcd8aee01c4cf1ed52eb 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_17.44.22.e4f18e5abc42.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Appending Data to a Spreadsheet&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2044 &lt;p&gt;Notice the additional &lt;em&gt;writing ;)&lt;/em&gt; on cell &lt;code&gt;C1&lt;/code&gt;.&lt;/p&gt;
2045 &lt;h2 id=&quot;writing-excel-spreadsheets-with-openpyxl&quot;&gt;Writing Excel Spreadsheets With openpyxl&lt;/h2&gt;
2046 &lt;p&gt;There are a lot of different things you can write to a spreadsheet, from simple text or number values to complex formulas, charts, or even images.&lt;/p&gt;
2047 &lt;p&gt;Let&amp;rsquo;s start creating some spreadsheets!&lt;/p&gt;
2048 &lt;h3 id=&quot;creating-a-simple-spreadsheet&quot;&gt;Creating a Simple Spreadsheet&lt;/h3&gt;
2049 &lt;p&gt;Previously, you saw a very quick example of how to write &amp;ldquo;Hello world!&amp;rdquo; into a spreadsheet, so you can start with that:&lt;/p&gt;
2050 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;
2051 &lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
2052 &lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;hello_world.xlsx&amp;quot;&lt;/span&gt;
2053 &lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;
2054 &lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
2055 &lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
2056 &lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;
2057 &lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;
2058 &lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;B1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;world!&amp;quot;&lt;/span&gt;
2059 &lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;
2060 &lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2061 &lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
2062 
2063 &lt;p&gt;The highlighted lines in the code above are the most important ones for writing. In the code, you can see that:&lt;/p&gt;
2064 &lt;ul&gt;
2065 &lt;li&gt;&lt;strong&gt;Line 5&lt;/strong&gt; shows you how to create a new empty workbook.&lt;/li&gt;
2066 &lt;li&gt;&lt;strong&gt;Lines 8 and 9&lt;/strong&gt; show you how to add data to specific cells.&lt;/li&gt;
2067 &lt;li&gt;&lt;strong&gt;Line 11&lt;/strong&gt; shows you how to save the spreadsheet when you&amp;rsquo;re done.&lt;/li&gt;
2068 &lt;/ul&gt;
2069 &lt;p&gt;Even though these lines above can be straightforward, it&amp;rsquo;s still good to know them well for when things get a bit more complicated.&lt;/p&gt;
2070 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
2071 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You&amp;rsquo;ll be using the &lt;code&gt;hello_world.xlsx&lt;/code&gt; spreadsheet for some of the upcoming examples, so keep it handy.&lt;/p&gt;
2072 &lt;/div&gt;
2073 &lt;p&gt;One thing you can do to help with coming code examples is add the following method to your Python file or console:&lt;/p&gt;
2074 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
2075 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values_only&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
2076 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2077 &lt;/pre&gt;&lt;/div&gt;
2078 
2079 &lt;p&gt;It makes it easier to print all of your spreadsheet values by just calling &lt;code&gt;print_rows()&lt;/code&gt;.&lt;/p&gt;
2080 &lt;h3 id=&quot;basic-spreadsheet-operations&quot;&gt;Basic Spreadsheet Operations&lt;/h3&gt;
2081 &lt;p&gt;Before you get into the more advanced topics, it&amp;rsquo;s good for you to know how to manage the most simple elements of a spreadsheet.&lt;/p&gt;
2082 &lt;h4 id=&quot;adding-and-updating-cell-values&quot;&gt;Adding and Updating Cell Values&lt;/h4&gt;
2083 &lt;p&gt;You already learned how to add values to a spreadsheet like this:&lt;/p&gt;
2084 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;value&amp;quot;&lt;/span&gt;
2085 &lt;/pre&gt;&lt;/div&gt;
2086 
2087 &lt;p&gt;There&amp;rsquo;s another way you can do this, by first selecting a cell and then changing its value:&lt;/p&gt;
2088 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
2089 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;
2090 &lt;span class=&quot;go&quot;&gt;&amp;lt;Cell &amp;#39;Sheet&amp;#39;.A1&amp;gt;&lt;/span&gt;
2091 
2092 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
2093 &lt;span class=&quot;go&quot;&gt;&amp;#39;hello&amp;#39;&lt;/span&gt;
2094 
2095 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;hey&amp;quot;&lt;/span&gt;
2096 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
2097 &lt;span class=&quot;go&quot;&gt;&amp;#39;hey&amp;#39;&lt;/span&gt;
2098 &lt;/pre&gt;&lt;/div&gt;
2099 
2100 &lt;p&gt;The new value is only stored into the spreadsheet once you call &lt;code&gt;workbook.save()&lt;/code&gt;.&lt;/p&gt;
2101 &lt;p&gt;The &lt;code&gt;openpyxl&lt;/code&gt; creates a cell when adding a value, if that cell didn&amp;rsquo;t exist before:&lt;/p&gt;
2102 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Before, our spreadsheet has only 1 row&lt;/span&gt;
2103 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
2104 &lt;span class=&quot;go&quot;&gt;(&amp;#39;hello&amp;#39;, &amp;#39;world!&amp;#39;)&lt;/span&gt;
2105 
2106 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Try adding a value to row 10&lt;/span&gt;
2107 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;B10&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;test&amp;quot;&lt;/span&gt;
2108 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
2109 &lt;span class=&quot;go&quot;&gt;(&amp;#39;hello&amp;#39;, &amp;#39;world!&amp;#39;)&lt;/span&gt;
2110 &lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
2111 &lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
2112 &lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
2113 &lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
2114 &lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
2115 &lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
2116 &lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
2117 &lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
2118 &lt;span class=&quot;go&quot;&gt;(None, &amp;#39;test&amp;#39;)&lt;/span&gt;
2119 &lt;/pre&gt;&lt;/div&gt;
2120 
2121 &lt;p&gt;As you can see, when trying to add a value to cell &lt;code&gt;B10&lt;/code&gt;, you end up with a tuple with 10 rows, just so you can have that &lt;em&gt;test&lt;/em&gt; value.&lt;/p&gt;
2122 &lt;h4 id=&quot;managing-rows-and-columns&quot;&gt;Managing Rows and Columns&lt;/h4&gt;
2123 &lt;p&gt;One of the most common things you have to do when manipulating spreadsheets is adding or removing rows and columns. The &lt;code&gt;openpyxl&lt;/code&gt; package allows you to do that in a very straightforward way by using the methods:&lt;/p&gt;
2124 &lt;ul&gt;
2125 &lt;li&gt;&lt;code&gt;.insert_rows()&lt;/code&gt;&lt;/li&gt;
2126 &lt;li&gt;&lt;code&gt;.delete_rows()&lt;/code&gt;&lt;/li&gt;
2127 &lt;li&gt;&lt;code&gt;.insert_cols()&lt;/code&gt;&lt;/li&gt;
2128 &lt;li&gt;&lt;code&gt;.delete_cols()&lt;/code&gt;&lt;/li&gt;
2129 &lt;/ul&gt;
2130 &lt;p&gt;Every single one of those methods can receive two arguments:&lt;/p&gt;
2131 &lt;ol&gt;
2132 &lt;li&gt;&lt;code&gt;idx&lt;/code&gt;&lt;/li&gt;
2133 &lt;li&gt;&lt;code&gt;amount&lt;/code&gt;&lt;/li&gt;
2134 &lt;/ol&gt;
2135 &lt;p&gt;Using our basic &lt;code&gt;hello_world.xlsx&lt;/code&gt; example again, let&amp;rsquo;s see how these methods work:&lt;/p&gt;
2136 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
2137 &lt;span class=&quot;go&quot;&gt;(&amp;#39;hello&amp;#39;, &amp;#39;world!&amp;#39;)&lt;/span&gt;
2138 
2139 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Insert a column before the existing column 1 (&amp;quot;A&amp;quot;)&lt;/span&gt;
2140 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert_cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2141 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
2142 &lt;span class=&quot;go&quot;&gt;(None, &amp;#39;hello&amp;#39;, &amp;#39;world!&amp;#39;)&lt;/span&gt;
2143 
2144 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Insert 5 columns between column 2 (&amp;quot;B&amp;quot;) and 3 (&amp;quot;C&amp;quot;)&lt;/span&gt;
2145 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert_cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2146 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
2147 &lt;span class=&quot;go&quot;&gt;(None, &amp;#39;hello&amp;#39;, None, None, None, None, None, &amp;#39;world!&amp;#39;)&lt;/span&gt;
2148 
2149 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Delete the created columns&lt;/span&gt;
2150 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;delete_cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2151 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;delete_cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2152 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
2153 &lt;span class=&quot;go&quot;&gt;(&amp;#39;hello&amp;#39;, &amp;#39;world!&amp;#39;)&lt;/span&gt;
2154 
2155 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Insert a new row in the beginning&lt;/span&gt;
2156 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2157 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
2158 &lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
2159 &lt;span class=&quot;go&quot;&gt;(&amp;#39;hello&amp;#39;, &amp;#39;world!&amp;#39;)&lt;/span&gt;
2160 
2161 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Insert 3 new rows in the beginning&lt;/span&gt;
2162 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2163 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
2164 &lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
2165 &lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
2166 &lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
2167 &lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
2168 &lt;span class=&quot;go&quot;&gt;(&amp;#39;hello&amp;#39;, &amp;#39;world!&amp;#39;)&lt;/span&gt;
2169 
2170 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Delete the first 4 rows&lt;/span&gt;
2171 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;delete_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2172 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
2173 &lt;span class=&quot;go&quot;&gt;(&amp;#39;hello&amp;#39;, &amp;#39;world!&amp;#39;)&lt;/span&gt;
2174 &lt;/pre&gt;&lt;/div&gt;
2175 
2176 &lt;p&gt;The only thing you need to remember is that when inserting new data (rows or columns), the insertion happens &lt;strong&gt;before&lt;/strong&gt; the &lt;code&gt;idx&lt;/code&gt; parameter.&lt;/p&gt;
2177 &lt;p&gt;So, if you do &lt;code&gt;insert_rows(1)&lt;/code&gt;, it inserts a new row &lt;strong&gt;before&lt;/strong&gt; the existing first row.&lt;/p&gt;
2178 &lt;p&gt;It&amp;rsquo;s the same for columns: when you call &lt;code&gt;insert_cols(2)&lt;/code&gt;, it inserts a new column right &lt;strong&gt;before&lt;/strong&gt; the already existing second column (&lt;code&gt;B&lt;/code&gt;).&lt;/p&gt;
2179 &lt;p&gt;However, when deleting rows or columns, &lt;code&gt;.delete_...&lt;/code&gt; deletes data &lt;strong&gt;starting from&lt;/strong&gt; the index passed as an argument.&lt;/p&gt;
2180 &lt;p&gt;For example, when doing &lt;code&gt;delete_rows(2)&lt;/code&gt; it deletes row &lt;code&gt;2&lt;/code&gt;, and when doing &lt;code&gt;delete_cols(3)&lt;/code&gt; it deletes the third column (&lt;code&gt;C&lt;/code&gt;).&lt;/p&gt;
2181 &lt;h4 id=&quot;managing-sheets&quot;&gt;Managing Sheets&lt;/h4&gt;
2182 &lt;p&gt;Sheet management is also one of those things you might need to know, even though it might be something that you don&amp;rsquo;t use that often.&lt;/p&gt;
2183 &lt;p&gt;If you look back at the code examples from this tutorial, you&amp;rsquo;ll notice the following recurring piece of code:&lt;/p&gt;
2184 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
2185 &lt;/pre&gt;&lt;/div&gt;
2186 
2187 &lt;p&gt;This is the way to select the default sheet from a spreadsheet. However, if you&amp;rsquo;re opening a spreadsheet with multiple sheets, then you can always select a specific one like this:&lt;/p&gt;
2188 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Let&amp;#39;s say you have two sheets: &amp;quot;Products&amp;quot; and &amp;quot;Company Sales&amp;quot;&lt;/span&gt;
2189 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
2190 &lt;span class=&quot;go&quot;&gt;[&amp;#39;Products&amp;#39;, &amp;#39;Company Sales&amp;#39;]&lt;/span&gt;
2191 
2192 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# You can select a sheet using its title&lt;/span&gt;
2193 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;products_sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Products&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
2194 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sales_sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Company Sales&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
2195 &lt;/pre&gt;&lt;/div&gt;
2196 
2197 &lt;p&gt;You can also change a sheet title very easily:&lt;/p&gt;
2198 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
2199 &lt;span class=&quot;go&quot;&gt;[&amp;#39;Products&amp;#39;, &amp;#39;Company Sales&amp;#39;]&lt;/span&gt;
2200 
2201 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;products_sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Products&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
2202 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;products_sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;New Products&amp;quot;&lt;/span&gt;
2203 
2204 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
2205 &lt;span class=&quot;go&quot;&gt;[&amp;#39;New Products&amp;#39;, &amp;#39;Company Sales&amp;#39;]&lt;/span&gt;
2206 &lt;/pre&gt;&lt;/div&gt;
2207 
2208 &lt;p&gt;If you want to create or delete sheets, then you can also do that with &lt;code&gt;.create_sheet()&lt;/code&gt; and &lt;code&gt;.remove()&lt;/code&gt;:&lt;/p&gt;
2209 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
2210 &lt;span class=&quot;go&quot;&gt;[&amp;#39;Products&amp;#39;, &amp;#39;Company Sales&amp;#39;]&lt;/span&gt;
2211 
2212 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;operations_sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Operations&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2213 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
2214 &lt;span class=&quot;go&quot;&gt;[&amp;#39;Products&amp;#39;, &amp;#39;Company Sales&amp;#39;, &amp;#39;Operations&amp;#39;]&lt;/span&gt;
2215 
2216 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# You can also define the position to create the sheet at&lt;/span&gt;
2217 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hr_sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;HR&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2218 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
2219 &lt;span class=&quot;go&quot;&gt;[&amp;#39;HR&amp;#39;, &amp;#39;Products&amp;#39;, &amp;#39;Company Sales&amp;#39;, &amp;#39;Operations&amp;#39;]&lt;/span&gt;
2220 
2221 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# To remove them, just pass the sheet as an argument to the .remove()&lt;/span&gt;
2222 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remove&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;operations_sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2223 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
2224 &lt;span class=&quot;go&quot;&gt;[&amp;#39;HR&amp;#39;, &amp;#39;Products&amp;#39;, &amp;#39;Company Sales&amp;#39;]&lt;/span&gt;
2225 
2226 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remove&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hr_sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2227 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
2228 &lt;span class=&quot;go&quot;&gt;[&amp;#39;Products&amp;#39;, &amp;#39;Company Sales&amp;#39;]&lt;/span&gt;
2229 &lt;/pre&gt;&lt;/div&gt;
2230 
2231 &lt;p&gt;One other thing you can do is make duplicates of a sheet using &lt;code&gt;copy_worksheet()&lt;/code&gt;:&lt;/p&gt;
2232 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
2233 &lt;span class=&quot;go&quot;&gt;[&amp;#39;Products&amp;#39;, &amp;#39;Company Sales&amp;#39;]&lt;/span&gt;
2234 
2235 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;products_sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Products&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
2236 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;copy_worksheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;products_sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2237 &lt;span class=&quot;go&quot;&gt;&amp;lt;Worksheet &amp;quot;Products Copy&amp;quot;&amp;gt;&lt;/span&gt;
2238 
2239 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
2240 &lt;span class=&quot;go&quot;&gt;[&amp;#39;Products&amp;#39;, &amp;#39;Company Sales&amp;#39;, &amp;#39;Products Copy&amp;#39;]&lt;/span&gt;
2241 &lt;/pre&gt;&lt;/div&gt;
2242 
2243 &lt;p&gt;If you open your spreadsheet after saving the above code, you&amp;rsquo;ll notice that the sheet &lt;em&gt;Products Copy&lt;/em&gt; is a duplicate of the sheet &lt;em&gt;Products&lt;/em&gt;.&lt;/p&gt;
2244 &lt;h4 id=&quot;freezing-rows-and-columns&quot;&gt;Freezing Rows and Columns&lt;/h4&gt;
2245 &lt;p&gt;Something that you might want to do when working with big spreadsheets is to freeze a few rows or columns, so they remain visible when you scroll right or down.&lt;/p&gt;
2246 &lt;p&gt;Freezing data allows you to keep an eye on important rows or columns, regardless of where you scroll in the spreadsheet.&lt;/p&gt;
2247 &lt;p&gt;Again, &lt;code&gt;openpyxl&lt;/code&gt; also has a way to accomplish this by using the worksheet &lt;code&gt;freeze_panes&lt;/code&gt; attribute. For this example, go back to our &lt;code&gt;sample.xlsx&lt;/code&gt; spreadsheet and try doing the following:&lt;/p&gt;
2248 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2249 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
2250 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;freeze_panes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;C2&amp;quot;&lt;/span&gt;
2251 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_frozen.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2252 &lt;/pre&gt;&lt;/div&gt;
2253 
2254 &lt;p&gt;If you open the &lt;code&gt;sample_frozen.xlsx&lt;/code&gt; spreadsheet in your favorite spreadsheet editor, you&amp;rsquo;ll notice that row &lt;code&gt;1&lt;/code&gt; and columns &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; are frozen and are always visible no matter where you navigate within the spreadsheet.&lt;/p&gt;
2255 &lt;p&gt;This feature is handy, for example, to keep headers within sight, so you always know what each column represents.&lt;/p&gt;
2256 &lt;p&gt;Here&amp;rsquo;s how it looks in the editor:&lt;/p&gt;
2257 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.12.20.55694a0781f8.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.12.20.55694a0781f8.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.12.20.55694a0781f8.png&amp;amp;w=540&amp;amp;sig=5826de23e5df2e08d625844698fc3a29b32ee7b2 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.12.20.55694a0781f8.png&amp;amp;w=1080&amp;amp;sig=c3abe2321f00372d975bbbf033f3ebe3687eb09f 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_18.12.20.55694a0781f8.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Frozen Rows and Columns&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2258 &lt;p&gt;Notice how you&amp;rsquo;re at the end of the spreadsheet, and yet, you can see both row &lt;code&gt;1&lt;/code&gt; and columns &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt;.&lt;/p&gt;
2259 &lt;h4 id=&quot;adding-filters&quot;&gt;Adding Filters&lt;/h4&gt;
2260 &lt;p&gt;You can use &lt;code&gt;openpyxl&lt;/code&gt; to add filters and sorts to your spreadsheet. However, when you open the spreadsheet, the data won&amp;rsquo;t be rearranged according to these sorts and filters.&lt;/p&gt;
2261 &lt;p&gt;At first, this might seem like a pretty useless feature, but when you&amp;rsquo;re programmatically creating a spreadsheet that is going to be sent and used by somebody else, it&amp;rsquo;s still nice to at least create the filters and allow people to use it afterward.&lt;/p&gt;
2262 &lt;p&gt;The code below is an example of how you would add some filters to our existing &lt;code&gt;sample.xlsx&lt;/code&gt; spreadsheet:&lt;/p&gt;
2263 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Check the used spreadsheet space using the attribute &amp;quot;dimensions&amp;quot;&lt;/span&gt;
2264 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dimensions&lt;/span&gt;
2265 &lt;span class=&quot;go&quot;&gt;&amp;#39;A1:O100&amp;#39;&lt;/span&gt;
2266 
2267 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;auto_filter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;A1:O100&amp;quot;&lt;/span&gt;
2268 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_with_filters.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2269 &lt;/pre&gt;&lt;/div&gt;
2270 
2271 &lt;p&gt;You should now see the filters created when opening the spreadsheet in your editor:&lt;/p&gt;
2272 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.20.35.5fdbfe805194.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.20.35.5fdbfe805194.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.20.35.5fdbfe805194.png&amp;amp;w=540&amp;amp;sig=c1d7ad4f2dfc03fc8730e3babf9000ac74170c7d 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.20.35.5fdbfe805194.png&amp;amp;w=1080&amp;amp;sig=27e888a967ddc112f1e824be671a15e2c111fe6c 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_18.20.35.5fdbfe805194.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Filters&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2273 &lt;p&gt;You don&amp;rsquo;t have to use &lt;code&gt;sheet.dimensions&lt;/code&gt; if you know precisely which part of the spreadsheet you want to apply filters to.&lt;/p&gt;
2274 &lt;h3 id=&quot;adding-formulas&quot;&gt;Adding Formulas&lt;/h3&gt;
2275 &lt;p&gt;&lt;strong&gt;Formulas&lt;/strong&gt; (or &lt;strong&gt;formulae&lt;/strong&gt;) are one of the most powerful features of spreadsheets.&lt;/p&gt;
2276 &lt;p&gt;They gives you the power to apply specific mathematical equations to a range of cells. Using formulas with &lt;code&gt;openpyxl&lt;/code&gt; is as simple as editing the value of a cell.&lt;/p&gt;
2277 &lt;p&gt;You can see the list of formulas supported by &lt;code&gt;openpyxl&lt;/code&gt;:&lt;/p&gt;
2278 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.utils&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FORMULAE&lt;/span&gt;
2279 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FORMULAE&lt;/span&gt;
2280 &lt;span class=&quot;go&quot;&gt;frozenset({&amp;#39;ABS&amp;#39;,&lt;/span&gt;
2281 &lt;span class=&quot;go&quot;&gt;           &amp;#39;ACCRINT&amp;#39;,&lt;/span&gt;
2282 &lt;span class=&quot;go&quot;&gt;           &amp;#39;ACCRINTM&amp;#39;,&lt;/span&gt;
2283 &lt;span class=&quot;go&quot;&gt;           &amp;#39;ACOS&amp;#39;,&lt;/span&gt;
2284 &lt;span class=&quot;go&quot;&gt;           &amp;#39;ACOSH&amp;#39;,&lt;/span&gt;
2285 &lt;span class=&quot;go&quot;&gt;           &amp;#39;AMORDEGRC&amp;#39;,&lt;/span&gt;
2286 &lt;span class=&quot;go&quot;&gt;           &amp;#39;AMORLINC&amp;#39;,&lt;/span&gt;
2287 &lt;span class=&quot;go&quot;&gt;           &amp;#39;AND&amp;#39;,&lt;/span&gt;
2288 &lt;span class=&quot;go&quot;&gt;           ...&lt;/span&gt;
2289 &lt;span class=&quot;go&quot;&gt;           &amp;#39;YEARFRAC&amp;#39;,&lt;/span&gt;
2290 &lt;span class=&quot;go&quot;&gt;           &amp;#39;YIELD&amp;#39;,&lt;/span&gt;
2291 &lt;span class=&quot;go&quot;&gt;           &amp;#39;YIELDDISC&amp;#39;,&lt;/span&gt;
2292 &lt;span class=&quot;go&quot;&gt;           &amp;#39;YIELDMAT&amp;#39;,&lt;/span&gt;
2293 &lt;span class=&quot;go&quot;&gt;           &amp;#39;ZTEST&amp;#39;})&lt;/span&gt;
2294 &lt;/pre&gt;&lt;/div&gt;
2295 
2296 &lt;p&gt;Let&amp;rsquo;s add some formulas to our &lt;code&gt;sample.xlsx&lt;/code&gt; spreadsheet.&lt;/p&gt;
2297 &lt;p&gt;Starting with something easy, let&amp;rsquo;s check the average star rating for the 99 reviews within the spreadsheet:&lt;/p&gt;
2298 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Star rating is column &amp;quot;H&amp;quot;&lt;/span&gt;
2299 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;P2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;=AVERAGE(H2:H100)&amp;quot;&lt;/span&gt;
2300 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_formulas.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2301 &lt;/pre&gt;&lt;/div&gt;
2302 
2303 &lt;p&gt;If you open the spreadsheet now and go to cell &lt;code&gt;P2&lt;/code&gt;, you should see that its value is: &lt;em&gt;4.18181818181818&lt;/em&gt;. Have a look in the editor:&lt;/p&gt;
2304 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.33.09.7c2633f706cc.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.33.09.7c2633f706cc.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.33.09.7c2633f706cc.png&amp;amp;w=540&amp;amp;sig=5d7a9eb97acf524d5d2b9b93ae0e9214bbcf95c8 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.33.09.7c2633f706cc.png&amp;amp;w=1080&amp;amp;sig=8af67321cb101fb2f30fd0ca2bdcc62de35c9334 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_18.33.09.7c2633f706cc.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Average Formula&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2305 &lt;p&gt;You can use the same methodology to add any formulas to your spreadsheet. For example, let&amp;rsquo;s count the number of reviews that had helpful votes:&lt;/p&gt;
2306 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# The helpful votes are counted on column &amp;quot;I&amp;quot;&lt;/span&gt;
2307 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;P3&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;=COUNTIF(I2:I100, &amp;quot;&amp;gt;0&amp;quot;)&amp;#39;&lt;/span&gt;
2308 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_formulas.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2309 &lt;/pre&gt;&lt;/div&gt;
2310 
2311 &lt;p&gt;You should get the number &lt;code&gt;21&lt;/code&gt; on your &lt;code&gt;P3&lt;/code&gt; spreadsheet cell like so:&lt;/p&gt;
2312 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.35.24.e26e97b0c9c0.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.35.24.e26e97b0c9c0.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.35.24.e26e97b0c9c0.png&amp;amp;w=540&amp;amp;sig=0ec4c4c12a792a1a393e0273855282bfa0594d53 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.35.24.e26e97b0c9c0.png&amp;amp;w=1080&amp;amp;sig=67b399b8cb79ddbe7285da0325fe8f6b9edf3ecc 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_18.35.24.e26e97b0c9c0.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Average and CountIf Formula&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2313 &lt;p&gt;You&amp;rsquo;ll have to make sure that the strings within a formula are always in double quotes, so you either have to use single quotes around the formula like in the example above or you&amp;rsquo;ll have to escape the double quotes inside the formula: &lt;code&gt;&quot;=COUNTIF(I2:I100, \&quot;&amp;gt;0\&quot;)&quot;&lt;/code&gt;.&lt;/p&gt;
2314 &lt;p&gt;There are a ton of other formulas you can add to your spreadsheet using the same procedure you tried above. Give it a go yourself!&lt;/p&gt;
2315 &lt;h3 id=&quot;adding-styles&quot;&gt;Adding Styles&lt;/h3&gt;
2316 &lt;p&gt;Even though styling a spreadsheet might not be something you would do every day, it&amp;rsquo;s still good to know how to do it.&lt;/p&gt;
2317 &lt;p&gt;Using &lt;code&gt;openpyxl&lt;/code&gt;, you can apply multiple styling options to your spreadsheet, including fonts, borders, colors, and so on. Have a look at the &lt;code&gt;openpyxl&lt;/code&gt; &lt;a href=&quot;https://openpyxl.readthedocs.io/en/stable/styles.html&quot;&gt;documentation&lt;/a&gt; to learn more.&lt;/p&gt;
2318 &lt;p&gt;You can also choose to either apply a style directly to a cell or create a template and reuse it to apply styles to multiple cells.&lt;/p&gt;
2319 &lt;p&gt;Let&amp;rsquo;s start by having a look at simple cell styling, using our &lt;code&gt;sample.xlsx&lt;/code&gt; again as the base spreadsheet:&lt;/p&gt;
2320 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Import necessary style classes&lt;/span&gt;
2321 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.styles&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Alignment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Border&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Side&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;
2322 
2323 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Create a few styles&lt;/span&gt;
2324 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bold_font&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bold&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2325 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;big_red_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2326 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;center_aligned_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Alignment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;horizontal&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;center&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2327 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;double_border_side&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Side&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;border_style&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;double&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2328 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square_border&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Border&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;top&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;double_border_side&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2329 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                       &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;double_border_side&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2330 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                       &lt;span class=&quot;n&quot;&gt;bottom&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;double_border_side&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2331 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                       &lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;double_border_side&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2332 
2333 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Style some cells!&lt;/span&gt;
2334 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bold_font&lt;/span&gt;
2335 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A3&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;big_red_text&lt;/span&gt;
2336 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A4&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alignment&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;center_aligned_text&lt;/span&gt;
2337 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A5&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;border&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;square_border&lt;/span&gt;
2338 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_styles.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2339 &lt;/pre&gt;&lt;/div&gt;
2340 
2341 &lt;p&gt;If you open your spreadsheet now, you should see quite a few different styles on the first 5 cells of column &lt;code&gt;A&lt;/code&gt;:&lt;/p&gt;
2342 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.43.15.e3aeb3fb06e3.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.43.15.e3aeb3fb06e3.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.43.15.e3aeb3fb06e3.png&amp;amp;w=540&amp;amp;sig=ecc21878006697a6135ae515442642a95ab2bfb6 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.43.15.e3aeb3fb06e3.png&amp;amp;w=1080&amp;amp;sig=6f0ef4a148f1ca5a588e0cb2c02b0c9aad4246f2 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_18.43.15.e3aeb3fb06e3.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Simple Cell Styles&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2343 &lt;p&gt;There you go. You got:&lt;/p&gt;
2344 &lt;ul&gt;
2345 &lt;li&gt;&lt;strong&gt;A2&lt;/strong&gt; with the text in bold&lt;/li&gt;
2346 &lt;li&gt;&lt;strong&gt;A3&lt;/strong&gt; with the text in red and bigger font size&lt;/li&gt;
2347 &lt;li&gt;&lt;strong&gt;A4&lt;/strong&gt; with the text centered&lt;/li&gt;
2348 &lt;li&gt;&lt;strong&gt;A5&lt;/strong&gt; with a square border around the text&lt;/li&gt;
2349 &lt;/ul&gt;
2350 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
2351 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; For the colors, you can also use HEX codes instead by doing  &lt;code&gt;Font(color=&quot;C70E0F&quot;)&lt;/code&gt;.&lt;/p&gt;
2352 &lt;/div&gt;
2353 &lt;p&gt;You can also combine styles by simply adding them to the cell at the same time:&lt;/p&gt;
2354 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Reusing the same styles from the example above&lt;/span&gt;
2355 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A6&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alignment&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;center_aligned_text&lt;/span&gt;
2356 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A6&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;big_red_text&lt;/span&gt;
2357 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A6&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;border&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;square_border&lt;/span&gt;
2358 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_styles.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2359 &lt;/pre&gt;&lt;/div&gt;
2360 
2361 &lt;p&gt;Have a look at cell &lt;code&gt;A6&lt;/code&gt; here:&lt;/p&gt;
2362 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.46.04.314517930065.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.46.04.314517930065.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.46.04.314517930065.png&amp;amp;w=540&amp;amp;sig=290bbf523eb24ac8c9741daf86701ca57cad4b96 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.46.04.314517930065.png&amp;amp;w=1080&amp;amp;sig=9decedef154c2138e26b287f2186213142650f6e 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_18.46.04.314517930065.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Coupled Cell Styles&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2363 &lt;p&gt;When you want to apply multiple styles to one or several cells, you can use a &lt;code&gt;NamedStyle&lt;/code&gt; class instead, which is like a style template that you can use over and over again. Have a look at the example below:&lt;/p&gt;
2364 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.styles&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NamedStyle&lt;/span&gt;
2365 
2366 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Let&amp;#39;s create a style template for the header row&lt;/span&gt;
2367 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;header&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NamedStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;header&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2368 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bold&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2369 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;border&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Border&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bottom&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Side&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;border_style&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;thin&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
2370 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alignment&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Alignment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;horizontal&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;center&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vertical&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;center&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2371 
2372 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Now let&amp;#39;s apply this to all first row (header) cells&lt;/span&gt;
2373 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;header_row&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
2374 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;header_row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
2375 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;style&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;header&lt;/span&gt;
2376 
2377 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_styles.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2378 &lt;/pre&gt;&lt;/div&gt;
2379 
2380 &lt;p&gt;If you open the spreadsheet now, you should see that its first row is bold, the text is aligned to the center, and there&amp;rsquo;s a small bottom border! Have a look below:&lt;/p&gt;
2381 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.48.33.4bc57d1b24d5.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.48.33.4bc57d1b24d5.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.48.33.4bc57d1b24d5.png&amp;amp;w=540&amp;amp;sig=199107a0c9ea60fbf1dfcc078a7680b43faeef3a 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.48.33.4bc57d1b24d5.png&amp;amp;w=1080&amp;amp;sig=af3e5225e36a24dea088e8c175da02050bb5dda9 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_18.48.33.4bc57d1b24d5.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Named Styles&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2382 &lt;p&gt;As you saw above, there are many options when it comes to styling, and it depends on the use case, so feel free to check &lt;code&gt;openpyxl&lt;/code&gt; &lt;a href=&quot;https://openpyxl.readthedocs.io/en/stable/styles.html&quot;&gt;documentation&lt;/a&gt; and see what other things you can do.&lt;/p&gt;
2383 &lt;h3 id=&quot;conditional-formatting&quot;&gt;Conditional Formatting&lt;/h3&gt;
2384 &lt;p&gt;This feature is one of my personal favorites when it comes to adding styles to a spreadsheet.&lt;/p&gt;
2385 &lt;p&gt;It&amp;rsquo;s a much more powerful approach to styling because it dynamically applies styles according to how the data in the spreadsheet changes.&lt;/p&gt;
2386 &lt;p&gt;In a nutshell, &lt;strong&gt;conditional formatting&lt;/strong&gt; allows you to specify a list of styles to apply to a cell (or cell range) according to specific conditions.&lt;/p&gt;
2387 &lt;p&gt;For example, a widespread use case is to have a balance sheet where all the negative totals are in red, and the positive ones are in green. This formatting makes it much more efficient to spot good vs bad periods.&lt;/p&gt;
2388 &lt;p&gt;Without further ado, let&amp;rsquo;s pick our favorite spreadsheet&amp;mdash;&lt;code&gt;sample.xlsx&lt;/code&gt;&amp;mdash;and add some conditional formatting.&lt;/p&gt;
2389 &lt;p&gt;You can start by adding a simple one that adds a red background to all reviews with less than 3 stars:&lt;/p&gt;
2390 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.styles&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PatternFill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;
2391 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.styles.differential&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DifferentialStyle&lt;/span&gt;
2392 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.formatting.rule&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Rule&lt;/span&gt;
2393 
2394 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;red_background&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PatternFill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bgColor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2395 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;diff_style&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DifferentialStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;red_background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2396 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rule&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Rule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;expression&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dxf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;diff_style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2397 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rule&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;formula&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;$H1&amp;lt;3&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
2398 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conditional_formatting&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A1:O100&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2399 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_conditional_formatting.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2400 &lt;/pre&gt;&lt;/div&gt;
2401 
2402 &lt;p&gt;Now you&amp;rsquo;ll see all the reviews with a star rating below 3 marked with a red background:&lt;/p&gt;
2403 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.55.41.17f234a186c6.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.55.41.17f234a186c6.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.55.41.17f234a186c6.png&amp;amp;w=540&amp;amp;sig=f3c141c2fe708c031c32c083cb038a736fd8da87 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.55.41.17f234a186c6.png&amp;amp;w=1080&amp;amp;sig=9ded3045cee09d34cd6f5dada7a4eea669ac4808 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_18.55.41.17f234a186c6.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Simple Conditional Formatting&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2404 &lt;p&gt;Code-wise, the only things that are new here are the objects &lt;code&gt;DifferentialStyle&lt;/code&gt; and &lt;code&gt;Rule&lt;/code&gt;:&lt;/p&gt;
2405 &lt;ul&gt;
2406 &lt;li&gt;&lt;strong&gt;&lt;code&gt;DifferentialStyle&lt;/code&gt;&lt;/strong&gt; is quite similar to &lt;code&gt;NamedStyle&lt;/code&gt;, which you already saw above, and it&amp;rsquo;s used to aggregate multiple styles such as fonts, borders, alignment, and so forth.&lt;/li&gt;
2407 &lt;li&gt;&lt;strong&gt;&lt;code&gt;Rule&lt;/code&gt;&lt;/strong&gt; is responsible for selecting the cells and applying the styles if the cells match the rule&amp;rsquo;s logic.&lt;/li&gt;
2408 &lt;/ul&gt;
2409 &lt;p&gt;Using a &lt;code&gt;Rule&lt;/code&gt; object, you can create numerous conditional formatting scenarios.&lt;/p&gt;
2410 &lt;p&gt;However, for simplicity sake, the &lt;code&gt;openpyxl&lt;/code&gt; package offers 3 built-in formats that make it easier to create a few common conditional formatting patterns. These built-ins are:&lt;/p&gt;
2411 &lt;ul&gt;
2412 &lt;li&gt;&lt;code&gt;ColorScale&lt;/code&gt;&lt;/li&gt;
2413 &lt;li&gt;&lt;code&gt;IconSet&lt;/code&gt;&lt;/li&gt;
2414 &lt;li&gt;&lt;code&gt;DataBar&lt;/code&gt;&lt;/li&gt;
2415 &lt;/ul&gt;
2416 &lt;p&gt;The &lt;strong&gt;ColorScale&lt;/strong&gt; gives you the ability to create color gradients:&lt;/p&gt;
2417 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.formatting.rule&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ColorScaleRule&lt;/span&gt;
2418 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color_scale_rule&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ColorScaleRule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;min&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2419 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;start_color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2420 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;end_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;max&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2421 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;end_color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GREEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2422 
2423 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Again, let&amp;#39;s add this gradient to the star ratings, column &amp;quot;H&amp;quot;&lt;/span&gt;
2424 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conditional_formatting&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;H2:H100&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color_scale_rule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2425 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_conditional_formatting_color_scale.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2426 &lt;/pre&gt;&lt;/div&gt;
2427 
2428 &lt;p&gt;Now you should see a color gradient on column &lt;code&gt;H&lt;/code&gt;, from red to green, according to the star rating:&lt;/p&gt;
2429 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_19.00.57.26756963c1e9.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_19.00.57.26756963c1e9.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_19.00.57.26756963c1e9.png&amp;amp;w=540&amp;amp;sig=782964d150a8adc1de811fab78b0accde6357f85 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_19.00.57.26756963c1e9.png&amp;amp;w=1080&amp;amp;sig=4c7c87c194ec0a8eb3a9e73fdabf3ead991e96ea 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_19.00.57.26756963c1e9.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Color Scale Conditional Formatting&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2430 &lt;p&gt;You can also add a third color and make two gradients instead:&lt;/p&gt;
2431 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.formatting.rule&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ColorScaleRule&lt;/span&gt;
2432 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color_scale_rule&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ColorScaleRule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2433 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;start_value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2434 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;start_color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2435 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;mid_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2436 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;mid_value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2437 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;mid_color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;YELLOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2438 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;end_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2439 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;end_value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2440 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;end_color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GREEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2441 
2442 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Again, let&amp;#39;s add this gradient to the star ratings, column &amp;quot;H&amp;quot;&lt;/span&gt;
2443 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conditional_formatting&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;H2:H100&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color_scale_rule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2444 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_conditional_formatting_color_scale_3.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2445 &lt;/pre&gt;&lt;/div&gt;
2446 
2447 &lt;p&gt;This time, you&amp;rsquo;ll notice that star ratings between 1 and 3 have a gradient from red to yellow, and star ratings between 3 and 5 have a gradient from yellow to green:&lt;/p&gt;
2448 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_19.03.30.0de9a2ff9866.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_19.03.30.0de9a2ff9866.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_19.03.30.0de9a2ff9866.png&amp;amp;w=540&amp;amp;sig=17daddc356c8ff5c78497b200fe57ab69f80617d 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_19.03.30.0de9a2ff9866.png&amp;amp;w=1080&amp;amp;sig=6dcb18f4aa3f8ae0a395ca1e3581f8d4c59805ad 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_19.03.30.0de9a2ff9866.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With 2 Color Scales Conditional Formatting&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2449 &lt;p&gt;The &lt;strong&gt;IconSet&lt;/strong&gt; allows you to add an icon to the cell according to its value:&lt;/p&gt;
2450 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.formatting.rule&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IconSetRule&lt;/span&gt;
2451 
2452 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;icon_set_rule&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IconSetRule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;5Arrows&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;num&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
2453 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conditional_formatting&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;H2:H100&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;icon_set_rule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2454 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_conditional_formatting_icon_set.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2455 &lt;/pre&gt;&lt;/div&gt;
2456 
2457 &lt;p&gt;You&amp;rsquo;ll see a colored arrow next to the star rating. This arrow is red and points down when the value of the cell is 1 and, as the rating gets better, the arrow starts pointing up and becomes green:&lt;/p&gt;
2458 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_19.07.29.23e75ff46771.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_19.07.29.23e75ff46771.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_19.07.29.23e75ff46771.png&amp;amp;w=540&amp;amp;sig=388fc68ff53fa2e2d3d5678acfd41f10fa8eccde 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_19.07.29.23e75ff46771.png&amp;amp;w=1080&amp;amp;sig=e33f483f9758189782bb619a6714c948130375aa 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_19.07.29.23e75ff46771.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Icon Set Conditional Formatting&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2459 &lt;p&gt;The &lt;code&gt;openpyxl&lt;/code&gt; package has a &lt;a href=&quot;https://openpyxl.readthedocs.io/en/stable/formatting.html#iconset&quot;&gt;full list&lt;/a&gt; of other icons you can use, besides the arrow.&lt;/p&gt;
2460 &lt;p&gt;Finally, the &lt;strong&gt;DataBar&lt;/strong&gt; allows you to create progress bars:&lt;/p&gt;
2461 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.formatting.rule&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DataBarRule&lt;/span&gt;
2462 
2463 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_bar_rule&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DataBarRule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2464 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                            &lt;span class=&quot;n&quot;&gt;start_value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2465 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                            &lt;span class=&quot;n&quot;&gt;end_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2466 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                            &lt;span class=&quot;n&quot;&gt;end_value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;5&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2467 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                            &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GREEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2468 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conditional_formatting&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;H2:H100&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_bar_rule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2469 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_conditional_formatting_data_bar.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2470 &lt;/pre&gt;&lt;/div&gt;
2471 
2472 &lt;p&gt;You&amp;rsquo;ll now see a green progress bar that gets fuller the closer the star rating is to the number 5:&lt;/p&gt;
2473 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_19.09.10.ebbe032c088d.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_19.09.10.ebbe032c088d.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_19.09.10.ebbe032c088d.png&amp;amp;w=540&amp;amp;sig=a7b8e3515fde3ff6b662ff1780cbc290da9ce2ad 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_19.09.10.ebbe032c088d.png&amp;amp;w=1080&amp;amp;sig=1e3d2befc147a43c5b98061cf5888abf22ca4c4a 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_19.09.10.ebbe032c088d.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Data Bar Conditional Formatting&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2474 &lt;p&gt;As you can see, there are a lot of cool things you can do with conditional formatting.&lt;/p&gt;
2475 &lt;p&gt;Here, you saw only a few examples of what you can achieve with it, but check the &lt;code&gt;openpyxl&lt;/code&gt; &lt;a href=&quot;https://openpyxl.readthedocs.io/en/stable/formatting.html&quot;&gt;documentation&lt;/a&gt; to see a bunch of other options.&lt;/p&gt;
2476 &lt;h3 id=&quot;adding-images&quot;&gt;Adding Images&lt;/h3&gt;
2477 &lt;p&gt;Even though images are not something that you&amp;rsquo;ll often see in a spreadsheet, it&amp;rsquo;s quite cool to be able to add them. Maybe you can use it for branding purposes or to make spreadsheets more personal.&lt;/p&gt;
2478 &lt;p&gt;To be able to load images to a spreadsheet using &lt;code&gt;openpyxl&lt;/code&gt;, you&amp;rsquo;ll have to install &lt;code&gt;Pillow&lt;/code&gt;:&lt;/p&gt;
2479 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pip install Pillow
2480 &lt;/pre&gt;&lt;/div&gt;
2481 
2482 &lt;p&gt;Apart from that, you&amp;rsquo;ll also need an image. For this example, you can grab the &lt;em&gt;Real Python&lt;/em&gt; logo below and convert it from &lt;code&gt;.webp&lt;/code&gt; to &lt;code&gt;.png&lt;/code&gt; using an online converter such as &lt;a href=&quot;https://cloudconvert.com/webp-to-png&quot;&gt;cloudconvert.com&lt;/a&gt;, save the final file as &lt;code&gt;logo.png&lt;/code&gt;, and copy it to the root folder where you&amp;rsquo;re running your examples:&lt;/p&gt;
2483 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/real-python-logo-round.4d95338e8944.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-25&quot; src=&quot;https://files.realpython.com/media/real-python-logo-round.4d95338e8944.png&quot; width=&quot;1500&quot; height=&quot;1500&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/real-python-logo-round.4d95338e8944.png&amp;amp;w=375&amp;amp;sig=e431a39c9d7f2d5963a81687571a41288c359142 375w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/real-python-logo-round.4d95338e8944.png&amp;amp;w=750&amp;amp;sig=a098752adfc378feee6bc69748af593ed078b8c0 750w, https://files.realpython.com/media/real-python-logo-round.4d95338e8944.png 1500w&quot; sizes=&quot;75vw&quot; alt=&quot;Real Python Logo&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2484 &lt;p&gt;Afterward, this is the code you need to import that image into the &lt;code&gt;hello_word.xlsx&lt;/code&gt; spreadsheet:&lt;/p&gt;
2485 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;
2486 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.drawing.image&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;
2487 
2488 &lt;span class=&quot;c1&quot;&gt;# Let&amp;#39;s use the hello_world spreadsheet since it has less data&lt;/span&gt;
2489 &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;hello_world.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2490 &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
2491 
2492 &lt;span class=&quot;n&quot;&gt;logo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;logo.png&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2493 
2494 &lt;span class=&quot;c1&quot;&gt;# A bit of resizing to not fill the whole spreadsheet with the logo&lt;/span&gt;
2495 &lt;span class=&quot;n&quot;&gt;logo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;height&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;150&lt;/span&gt;
2496 &lt;span class=&quot;n&quot;&gt;logo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;150&lt;/span&gt;
2497 
2498 &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;A3&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2499 &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;hello_world_logo.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2500 &lt;/pre&gt;&lt;/div&gt;
2501 
2502 &lt;p&gt;You have an image on your spreadsheet! Here it is:&lt;/p&gt;
2503 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_20.05.30.2a69f2a77f68.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_20.05.30.2a69f2a77f68.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_20.05.30.2a69f2a77f68.png&amp;amp;w=540&amp;amp;sig=574c2c425c011fa21e790f7cd4a41547f2449b01 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_20.05.30.2a69f2a77f68.png&amp;amp;w=1080&amp;amp;sig=e6b9c78c7daa929ae86f887d560c3f50f0851d5d 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_20.05.30.2a69f2a77f68.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Image&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2504 &lt;p&gt;The image&amp;rsquo;s left top corner is on the cell you chose, in this case, &lt;code&gt;A3&lt;/code&gt;.&lt;/p&gt;
2505 &lt;h3 id=&quot;adding-pretty-charts&quot;&gt;Adding Pretty Charts&lt;/h3&gt;
2506 &lt;p&gt;Another powerful thing you can do with spreadsheets is create an incredible variety of charts. &lt;/p&gt;
2507 &lt;p&gt;Charts are a great way to visualize and understand loads of data quickly. There are a lot of different chart types: bar chart, pie chart, line chart, and so on. &lt;code&gt;openpyxl&lt;/code&gt; has support for a lot of them.&lt;/p&gt;
2508 &lt;p&gt;Here, you&amp;rsquo;ll see only a couple of examples of charts because the theory behind it is the same for every single chart type:&lt;/p&gt;
2509 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
2510 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; A few of the chart types that &lt;code&gt;openpyxl&lt;/code&gt; currently doesn&amp;rsquo;t have support for are Funnel, Gantt, Pareto, Treemap, Waterfall, Map, and Sunburst.&lt;/p&gt;
2511 &lt;/div&gt;
2512 &lt;p&gt;For any chart you want to build, you&amp;rsquo;ll need to define the chart type: &lt;code&gt;BarChart&lt;/code&gt;, &lt;code&gt;LineChart&lt;/code&gt;, and so forth, plus the data to be used for the chart, which is called &lt;code&gt;Reference&lt;/code&gt;.&lt;/p&gt;
2513 &lt;p&gt;Before you can build your chart, you need to define what data you want to see represented in it. Sometimes, you can use the dataset as is, but other times you need to massage the data a bit to get additional information.&lt;/p&gt;
2514 &lt;p&gt;Let&amp;rsquo;s start by building a new workbook with some sample data:&lt;/p&gt;
2515 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;
2516 &lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.chart&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BarChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;
2517 &lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;
2518 &lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
2519 &lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
2520 &lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;
2521 &lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Let&amp;#39;s create some sample sales data&lt;/span&gt;
2522 &lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
2523 &lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Online&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Store&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2524 &lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;45&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2525 &lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2526 &lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2527 &lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2528 &lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2529 &lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;35&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2530 &lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2531 &lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
2532 &lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;
2533 &lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
2534 &lt;span class=&quot;lineno&quot;&gt;20 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2535 &lt;/pre&gt;&lt;/div&gt;
2536 
2537 &lt;p&gt;Now you&amp;rsquo;re going to start by creating a &lt;strong&gt;bar chart&lt;/strong&gt; that displays the total number of sales per product:&lt;/p&gt;
2538 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;22 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BarChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
2539 &lt;span class=&quot;lineno&quot;&gt;23 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;worksheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2540 &lt;span class=&quot;lineno&quot;&gt;24 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2541 &lt;span class=&quot;lineno&quot;&gt;25 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2542 &lt;span class=&quot;lineno&quot;&gt;26 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2543 &lt;span class=&quot;lineno&quot;&gt;27 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2544 &lt;span class=&quot;lineno&quot;&gt;28 &lt;/span&gt;
2545 &lt;span class=&quot;lineno&quot;&gt;29 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;titles_from_data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2546 &lt;span class=&quot;lineno&quot;&gt;30 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;E2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2547 &lt;span class=&quot;lineno&quot;&gt;31 &lt;/span&gt;
2548 &lt;span class=&quot;lineno&quot;&gt;32 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;chart.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2549 &lt;/pre&gt;&lt;/div&gt;
2550 
2551 &lt;p&gt;There you have it. Below, you can see a very straightforward bar chart  showing the difference between &lt;strong&gt;online&lt;/strong&gt; product sales online and &lt;strong&gt;in-store&lt;/strong&gt; product sales:&lt;/p&gt;
2552 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_20.59.43.7eac35127b97.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_20.59.43.7eac35127b97.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_20.59.43.7eac35127b97.png&amp;amp;w=540&amp;amp;sig=bcdfa015a56b903169702ddbeb1ec06c8d67bc87 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_20.59.43.7eac35127b97.png&amp;amp;w=1080&amp;amp;sig=904be9b2a172b3ae7436311c9d05f6a9ad8ae451 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_20.59.43.7eac35127b97.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Bar Chart&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2553 &lt;p&gt;Like with images, the top left corner of the chart is on the cell you added the chart to. In your case, it was on cell &lt;code&gt;E2&lt;/code&gt;.&lt;/p&gt;
2554 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
2555 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Depending on whether you&amp;rsquo;re using Microsoft Excel or an open-source alternative (LibreOffice or OpenOffice), the chart might look slightly different.&lt;/p&gt;
2556 &lt;/div&gt;
2557 &lt;p&gt;Try creating a &lt;strong&gt;line chart&lt;/strong&gt; instead, changing the data a bit:&lt;/p&gt;
2558 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt;
2559 &lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;
2560 &lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.chart&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LineChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;
2561 &lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;
2562 &lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
2563 &lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
2564 &lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;
2565 &lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Let&amp;#39;s create some sample sales data&lt;/span&gt;
2566 &lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
2567 &lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;January&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;February&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;March&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;April&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2568 &lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;    &lt;span class=&quot;s2&quot;&gt;&amp;quot;May&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;June&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;July&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;August&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;September&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2569 &lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;     &lt;span class=&quot;s2&quot;&gt;&amp;quot;October&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;November&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;December&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2570 &lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2571 &lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2572 &lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2573 &lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
2574 &lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;
2575 &lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
2576 &lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2577 &lt;span class=&quot;lineno&quot;&gt;20 &lt;/span&gt;
2578 &lt;span class=&quot;lineno&quot;&gt;21 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2579 &lt;span class=&quot;lineno&quot;&gt;22 &lt;/span&gt;                           &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2580 &lt;span class=&quot;lineno&quot;&gt;23 &lt;/span&gt;                           &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2581 &lt;span class=&quot;lineno&quot;&gt;24 &lt;/span&gt;                           &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
2582 &lt;span class=&quot;lineno&quot;&gt;25 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
2583 &lt;span class=&quot;lineno&quot;&gt;26 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2584 &lt;/pre&gt;&lt;/div&gt;
2585 
2586 &lt;p&gt;With the above code, you&amp;rsquo;ll be able to generate some random data regarding the sales of 3 different products across a whole year.&lt;/p&gt;
2587 &lt;p&gt;Once that&amp;rsquo;s done, you can very easily create a line chart with the following code:&lt;/p&gt;
2588 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;28 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LineChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
2589 &lt;span class=&quot;lineno&quot;&gt;29 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;worksheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2590 &lt;span class=&quot;lineno&quot;&gt;30 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2591 &lt;span class=&quot;lineno&quot;&gt;31 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2592 &lt;span class=&quot;lineno&quot;&gt;32 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2593 &lt;span class=&quot;lineno&quot;&gt;33 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2594 &lt;span class=&quot;lineno&quot;&gt;34 &lt;/span&gt;
2595 &lt;span class=&quot;lineno&quot;&gt;35 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from_rows&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;titles_from_data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2596 &lt;span class=&quot;lineno&quot;&gt;36 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;C6&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2597 &lt;span class=&quot;lineno&quot;&gt;37 &lt;/span&gt;
2598 &lt;span class=&quot;lineno&quot;&gt;38 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;line_chart.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2599 &lt;/pre&gt;&lt;/div&gt;
2600 
2601 &lt;p&gt;Here&amp;rsquo;s the outcome of the above piece of code:&lt;/p&gt;
2602 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.06.42.e4e52ab1b433.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.06.42.e4e52ab1b433.png&quot; width=&quot;2160&quot; height=&quot;1414&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.06.42.e4e52ab1b433.png&amp;amp;w=540&amp;amp;sig=362319a9716ded57c9567de98c52a4dc805b5346 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.06.42.e4e52ab1b433.png&amp;amp;w=1080&amp;amp;sig=cbd7b37aa77318e4ed8d2401281817fb53a7144b 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_21.06.42.e4e52ab1b433.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Line Chart&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2603 &lt;p&gt;One thing to keep in mind here is the fact that you&amp;rsquo;re using &lt;code&gt;from_rows=True&lt;/code&gt; when adding the data. This argument makes the chart plot row by row instead of column by column.&lt;/p&gt;
2604 &lt;p&gt;In your sample data, you see that each product has a row with 12 values (1 column per month). That&amp;rsquo;s why you use &lt;code&gt;from_rows&lt;/code&gt;. If you don&amp;rsquo;t pass that argument, by default, the chart tries to plot by column, and you&amp;rsquo;ll get a month-by-month comparison of sales.&lt;/p&gt;
2605 &lt;p&gt;Another difference that has to do with the above argument change is the fact that our &lt;code&gt;Reference&lt;/code&gt; now starts from the first column, &lt;code&gt;min_col=1&lt;/code&gt;, instead of the second one. This change is needed because the chart now expects the first column to have the titles.&lt;/p&gt;
2606 &lt;p&gt;There are a couple of other things you can also change regarding the style of the chart. For example, you can add specific categories to the chart:&lt;/p&gt;
2607 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cats&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;worksheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2608                  &lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2609                  &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2610                  &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2611                  &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2612 &lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_categories&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cats&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2613 &lt;/pre&gt;&lt;/div&gt;
2614 
2615 &lt;p&gt;Add this piece of code before saving the workbook, and you should see the month names appearing instead of numbers:&lt;/p&gt;
2616 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.08.05.8867e2cced85.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.08.05.8867e2cced85.png&quot; width=&quot;2160&quot; height=&quot;1414&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.08.05.8867e2cced85.png&amp;amp;w=540&amp;amp;sig=6e48719b75e585dcb58fec3768def0630bb367ff 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.08.05.8867e2cced85.png&amp;amp;w=1080&amp;amp;sig=9fed1350289fe067d8f50c07516bfcc460f4a720 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_21.08.05.8867e2cced85.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Line Chart and Categories&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2617 &lt;p&gt;Code-wise, this is a minimal change. But in terms of the readability of the spreadsheet, this makes it much easier for someone to open the spreadsheet and understand the chart straight away.&lt;/p&gt;
2618 &lt;p&gt;Another thing you can do to improve the chart readability is to add an axis. You can do it using the attributes &lt;code&gt;x_axis&lt;/code&gt; and &lt;code&gt;y_axis&lt;/code&gt;:&lt;/p&gt;
2619 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Months&amp;quot;&lt;/span&gt;
2620 &lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Sales (per unit)&amp;quot;&lt;/span&gt;
2621 &lt;/pre&gt;&lt;/div&gt;
2622 
2623 &lt;p&gt;This will generate a spreadsheet like the below one:&lt;/p&gt;
2624 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.09.46.ce55f629b073.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.09.46.ce55f629b073.png&quot; width=&quot;2160&quot; height=&quot;1414&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.09.46.ce55f629b073.png&amp;amp;w=540&amp;amp;sig=ac73f73702b55a957c77d6da224cde46c2c9a802 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.09.46.ce55f629b073.png&amp;amp;w=1080&amp;amp;sig=408f6929a900e4a5ccb5cb1cca8647cf64d0d069 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_21.09.46.ce55f629b073.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Line Chart, Categories and Axis Titles&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2625 &lt;p&gt;As you can see, small changes like the above make reading your chart a much easier and quicker task.&lt;/p&gt;
2626 &lt;p&gt;There is also a way to style your chart by using Excel&amp;rsquo;s default &lt;code&gt;ChartStyle&lt;/code&gt; property. In this case, you have to choose a number between 1 and 48. Depending on your choice, the colors of your chart change as well:&lt;/p&gt;
2627 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# You can play with this by choosing any number between 1 and 48&lt;/span&gt;
2628 &lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;style&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt;
2629 &lt;/pre&gt;&lt;/div&gt;
2630 
2631 &lt;p&gt;With the style selected above, all lines have some shade of orange:&lt;/p&gt;
2632 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.16.31.7df18bbe94cb.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.16.31.7df18bbe94cb.png&quot; width=&quot;2160&quot; height=&quot;1414&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.16.31.7df18bbe94cb.png&amp;amp;w=540&amp;amp;sig=4b0439c6c48d0b3f96f411e6198f6fd49d5c7026 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.16.31.7df18bbe94cb.png&amp;amp;w=1080&amp;amp;sig=4d0aad0a27321bb321dc9c88158f357155968121 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_21.16.31.7df18bbe94cb.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Line Chart, Categories, Axis Titles and Style&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2633 &lt;p&gt;There is no clear documentation on what each style number looks like, but &lt;a href=&quot;https://1drv.ms/x/s!Asf0Y5Y4GI3Mg6kZNRd1IA09NLWv9A&quot;&gt;this spreadsheet&lt;/a&gt; has a few examples of the styles available.&lt;/p&gt;
2634 &lt;div class=&quot;card mb-3&quot; id=&quot;collapse_card0fb191&quot;&gt;
2635 &lt;div class=&quot;card-header border-0&quot;&gt;&lt;p class=&quot;m-0&quot;&gt;&lt;button class=&quot;btn&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapse0fb191&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapse0fb191&quot;&gt;Complete Code Example&lt;/button&gt; &lt;button class=&quot;btn btn-link float-right&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapse0fb191&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapse0fb191&quot;&gt;Show/Hide&lt;/button&gt;&lt;/p&gt;&lt;/div&gt;
2636 &lt;div id=&quot;collapse0fb191&quot; class=&quot;collapse&quot; data-parent=&quot;#collapse_card0fb191&quot;&gt;&lt;div class=&quot;card-body&quot; markdown=&quot;1&quot;&gt;
2637 
2638 &lt;p&gt;Here&amp;rsquo;s the full code used to generate the line chart with categories, axis titles, and style:&lt;/p&gt;
2639 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt;
2640 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;
2641 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.chart&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LineChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;
2642 
2643 &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
2644 &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
2645 
2646 &lt;span class=&quot;c1&quot;&gt;# Let&amp;#39;s create some sample sales data&lt;/span&gt;
2647 &lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
2648     &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;January&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;February&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;March&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;April&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2649     &lt;span class=&quot;s2&quot;&gt;&amp;quot;May&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;June&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;July&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;August&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;September&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2650      &lt;span class=&quot;s2&quot;&gt;&amp;quot;October&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;November&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;December&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2651     &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2652     &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2653     &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2654 &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
2655 
2656 &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
2657     &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2658 
2659 &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2660                            &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2661                            &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2662                            &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
2663     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
2664         &lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2665 
2666 &lt;span class=&quot;c1&quot;&gt;# Create a LineChart and add the main data&lt;/span&gt;
2667 &lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LineChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
2668 &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;worksheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2669                            &lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2670                            &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2671                            &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2672                            &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2673 &lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;titles_from_data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from_rows&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2674 
2675 &lt;span class=&quot;c1&quot;&gt;# Add categories to the chart&lt;/span&gt;
2676 &lt;span class=&quot;n&quot;&gt;cats&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;worksheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2677                  &lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2678                  &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2679                  &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2680                  &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2681 &lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_categories&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cats&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2682 
2683 &lt;span class=&quot;c1&quot;&gt;# Rename the X and Y Axis&lt;/span&gt;
2684 &lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Months&amp;quot;&lt;/span&gt;
2685 &lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Sales (per unit)&amp;quot;&lt;/span&gt;
2686 
2687 &lt;span class=&quot;c1&quot;&gt;# Apply a specific Style&lt;/span&gt;
2688 &lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;style&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt;
2689 
2690 &lt;span class=&quot;c1&quot;&gt;# Save!&lt;/span&gt;
2691 &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;C6&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2692 &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;line_chart.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2693 &lt;/pre&gt;&lt;/div&gt;
2694 
2695 &lt;/div&gt;&lt;/div&gt;
2696 
2697 &lt;/div&gt;
2698 &lt;p&gt;There are a lot more chart types and customization you can apply, so be sure to check out the &lt;a href=&quot;https://openpyxl.readthedocs.io/en/stable/charts/introduction.html&quot;&gt;package documentation&lt;/a&gt; on this if you need some specific formatting.&lt;/p&gt;
2699 &lt;h3 id=&quot;convert-python-classes-to-excel-spreadsheet&quot;&gt;Convert Python Classes to Excel Spreadsheet&lt;/h3&gt;
2700 &lt;p&gt;You already saw how to convert an Excel spreadsheet&amp;rsquo;s data into Python classes, but now let&amp;rsquo;s do the opposite.&lt;/p&gt;
2701 &lt;p&gt;Let&amp;rsquo;s imagine you have a database and are using some Object-Relational Mapping (ORM) to map DB objects into Python classes. Now, you want to export those same objects into a spreadsheet.&lt;/p&gt;
2702 &lt;p&gt;Let&amp;rsquo;s assume the following &lt;a href=&quot;https://realpython.com/python-data-classes/&quot;&gt;data classes&lt;/a&gt; to represent the data coming from your database regarding product sales:&lt;/p&gt;
2703 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dataclasses&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataclass&lt;/span&gt;
2704 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;typing&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;
2705 
2706 &lt;span class=&quot;nd&quot;&gt;@dataclass&lt;/span&gt;
2707 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Sale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
2708     &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
2709     &lt;span class=&quot;n&quot;&gt;quantity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;
2710 
2711 &lt;span class=&quot;nd&quot;&gt;@dataclass&lt;/span&gt;
2712 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
2713     &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
2714     &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
2715     &lt;span class=&quot;n&quot;&gt;sales&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
2716 &lt;/pre&gt;&lt;/div&gt;
2717 
2718 &lt;p&gt;Now, let&amp;rsquo;s generate some random data, assuming the above classes are stored in a &lt;code&gt;db_classes.py&lt;/code&gt; file:&lt;/p&gt;
2719 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt;
2720 &lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
2721 &lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Ignore these for now. You&amp;#39;ll use them in a sec ;)&lt;/span&gt;
2722 &lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;
2723 &lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.chart&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LineChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;
2724 &lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;
2725 &lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;db_classes&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sale&lt;/span&gt;
2726 &lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;
2727 &lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;products&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
2728 &lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;
2729 &lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Let&amp;#39;s create 5 products&lt;/span&gt;
2730 &lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
2731 &lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;sales&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
2732 &lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;
2733 &lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Create 5 months of sales&lt;/span&gt;
2734 &lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
2735 &lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;sale&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quantity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
2736 &lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;sales&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2737 &lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;
2738 &lt;span class=&quot;lineno&quot;&gt;20 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
2739 &lt;span class=&quot;lineno&quot;&gt;21 &lt;/span&gt;                      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Product &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2740 &lt;span class=&quot;lineno&quot;&gt;22 &lt;/span&gt;                      &lt;span class=&quot;n&quot;&gt;sales&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sales&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2741 &lt;span class=&quot;lineno&quot;&gt;23 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;products&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2742 &lt;/pre&gt;&lt;/div&gt;
2743 
2744 &lt;p&gt;By running this piece of code, you should get 5 products with 5 months of sales with a random quantity of sales for each month.&lt;/p&gt;
2745 &lt;p&gt;Now, to convert this into a spreadsheet, you need to iterate over the data and append it to the spreadsheet:&lt;/p&gt;
2746 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;25 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
2747 &lt;span class=&quot;lineno&quot;&gt;26 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
2748 &lt;span class=&quot;lineno&quot;&gt;27 &lt;/span&gt;
2749 &lt;span class=&quot;lineno&quot;&gt;28 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Append column names first&lt;/span&gt;
2750 &lt;span class=&quot;lineno&quot;&gt;29 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Product ID&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Product Name&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Month 1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2751 &lt;span class=&quot;lineno&quot;&gt;30 &lt;/span&gt;              &lt;span class=&quot;s2&quot;&gt;&amp;quot;Month 2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Month 3&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Month 4&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Month 5&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
2752 &lt;span class=&quot;lineno&quot;&gt;31 &lt;/span&gt;
2753 &lt;span class=&quot;lineno&quot;&gt;32 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Append the data&lt;/span&gt;
2754 &lt;span class=&quot;lineno&quot;&gt;33 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;products&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
2755 &lt;span class=&quot;lineno&quot;&gt;34 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
2756 &lt;span class=&quot;lineno&quot;&gt;35 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sale&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sales&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
2757 &lt;span class=&quot;lineno&quot;&gt;36 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sale&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quantity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2758 &lt;span class=&quot;lineno&quot;&gt;37 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2759 &lt;/pre&gt;&lt;/div&gt;
2760 
2761 &lt;p&gt;That&amp;rsquo;s it. That should allow you to create a spreadsheet with some data coming from your database.&lt;/p&gt;
2762 &lt;p&gt;However, why not use some of that cool knowledge you gained recently to add a chart as well to display that data more visually?&lt;/p&gt;
2763 &lt;p&gt;All right, then you could probably do something like this:&lt;/p&gt;
2764 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;38 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LineChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
2765 &lt;span class=&quot;lineno&quot;&gt;39 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;worksheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2766 &lt;span class=&quot;lineno&quot;&gt;40 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2767 &lt;span class=&quot;lineno&quot;&gt;41 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2768 &lt;span class=&quot;lineno&quot;&gt;42 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2769 &lt;span class=&quot;lineno&quot;&gt;43 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2770 &lt;span class=&quot;lineno&quot;&gt;44 &lt;/span&gt;
2771 &lt;span class=&quot;lineno&quot;&gt;45 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;titles_from_data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from_rows&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2772 &lt;span class=&quot;lineno&quot;&gt;46 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;B8&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2773 &lt;span class=&quot;lineno&quot;&gt;47 &lt;/span&gt;
2774 &lt;span class=&quot;lineno&quot;&gt;48 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cats&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;worksheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2775 &lt;span class=&quot;lineno&quot;&gt;49 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2776 &lt;span class=&quot;lineno&quot;&gt;50 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2777 &lt;span class=&quot;lineno&quot;&gt;51 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
2778 &lt;span class=&quot;lineno&quot;&gt;52 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2779 &lt;span class=&quot;lineno&quot;&gt;53 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_categories&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cats&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2780 &lt;span class=&quot;lineno&quot;&gt;54 &lt;/span&gt;
2781 &lt;span class=&quot;lineno&quot;&gt;55 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Months&amp;quot;&lt;/span&gt;
2782 &lt;span class=&quot;lineno&quot;&gt;56 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Sales (per unit)&amp;quot;&lt;/span&gt;
2783 &lt;span class=&quot;lineno&quot;&gt;57 &lt;/span&gt;
2784 &lt;span class=&quot;lineno&quot;&gt;58 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;oop_sample.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2785 &lt;/pre&gt;&lt;/div&gt;
2786 
2787 &lt;p&gt;Now we&amp;rsquo;re talking! Here&amp;rsquo;s a spreadsheet generated from database objects and with a chart and everything:&lt;/p&gt;
2788 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.26.23.1f355e76586d.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.26.23.1f355e76586d.png&quot; width=&quot;2160&quot; height=&quot;1414&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.26.23.1f355e76586d.png&amp;amp;w=540&amp;amp;sig=135f4ee5413467c91f65bbb6e914724cdf1fd413 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.26.23.1f355e76586d.png&amp;amp;w=1080&amp;amp;sig=5b7dd165f92c237ddd049350ca6fc6a165e10512 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_21.26.23.1f355e76586d.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Conversion from Python Data Classes&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2789 &lt;p&gt;That&amp;rsquo;s a great way for you to wrap up your new knowledge of charts!&lt;/p&gt;
2790 &lt;h3 id=&quot;bonus-working-with-pandas&quot;&gt;Bonus: Working With Pandas&lt;/h3&gt;
2791 &lt;p&gt;Even though you can use &lt;a href=&quot;https://realpython.com/working-with-large-excel-files-in-pandas/&quot;&gt;Pandas to handle Excel files&lt;/a&gt;, there are few things that you either can&amp;rsquo;t accomplish with Pandas or that you&amp;rsquo;d be better off just using &lt;code&gt;openpyxl&lt;/code&gt; directly.&lt;/p&gt;
2792 &lt;p&gt;For example, some of the advantages of using &lt;code&gt;openpyxl&lt;/code&gt; are the ability to easily customize your spreadsheet with styles, conditional formatting, and such.&lt;/p&gt;
2793 &lt;p&gt;But guess what, you don&amp;rsquo;t have to worry about picking. In fact, &lt;code&gt;openpyxl&lt;/code&gt; has support for both converting data from a Pandas DataFrame into a workbook or the opposite, converting an &lt;code&gt;openpyxl&lt;/code&gt; workbook into a Pandas DataFrame.&lt;/p&gt;
2794 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
2795 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If you&amp;rsquo;re new to Pandas, check our &lt;a href=&quot;https://realpython.com/courses/pandas-dataframes-101/&quot;&gt;course on Pandas DataFrames&lt;/a&gt; beforehand.&lt;/p&gt;
2796 &lt;/div&gt;
2797 &lt;p&gt;First things first, remember to install the &lt;code&gt;pandas&lt;/code&gt; package:&lt;/p&gt;
2798 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pip install pandas
2799 &lt;/pre&gt;&lt;/div&gt;
2800 
2801 &lt;p&gt;Then, let&amp;rsquo;s create a sample DataFrame:&lt;/p&gt;
2802 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pandas&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pd&lt;/span&gt;
2803 &lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
2804 &lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
2805 &lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;    &lt;span class=&quot;s2&quot;&gt;&amp;quot;Product Name&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Product 1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Product 2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2806 &lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;    &lt;span class=&quot;s2&quot;&gt;&amp;quot;Sales Month 1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2807 &lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;    &lt;span class=&quot;s2&quot;&gt;&amp;quot;Sales Month 2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;35&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
2808 &lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
2809 &lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2810 &lt;/pre&gt;&lt;/div&gt;
2811 
2812 &lt;p&gt;Now that you have some data, you can use &lt;code&gt;.dataframe_to_rows()&lt;/code&gt; to convert it from a DataFrame into a worksheet:&lt;/p&gt;
2813 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;
2814 &lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.utils.dataframe&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataframe_to_rows&lt;/span&gt;
2815 &lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;
2816 &lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
2817 &lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
2818 &lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;
2819 &lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataframe_to_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
2820 &lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2821 &lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;
2822 &lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;pandas.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2823 &lt;/pre&gt;&lt;/div&gt;
2824 
2825 &lt;p&gt;You should see a spreadsheet that looks like this:&lt;/p&gt;
2826 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.42.15.0a4208db25f0.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.42.15.0a4208db25f0.png&quot; width=&quot;2160&quot; height=&quot;1414&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.42.15.0a4208db25f0.png&amp;amp;w=540&amp;amp;sig=636303dd8f99512651c5868f4ef572b2afa75d3c 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.42.15.0a4208db25f0.png&amp;amp;w=1080&amp;amp;sig=c2ddf015c46566fc545ad03187cc5780a61938f9 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_21.42.15.0a4208db25f0.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Data from Pandas Data Frame&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
2827 &lt;p&gt;If you want to add the &lt;a href=&quot;https://realpython.com/python-data-cleaning-numpy-pandas/#changing-the-index-of-a-dataframe&quot;&gt;DataFrame&amp;rsquo;s index&lt;/a&gt;, you can change &lt;code&gt;index=True&lt;/code&gt;, and it adds each row&amp;rsquo;s index into your spreadsheet.&lt;/p&gt;
2828 &lt;p&gt;On the other hand, if you want to convert a spreadsheet into a DataFrame, you can also do it in a very straightforward way like so:&lt;/p&gt;
2829 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pandas&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pd&lt;/span&gt;
2830 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;
2831 
2832 &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2833 &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
2834 
2835 &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;
2836 &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2837 &lt;/pre&gt;&lt;/div&gt;
2838 
2839 &lt;p&gt;Alternatively, if you want to add the correct headers and use the review ID as the index, for example, then you can also do it like this instead:&lt;/p&gt;
2840 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pandas&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pd&lt;/span&gt;
2841 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;
2842 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;mapping&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;REVIEW_ID&lt;/span&gt;
2843 
2844 &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2845 &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
2846 
2847 &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;
2848 
2849 &lt;span class=&quot;c1&quot;&gt;# Set the first row as the columns for the DataFrame&lt;/span&gt;
2850 &lt;span class=&quot;n&quot;&gt;cols&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2851 &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2852 
2853 &lt;span class=&quot;c1&quot;&gt;# Set the field &amp;quot;review_id&amp;quot; as the indexes for each row&lt;/span&gt;
2854 &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;REVIEW_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
2855 
2856 &lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
2857 &lt;/pre&gt;&lt;/div&gt;
2858 
2859 &lt;p&gt;Using indexes and columns allows you to access data from your DataFrame easily:&lt;/p&gt;
2860 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;
2861 &lt;span class=&quot;go&quot;&gt;Index([&amp;#39;marketplace&amp;#39;, &amp;#39;customer_id&amp;#39;, &amp;#39;review_id&amp;#39;, &amp;#39;product_id&amp;#39;,&lt;/span&gt;
2862 &lt;span class=&quot;go&quot;&gt;       &amp;#39;product_parent&amp;#39;, &amp;#39;product_title&amp;#39;, &amp;#39;product_category&amp;#39;, &amp;#39;star_rating&amp;#39;,&lt;/span&gt;
2863 &lt;span class=&quot;go&quot;&gt;       &amp;#39;helpful_votes&amp;#39;, &amp;#39;total_votes&amp;#39;, &amp;#39;vine&amp;#39;, &amp;#39;verified_purchase&amp;#39;,&lt;/span&gt;
2864 &lt;span class=&quot;go&quot;&gt;       &amp;#39;review_headline&amp;#39;, &amp;#39;review_body&amp;#39;, &amp;#39;review_date&amp;#39;],&lt;/span&gt;
2865 &lt;span class=&quot;go&quot;&gt;      dtype=&amp;#39;object&amp;#39;)&lt;/span&gt;
2866 
2867 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Get first 10 reviews&amp;#39; star rating&lt;/span&gt;
2868 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;star_rating&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
2869 &lt;span class=&quot;go&quot;&gt;R3O9SGZBVQBV76    5&lt;/span&gt;
2870 &lt;span class=&quot;go&quot;&gt;RKH8BNC3L5DLF     5&lt;/span&gt;
2871 &lt;span class=&quot;go&quot;&gt;R2HLE8WKZSU3NL    2&lt;/span&gt;
2872 &lt;span class=&quot;go&quot;&gt;R31U3UH5AZ42LL    5&lt;/span&gt;
2873 &lt;span class=&quot;go&quot;&gt;R2SV659OUJ945Y    4&lt;/span&gt;
2874 &lt;span class=&quot;go&quot;&gt;RA51CP8TR5A2L     5&lt;/span&gt;
2875 &lt;span class=&quot;go&quot;&gt;RB2Q7DLDN6TH6     5&lt;/span&gt;
2876 &lt;span class=&quot;go&quot;&gt;R2RHFJV0UYBK3Y    1&lt;/span&gt;
2877 &lt;span class=&quot;go&quot;&gt;R2Z6JOQ94LFHEP    5&lt;/span&gt;
2878 &lt;span class=&quot;go&quot;&gt;RX27XIIWY5JPB     4&lt;/span&gt;
2879 &lt;span class=&quot;go&quot;&gt;Name: star_rating, dtype: int64&lt;/span&gt;
2880 
2881 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Grab review with id &amp;quot;R2EQL1V1L6E0C9&amp;quot;, using the index&lt;/span&gt;
2882 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;R2EQL1V1L6E0C9&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
2883 &lt;span class=&quot;go&quot;&gt;marketplace               US&lt;/span&gt;
2884 &lt;span class=&quot;go&quot;&gt;customer_id         15305006&lt;/span&gt;
2885 &lt;span class=&quot;go&quot;&gt;review_id     R2EQL1V1L6E0C9&lt;/span&gt;
2886 &lt;span class=&quot;go&quot;&gt;product_id        B004LURNO6&lt;/span&gt;
2887 &lt;span class=&quot;go&quot;&gt;product_parent     892860326&lt;/span&gt;
2888 &lt;span class=&quot;go&quot;&gt;review_headline   Five Stars&lt;/span&gt;
2889 &lt;span class=&quot;go&quot;&gt;review_body          Love it&lt;/span&gt;
2890 &lt;span class=&quot;go&quot;&gt;review_date       2015-08-31&lt;/span&gt;
2891 &lt;span class=&quot;go&quot;&gt;Name: R2EQL1V1L6E0C9, dtype: object&lt;/span&gt;
2892 &lt;/pre&gt;&lt;/div&gt;
2893 
2894 &lt;p&gt;There you go, whether you want to use &lt;code&gt;openpyxl&lt;/code&gt; to prettify your Pandas dataset or use Pandas to do some hardcore algebra, you now know how to switch between both packages.&lt;/p&gt;
2895 &lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
2896 &lt;p&gt;&lt;em&gt;Phew&lt;/em&gt;, after that long read, you now know how to work with spreadsheets in Python! You can rely on &lt;code&gt;openpyxl&lt;/code&gt;, your trustworthy companion, to:&lt;/p&gt;
2897 &lt;ul&gt;
2898 &lt;li&gt;Extract valuable information from spreadsheets in a Pythonic manner&lt;/li&gt;
2899 &lt;li&gt;Create your own spreadsheets, no matter the complexity level&lt;/li&gt;
2900 &lt;li&gt;Add cool features such as conditional formatting or charts to your spreadsheets&lt;/li&gt;
2901 &lt;/ul&gt;
2902 &lt;p&gt;There are a few other things you can do with &lt;code&gt;openpyxl&lt;/code&gt; that might not have been covered in this tutorial, but you can always check the package&amp;rsquo;s official &lt;a href=&quot;https://openpyxl.readthedocs.io/en/stable/index.html&quot;&gt;documentation website&lt;/a&gt; to learn more about it. You can even venture into checking its &lt;a href=&quot;https://bitbucket.org/openpyxl/openpyxl/src/default/&quot;&gt;source code&lt;/a&gt; and improving the package further.&lt;/p&gt;
2903 &lt;p&gt;Feel free to leave any comments below if you have any questions, or if there&amp;rsquo;s any section you&amp;rsquo;d love to hear more about.&lt;/p&gt;
2904 &lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Download Dataset:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/openpyxl-sample-dataset/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-openpyxl-sample-dataset&quot; data-focus=&quot;false&quot;&gt;Click here to download the dataset for the openpyxl exercise you&#39;ll be following in this tutorial.&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;
2905         &lt;hr /&gt;
2906         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
2907       </content>
2908     </entry>
2909   
2910     <entry>
2911       <title>Your Guide to the CPython Source Code</title>
2912       <id>https://realpython.com/cpython-source-code-guide/</id>
2913       <link href="https://realpython.com/cpython-source-code-guide/"/>
2914       <updated>2019-08-21T16:10:00+00:00</updated>
2915       <summary>In this detailed Python tutorial, you&#39;ll explore the CPython source code. By following this step-by-step walkthrough, you&#39;ll take a deep dive into how the CPython compiler works and how your Python code gets executed.</summary>
2916       <content type="html">
2917         &lt;p&gt;Are there certain parts of Python that just seem magic? Like how are dictionaries so much faster than looping over a list to find an item. How does a generator remember the state of the variables each time it yields a value and why do you never have to allocate memory like other languages? It turns out, CPython, the most popular Python runtime is written in human-readable C and Python code. This tutorial will walk you through the CPython source code. &lt;/p&gt;
2918 &lt;p&gt;You&amp;rsquo;ll cover all the concepts behind the internals of CPython, how they work and visual explanations as you go.&lt;/p&gt;
2919 &lt;p&gt;&lt;strong&gt;You&amp;rsquo;ll learn how to:&lt;/strong&gt;&lt;/p&gt;
2920 &lt;ul&gt;
2921 &lt;li&gt;Read and navigate the source code&lt;/li&gt;
2922 &lt;li&gt;Compile CPython from source code&lt;/li&gt;
2923 &lt;li&gt;Navigate and comprehend the inner workings of concepts like lists, dictionaries, and generators&lt;/li&gt;
2924 &lt;li&gt;Run the test suite&lt;/li&gt;
2925 &lt;li&gt;Modify or upgrade components of the CPython library to contribute them to future versions&lt;/li&gt;
2926 &lt;/ul&gt;
2927 &lt;p&gt;Yes, this is a very long article. If you just made yourself a fresh cup of tea, coffee or your favorite beverage, it&amp;rsquo;s going to be cold by the end of Part 1. &lt;/p&gt;
2928 &lt;p&gt;This tutorial is split into five parts. Take your time for each part and make sure you try out the demos and the interactive components. You can feel a sense of achievement that you grasp the core concepts of Python that can make you a better Python programmer.&lt;/p&gt;
2929 &lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Free Bonus:&lt;/strong&gt; &lt;a href=&quot;&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-mastery-course&quot; data-focus=&quot;false&quot;&gt;5 Thoughts On Python Mastery&lt;/a&gt;, a free course for Python developers that shows you the roadmap and the mindset you&#39;ll need to take your Python skills to the next level.&lt;/p&gt;&lt;/div&gt;
2930 
2931 &lt;h2 h1=&quot;h1&quot; id=&quot;part-1-introduction-to-cpython&quot;&gt;Part 1: Introduction to CPython&lt;/h2&gt;
2932 &lt;p&gt;When you type &lt;code&gt;python&lt;/code&gt; at the console or install a Python distribution from &lt;a href=&quot;https://www.python.org&quot;&gt;python.org&lt;/a&gt;, you are running &lt;strong&gt;CPython&lt;/strong&gt;. CPython is one of the many Python runtimes, maintained and written by different teams of developers. Some other runtimes you may have heard are &lt;a href=&quot;https://pypy.org/&quot;&gt;PyPy&lt;/a&gt;, &lt;a href=&quot;https://cython.org/&quot;&gt;Cython&lt;/a&gt;, and &lt;a href=&quot;https://www.jython.org/&quot;&gt;Jython&lt;/a&gt;.&lt;/p&gt;
2933 &lt;p&gt;The unique thing about CPython is that it contains both a runtime and the shared language specification that all Python runtimes use. CPython is the &amp;ldquo;official,&amp;rdquo; or reference implementation of Python.&lt;/p&gt;
2934 &lt;p&gt;The Python language specification is the document that the description of the Python language. For example, it says that &lt;code&gt;assert&lt;/code&gt; is a reserved keyword, and that &lt;code&gt;[]&lt;/code&gt; is used for indexing, slicing, and creating empty lists.&lt;/p&gt;
2935 &lt;p&gt;Think about what you expect to be inside the Python distribution on your computer:&lt;/p&gt;
2936 &lt;ul&gt;
2937 &lt;li&gt;When you type &lt;code&gt;python&lt;/code&gt; without a file or module, it gives an interactive prompt.&lt;/li&gt;
2938 &lt;li&gt;You can import built-in modules from the standard library like &lt;code&gt;json&lt;/code&gt;.&lt;/li&gt;
2939 &lt;li&gt;You can install packages from the internet using &lt;code&gt;pip&lt;/code&gt;.&lt;/li&gt;
2940 &lt;li&gt;You can test your applications using the built-in &lt;code&gt;unittest&lt;/code&gt; library.&lt;/li&gt;
2941 &lt;/ul&gt;
2942 &lt;p&gt;These are all part of the CPython distribution. There&amp;rsquo;s a lot more than just a compiler.&lt;/p&gt;
2943 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
2944 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This article is written against version &lt;a href=&quot;https://github.com/python/cpython/tree/v3.8.0b4&quot;&gt;3.8.0b4&lt;/a&gt; of the CPython source code.&lt;/p&gt;
2945 &lt;/div&gt;
2946 &lt;h3 id=&quot;whats-in-the-source-code&quot;&gt;What&amp;rsquo;s in the Source Code?&lt;/h3&gt;
2947 &lt;p&gt;The CPython source distribution comes with a whole range of tools, libraries, and components. We&amp;rsquo;ll explore those in this article. First we are going to focus on the compiler.&lt;/p&gt;
2948 &lt;p&gt;To download a copy of the CPython source code, you can use &lt;code&gt;git&lt;/code&gt; to pull the latest version to a working copy locally:&lt;/p&gt;
2949 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;git clone https://github.com/python/cpython&lt;/span&gt;
2950 &lt;span class=&quot;go&quot;&gt;cd cpython&lt;/span&gt;
2951 &lt;span class=&quot;go&quot;&gt;git checkout v3.8.0b4&lt;/span&gt;
2952 &lt;/pre&gt;&lt;/div&gt;
2953 
2954 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
2955 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If you don&amp;rsquo;t have Git available, you can download the source in a &lt;a href=&quot;https://github.com/python/cpython/archive/v3.8.0b4.zip&quot;&gt;ZIP&lt;/a&gt; file directly from the GitHub website.&lt;/p&gt;
2956 &lt;/div&gt;
2957 &lt;p&gt;Inside of the newly downloaded &lt;code&gt;cpython&lt;/code&gt; directory, you will find the following subdirectories:&lt;/p&gt;
2958 &lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;cpython/
2959 โ”‚
2960 โ”œโ”€โ”€ Doc      โ† Source for the documentation
2961 โ”œโ”€โ”€ Grammar  โ† The computer-readable language definition
2962 โ”œโ”€โ”€ Include  โ† The C header files
2963 โ”œโ”€โ”€ Lib      โ† Standard library modules written in Python
2964 โ”œโ”€โ”€ Mac      โ† macOS support files
2965 โ”œโ”€โ”€ Misc     โ† Miscellaneous files
2966 โ”œโ”€โ”€ Modules  โ† Standard Library Modules written in C
2967 โ”œโ”€โ”€ Objects  โ† Core types and the object model
2968 โ”œโ”€โ”€ Parser   โ† The Python parser source code
2969 โ”œโ”€โ”€ PC       โ† Windows build support files
2970 โ”œโ”€โ”€ PCbuild  โ† Windows build support files for older Windows versions
2971 โ”œโ”€โ”€ Programs โ† Source code for the python executable and other binaries
2972 โ”œโ”€โ”€ Python   โ† The CPython interpreter source code
2973 โ””โ”€โ”€ Tools    โ† Standalone tools useful for building or extending Python
2974 &lt;/pre&gt;&lt;/div&gt;
2975 
2976 &lt;p&gt;Next, we&amp;rsquo;ll compile CPython from the source code. This step requires a C compiler, and some build tools, which depend on the operating system you&amp;rsquo;re using.&lt;/p&gt;
2977 &lt;h3 id=&quot;compiling-cpython-macos&quot;&gt;Compiling CPython (macOS)&lt;/h3&gt;
2978 &lt;p&gt;Compiling CPython on macOS is straightforward. You will first need the essential C compiler toolkit. The Command Line Development Tools is an app that you can update in macOS through the App Store. You need to perform the initial installation on the terminal.&lt;/p&gt;
2979 &lt;p&gt;To open up a terminal in macOS, go to the Launchpad, then &lt;em&gt;Other&lt;/em&gt; then choose the &lt;em&gt;Terminal&lt;/em&gt; app. You will want to save this app to your Dock, so right-click the Icon and select &lt;em&gt;Keep in Dock&lt;/em&gt;.&lt;/p&gt;
2980 &lt;p&gt;Now, within the terminal, install the C compiler and toolkit by running the following:&lt;/p&gt;
2981 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; xcode-select --install
2982 &lt;/pre&gt;&lt;/div&gt;
2983 
2984 &lt;p&gt;This command will pop up with a prompt to download and install a set of tools, including Git, Make, and the GNU C compiler.&lt;/p&gt;
2985 &lt;p&gt;You will also need a working copy of &lt;a href=&quot;https://www.openssl.org/&quot;&gt;OpenSSL&lt;/a&gt; to use for fetching packages from the PyPi.org website. If you later plan on using this build to install additional packages, SSL validation is required.&lt;/p&gt;
2986 &lt;p&gt;The simplest way to install OpenSSL on macOS is by using &lt;a href=&quot;https://brew.sh&quot;&gt;HomeBrew&lt;/a&gt;. If you already have HomeBrew installed, you can install the dependencies for CPython with the &lt;code&gt;brew install&lt;/code&gt; command:&lt;/p&gt;
2987 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; brew install openssl xz zlib
2988 &lt;/pre&gt;&lt;/div&gt;
2989 
2990 &lt;p&gt;Now that you have the dependencies, you can run the &lt;code&gt;configure&lt;/code&gt; script, enabling SSL support by discovering the location that HomeBrew installed to and enabling the debug hooks &lt;code&gt;--with-pydebug&lt;/code&gt;:&lt;/p&gt;
2991 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;CPPFLAGS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;-I&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;brew --prefix zlib&lt;span class=&quot;k&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/include&amp;quot;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
2992  &lt;span class=&quot;nv&quot;&gt;LDFLAGS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;-L&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;brew --prefix zlib&lt;span class=&quot;k&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/lib&amp;quot;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
2993  ./configure --with-openssl&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;brew --prefix openssl&lt;span class=&quot;k&quot;&gt;)&lt;/span&gt; --with-pydebug
2994 &lt;/pre&gt;&lt;/div&gt;
2995 
2996 &lt;p&gt;This will generate a &lt;code&gt;Makefile&lt;/code&gt; in the root of the repository that you can use to automate the build process. The &lt;code&gt;./configure&lt;/code&gt; step only needs to be run once. You can build the CPython binary by running:&lt;/p&gt;
2997 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; make -j2 -s
2998 &lt;/pre&gt;&lt;/div&gt;
2999 
3000 &lt;p&gt;The &lt;code&gt;-j2&lt;/code&gt; flag allows &lt;code&gt;make&lt;/code&gt; to run 2 jobs simultaneously. If you have 4 cores, you can change this to 4. The &lt;code&gt;-s&lt;/code&gt; flag stops the &lt;code&gt;Makefile&lt;/code&gt; from printing every command it runs to the console. You can remove this, but the output is very verbose.&lt;/p&gt;
3001 &lt;p&gt;During the build, you may receive some errors, and in the summary, it will notify you that not all packages could be built. For example, &lt;code&gt;_dbm&lt;/code&gt;, &lt;code&gt;_sqlite3&lt;/code&gt;, &lt;code&gt;_uuid&lt;/code&gt;, &lt;code&gt;nis&lt;/code&gt;, &lt;code&gt;ossaudiodev&lt;/code&gt;, &lt;code&gt;spwd&lt;/code&gt;, and &lt;code&gt;_tkinter&lt;/code&gt; would fail to build with this set of instructions. That&amp;rsquo;s okay if you aren&amp;rsquo;t planning on developing against those packages. If you are, then check out the &lt;a href=&quot;https://devguide.python.org/&quot;&gt;dev guide&lt;/a&gt; website for more information.&lt;/p&gt;
3002 &lt;p&gt;The build will take a few minutes and generate a binary called &lt;code&gt;python.exe&lt;/code&gt;.  Every time you make changes to the source code, you will need to re-run &lt;code&gt;make&lt;/code&gt; with the same flags.
3003 The &lt;code&gt;python.exe&lt;/code&gt; binary is the debug binary of CPython. Execute &lt;code&gt;python.exe&lt;/code&gt; to see a working REPL:&lt;/p&gt;
3004 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; ./python.exe
3005 &lt;span class=&quot;go&quot;&gt;Python 3.8.0b4 (tags/v3.8.0b4:d93605de72, Aug 30 2019, 10:00:03) &lt;/span&gt;
3006 &lt;span class=&quot;go&quot;&gt;[Clang 10.0.1 (clang-1001.0.46.4)] on darwin&lt;/span&gt;
3007 &lt;span class=&quot;go&quot;&gt;Type &amp;quot;help&amp;quot;, &amp;quot;copyright&amp;quot;, &amp;quot;credits&amp;quot; or &amp;quot;license&amp;quot; for more information.&lt;/span&gt;
3008 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&lt;/span&gt;&amp;gt;&amp;gt; 
3009 &lt;/pre&gt;&lt;/div&gt;
3010 
3011 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
3012 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; 
3013 Yes, that&amp;rsquo;s right, the macOS build has a file extension for &lt;code&gt;.exe&lt;/code&gt;. This is &lt;em&gt;not&lt;/em&gt; because it&amp;rsquo;s a Windows binary. Because macOS has a case-insensitive filesystem and when working with the binary, the developers didn&amp;rsquo;t want people to accidentally refer to the directory &lt;code&gt;Python/&lt;/code&gt; so &lt;code&gt;.exe&lt;/code&gt; was appended to avoid ambiguity.
3014 If you later run &lt;code&gt;make install&lt;/code&gt; or &lt;code&gt;make altinstall&lt;/code&gt;, it will rename the file back to &lt;code&gt;python&lt;/code&gt;.&lt;/p&gt;
3015 &lt;/div&gt;
3016 &lt;h3 id=&quot;compiling-cpython-linux&quot;&gt;Compiling CPython (Linux)&lt;/h3&gt;
3017 &lt;p&gt;For Linux, the first step is to download and install &lt;code&gt;make&lt;/code&gt;, &lt;code&gt;gcc&lt;/code&gt;, &lt;code&gt;configure&lt;/code&gt;, and &lt;code&gt;pkgconfig&lt;/code&gt;. &lt;/p&gt;
3018 &lt;p&gt;For Fedora Core, RHEL, CentOS, or other yum-based systems: &lt;/p&gt;
3019 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; sudo yum install yum-utils
3020 &lt;/pre&gt;&lt;/div&gt;
3021 
3022 &lt;p&gt;For Debian, Ubuntu, or other &lt;code&gt;apt&lt;/code&gt;-based systems:&lt;/p&gt;
3023 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; sudo apt install build-essential
3024 &lt;/pre&gt;&lt;/div&gt;
3025 
3026 &lt;p&gt;Then install the required packages, for Fedora Core, RHEL, CentOS or other yum-based systems: &lt;/p&gt;
3027 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; sudo yum-builddep python3
3028 &lt;/pre&gt;&lt;/div&gt;
3029 
3030 &lt;p&gt;For Debian, Ubuntu, or other &lt;code&gt;apt&lt;/code&gt;-based systems:&lt;/p&gt;
3031 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; sudo apt install libssl-dev zlib1g-dev libncurses5-dev &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
3032   libncursesw5-dev libreadline-dev libsqlite3-dev libgdbm-dev &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
3033   libdb5.3-dev libbz2-dev libexpat1-dev liblzma-dev libffi-dev
3034 &lt;/pre&gt;&lt;/div&gt;
3035 
3036 &lt;p&gt;Now that you have the dependencies, you can run the &lt;code&gt;configure&lt;/code&gt; script, enabling the debug hooks &lt;code&gt;--with-pydebug&lt;/code&gt;:&lt;/p&gt;
3037 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; ./configure --with-pydebug
3038 &lt;/pre&gt;&lt;/div&gt;
3039 
3040 &lt;p&gt;Review the output to ensure that OpenSSL support was marked as &lt;code&gt;YES&lt;/code&gt;. Otherwise, check with your distribution for instructions on installing the headers for OpenSSL.&lt;/p&gt;
3041 &lt;p&gt;Next, you can build the CPython binary by running the generated &lt;code&gt;Makefile&lt;/code&gt;:&lt;/p&gt;
3042 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; make -j2 -s
3043 &lt;/pre&gt;&lt;/div&gt;
3044 
3045 &lt;p&gt;During the build, you may receive some errors, and in the summary, it will notify you that not all packages could be built. That&amp;rsquo;s okay if you aren&amp;rsquo;t planning on developing against those packages. If you are, then check out the &lt;a href=&quot;https://devguide.python.org/&quot;&gt;dev guide&lt;/a&gt; website for more information.&lt;/p&gt;
3046 &lt;p&gt;The build will take a few minutes and generate a binary called &lt;code&gt;python&lt;/code&gt;. This is the debug binary of CPython. Execute &lt;code&gt;./python&lt;/code&gt; to see a working REPL:&lt;/p&gt;
3047 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; ./python
3048 &lt;span class=&quot;go&quot;&gt;Python 3.8.0b4 (tags/v3.8.0b4:d93605de72, Aug 30 2019, 10:00:03) &lt;/span&gt;
3049 &lt;span class=&quot;go&quot;&gt;[Clang 10.0.1 (clang-1001.0.46.4)] on darwin&lt;/span&gt;
3050 &lt;span class=&quot;go&quot;&gt;Type &amp;quot;help&amp;quot;, &amp;quot;copyright&amp;quot;, &amp;quot;credits&amp;quot; or &amp;quot;license&amp;quot; for more information.&lt;/span&gt;
3051 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&lt;/span&gt;&amp;gt;&amp;gt; 
3052 &lt;/pre&gt;&lt;/div&gt;
3053 
3054 &lt;h3 id=&quot;compiling-cpython-windows&quot;&gt;Compiling CPython (Windows)&lt;/h3&gt;
3055 &lt;p&gt;Inside the PC folder is a Visual Studio project file for building and exploring CPython. To use this, you need to have Visual Studio installed on your PC.&lt;/p&gt;
3056 &lt;p&gt;The newest version of Visual Studio, Visual Studio 2019, makes it easier to work with Python and the CPython source code, so it is recommended for use in this tutorial. If you already have Visual Studio 2017 installed, that would also work fine.&lt;/p&gt;
3057 &lt;p&gt;None of the paid features are required for compiling CPython or this tutorial. You can use the Community edition of Visual Studio, which is available for free from &lt;a href=&quot;https://visualstudio.microsoft.com/vs/&quot;&gt;Microsoft&amp;rsquo;s Visual Studio website&lt;/a&gt;.&lt;/p&gt;
3058 &lt;p&gt;Once you&amp;rsquo;ve downloaded the installer, you&amp;rsquo;ll be asked to select which components you want to install. The bare minimum for this tutorial is:&lt;/p&gt;
3059 &lt;ul&gt;
3060 &lt;li&gt;The &lt;strong&gt;Python Development&lt;/strong&gt; workload&lt;/li&gt;
3061 &lt;li&gt;The optional &lt;strong&gt;Python native development tools&lt;/strong&gt;&lt;/li&gt;
3062 &lt;li&gt;Python 3 64-bit (3.7.2) (can be deselected if you already have Python 3.7 installed)&lt;/li&gt;
3063 &lt;/ul&gt;
3064 &lt;p&gt;Any other optional features can be deselected if you want to be more conscientious with disk space:&lt;/p&gt;
3065 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screen_Shot_2019-08-22_at_2.47.23_pm.5e8682a89503.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/Screen_Shot_2019-08-22_at_2.47.23_pm.5e8682a89503.png&quot; width=&quot;2504&quot; height=&quot;1260&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-08-22_at_2.47.23_pm.5e8682a89503.png&amp;amp;w=626&amp;amp;sig=86eb9f82580a69f533983087ba0fa4faf0d5bf96 626w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-08-22_at_2.47.23_pm.5e8682a89503.png&amp;amp;w=1252&amp;amp;sig=fe2157486f81073eabe043b3c441af23dd67b78a 1252w, https://files.realpython.com/media/Screen_Shot_2019-08-22_at_2.47.23_pm.5e8682a89503.png 2504w&quot; sizes=&quot;75vw&quot; alt=&quot;Visual Studio Options Window&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
3066 &lt;p&gt;The installer will then download and install all of the required components. The installation could take an hour, so you may want to read on and come back to this section.&lt;/p&gt;
3067 &lt;p&gt;Once the installer has completed, click the &lt;em&gt;Launch&lt;/em&gt; button to start Visual Studio. You will be prompted to sign in. If you have a Microsoft account you can log in, or skip that step.&lt;/p&gt;
3068 &lt;p&gt;Once Visual Studio starts, you will be prompted to Open a Project. A shortcut to getting started with the Git configuration and cloning CPython is to choose the &lt;em&gt;Clone or check out code&lt;/em&gt; option:&lt;/p&gt;
3069 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Capture3.e19765d74ec4.PNG&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-50&quot; src=&quot;https://files.realpython.com/media/Capture3.e19765d74ec4.PNG&quot; width=&quot;2048&quot; height=&quot;1420&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Capture3.e19765d74ec4.PNG&amp;amp;w=512&amp;amp;sig=e475e2a09cd780894f108850f91736c53ac95f27 512w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Capture3.e19765d74ec4.PNG&amp;amp;w=1024&amp;amp;sig=47d66a316c2546c9787e5af4cb4a9a006dca936d 1024w, https://files.realpython.com/media/Capture3.e19765d74ec4.PNG 2048w&quot; sizes=&quot;75vw&quot; alt=&quot;Choosing a Project Type in Visual Studio&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
3070 &lt;p&gt;For the project URL, type &lt;code&gt;https://github.com/python/cpython&lt;/code&gt; to clone:&lt;/p&gt;
3071 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Capture4.ea01418a971c.PNG&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-50&quot; src=&quot;https://files.realpython.com/media/Capture4.ea01418a971c.PNG&quot; width=&quot;2048&quot; height=&quot;1420&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Capture4.ea01418a971c.PNG&amp;amp;w=512&amp;amp;sig=47ed81b234652446a4f0e77e2a3f70e0074ac222 512w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Capture4.ea01418a971c.PNG&amp;amp;w=1024&amp;amp;sig=de393823fe557847f295d040573bd061b2ccd557 1024w, https://files.realpython.com/media/Capture4.ea01418a971c.PNG 2048w&quot; sizes=&quot;75vw&quot; alt=&quot;Cloning projects in Visual Studio&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
3072 &lt;p&gt;Visual Studio will then download a copy of CPython from GitHub using the version of Git bundled with Visual Studio. This step also saves you the hassle of having to install Git on Windows. The download may take 10 minutes.&lt;/p&gt;
3073 &lt;p&gt;Once the project has downloaded, you need to point it to the &lt;strong&gt;&lt;code&gt;pcbuild&lt;/code&gt;&lt;/strong&gt; Solution file, by clicking on &lt;em&gt;Solutions and Projects&lt;/em&gt; and selecting &lt;code&gt;pcbuild.sln&lt;/code&gt;:&lt;/p&gt;
3074 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Capture6.3d06a62b8e87.PNG&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border w-50&quot; src=&quot;https://files.realpython.com/media/Capture6.3d06a62b8e87.PNG&quot; width=&quot;863&quot; height=&quot;565&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Capture6.3d06a62b8e87.PNG&amp;amp;w=215&amp;amp;sig=0d4c23758a58848ea65b74514aca9e15a72748fa 215w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Capture6.3d06a62b8e87.PNG&amp;amp;w=431&amp;amp;sig=05f0ebe121ef54abf51bcdb55fba695d28656868 431w, https://files.realpython.com/media/Capture6.3d06a62b8e87.PNG 863w&quot; sizes=&quot;75vw&quot; alt=&quot;Selecting a solution&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
3075 &lt;p&gt;When the solution is loaded, it will prompt you to retarget the project&amp;rsquo;s inside the solution to the version of the C/C++ compiler you have installed. Visual Studio will also target the version of the Windows SDK you have installed.&lt;/p&gt;
3076 &lt;p&gt;Ensure that you change the Windows SDK version to the newest installed version and the platform toolset to the latest version. If you missed this window, you can right-click on the Solution in the &lt;em&gt;Solutions and Projects&lt;/em&gt; window and click &lt;em&gt;Retarget Solution&lt;/em&gt;.&lt;/p&gt;
3077 &lt;p&gt;Once this is complete, you need to download some source files to be able to build the whole CPython package. Inside the &lt;code&gt;PCBuild&lt;/code&gt; folder there is a &lt;code&gt;.bat&lt;/code&gt; file that automates this for you. &lt;a href=&quot;https://www.youtube.com/watch?v=bgSSJQolR0E&quot;&gt;Open up a command-line prompt inside&lt;/a&gt; the downloaded &lt;code&gt;PCBuild&lt;/code&gt; and run &lt;code&gt;get_externals.bat&lt;/code&gt;:&lt;/p&gt;
3078 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt; &amp;gt; get_externals.bat&lt;/span&gt;
3079 &lt;span class=&quot;go&quot;&gt;Using py -3.7 (found 3.7 with py.exe)&lt;/span&gt;
3080 &lt;span class=&quot;go&quot;&gt;Fetching external libraries...&lt;/span&gt;
3081 &lt;span class=&quot;go&quot;&gt;Fetching bzip2-1.0.6...&lt;/span&gt;
3082 &lt;span class=&quot;go&quot;&gt;Fetching sqlite-3.21.0.0...&lt;/span&gt;
3083 &lt;span class=&quot;go&quot;&gt;Fetching xz-5.2.2...&lt;/span&gt;
3084 &lt;span class=&quot;go&quot;&gt;Fetching zlib-1.2.11...&lt;/span&gt;
3085 &lt;span class=&quot;go&quot;&gt;Fetching external binaries...&lt;/span&gt;
3086 &lt;span class=&quot;go&quot;&gt;Fetching openssl-bin-1.1.0j...&lt;/span&gt;
3087 &lt;span class=&quot;go&quot;&gt;Fetching tcltk-8.6.9.0...&lt;/span&gt;
3088 &lt;span class=&quot;go&quot;&gt;Finished.&lt;/span&gt;
3089 &lt;/pre&gt;&lt;/div&gt;
3090 
3091 &lt;p&gt;Next, back within Visual Studio, build CPython by pressing &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-b&quot;&gt;B&lt;/kbd&gt;&lt;/span&gt;, or choosing &lt;em&gt;Build Solution&lt;/em&gt; from the top menu. If you receive any errors about the Windows SDK being missing, make sure you set the right targeting settings in the &lt;em&gt;Retarget Solution&lt;/em&gt; window. You should also see &lt;em&gt;Windows Kits&lt;/em&gt; inside your Start Menu, and &lt;em&gt;Windows Software Development Kit&lt;/em&gt; inside of that menu.&lt;/p&gt;
3092 &lt;p&gt;The build stage could take 10 minutes or more for the first time. Once the build is completed, you may see a few warnings that you can ignore and eventual completion.&lt;/p&gt;
3093 &lt;p&gt;To start the debug version of CPython, press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-f5&quot;&gt;F5&lt;/kbd&gt;&lt;/span&gt; and CPython will start in Debug mode straight into the REPL:&lt;/p&gt;
3094 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Capture8.967a3606daf0.PNG&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Capture8.967a3606daf0.PNG&quot; width=&quot;3360&quot; height=&quot;2100&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Capture8.967a3606daf0.PNG&amp;amp;w=840&amp;amp;sig=1822fde8ffe6946fc91e47dd8975aa23add8b23a 840w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Capture8.967a3606daf0.PNG&amp;amp;w=1680&amp;amp;sig=a093e68320ad548384c585840002e4294ab4bf94 1680w, https://files.realpython.com/media/Capture8.967a3606daf0.PNG 3360w&quot; sizes=&quot;75vw&quot; alt=&quot;CPython debugging Windows&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
3095 &lt;p&gt;Once this is completed, you can run the Release build by changing the build configuration from &lt;em&gt;Debug&lt;/em&gt; to &lt;em&gt;Release&lt;/em&gt; on the top menu bar and rerunning Build Solution again.
3096 You now have both Debug and Release versions of the CPython binary within &lt;code&gt;PCBuild\win32\&lt;/code&gt;.&lt;/p&gt;
3097 &lt;p&gt;You can set up Visual Studio to be able to open a REPL with either the Release or Debug build by choosing &lt;em&gt;&lt;code&gt;Tools&lt;/code&gt;-&amp;gt;&lt;code&gt;Python&lt;/code&gt;-&amp;gt;&lt;code&gt;Python Environments&lt;/code&gt;&lt;/em&gt; from the top menu:&lt;/p&gt;
3098 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Environments.96a819ecf0b3.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Environments.96a819ecf0b3.png&quot; width=&quot;3360&quot; height=&quot;2033&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Environments.96a819ecf0b3.png&amp;amp;w=840&amp;amp;sig=f8dd9b3b31d44c25cbe06d56078b0462cb0fa753 840w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Environments.96a819ecf0b3.png&amp;amp;w=1680&amp;amp;sig=e8ca69be87363b62ccda44bcf53bedd4e4e320c2 1680w, https://files.realpython.com/media/Environments.96a819ecf0b3.png 3360w&quot; sizes=&quot;75vw&quot; alt=&quot;Choosing Python environments&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
3099 &lt;p&gt;Then click &lt;em&gt;Add Environment&lt;/em&gt; and then target the Debug or Release binary. The Debug binary will end in &lt;code&gt;_d.exe&lt;/code&gt;, for example, &lt;code&gt;python_d.exe&lt;/code&gt; and &lt;code&gt;pythonw_d.exe&lt;/code&gt;. You will most likely want to use the debug binary as it comes with Debugging support in Visual Studio and will be useful for this tutorial.&lt;/p&gt;
3100 &lt;p&gt;In the Add Environment window, target the &lt;code&gt;python_d.exe&lt;/code&gt; file as the interpreter inside the &lt;code&gt;PCBuild/win32&lt;/code&gt; and the &lt;code&gt;pythonw_d.exe&lt;/code&gt; as the windowed interpreter:&lt;/p&gt;
3101 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/environment3.d33858c1f6aa.PNG&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/environment3.d33858c1f6aa.PNG&quot; width=&quot;2048&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/environment3.d33858c1f6aa.PNG&amp;amp;w=512&amp;amp;sig=ffc6b359ac60d689f40f98233466cef35354d239 512w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/environment3.d33858c1f6aa.PNG&amp;amp;w=1024&amp;amp;sig=d881ce7ecabc62624fb2a5154102a5be2ff6a4db 1024w, https://files.realpython.com/media/environment3.d33858c1f6aa.PNG 2048w&quot; sizes=&quot;75vw&quot; alt=&quot;Adding an environment in VS2019&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
3102 &lt;p&gt;Now, you can start a REPL session by clicking &lt;em&gt;Open Interactive Window&lt;/em&gt; in the Python Environments window and you will see the REPL for the compiled version of Python:&lt;/p&gt;
3103 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/environment4.7c9eade3b74e.PNG&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/environment4.7c9eade3b74e.PNG&quot; width=&quot;3360&quot; height=&quot;2033&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/environment4.7c9eade3b74e.PNG&amp;amp;w=840&amp;amp;sig=9e384e72bcfdebb39fe6dc23f21c239a0895ad51 840w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/environment4.7c9eade3b74e.PNG&amp;amp;w=1680&amp;amp;sig=be56aac5b9baffc9b8b0de4701527954ed32ef53 1680w, https://files.realpython.com/media/environment4.7c9eade3b74e.PNG 3360w&quot; sizes=&quot;75vw&quot; alt=&quot;Python Environment REPL&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
3104 &lt;p&gt;During this tutorial there will be REPL sessions with example commands. I encourage you to use the Debug binary to run these REPL sessions in case you want to put in any breakpoints within the code.&lt;/p&gt;
3105 &lt;p&gt;Lastly, to make it easier to navigate the code, in the Solution View, click on the toggle button next to the Home icon to switch to Folder view:&lt;/p&gt;
3106 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/environments5.6462694398e3.6fb872a5f57d.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/environments5.6462694398e3.6fb872a5f57d.png&quot; width=&quot;1231&quot; height=&quot;692&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/environments5.6462694398e3.6fb872a5f57d.png&amp;amp;w=307&amp;amp;sig=14bf2b50bd86dfabedc3ee1ec75a60ebccd574c4 307w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/environments5.6462694398e3.6fb872a5f57d.png&amp;amp;w=615&amp;amp;sig=0d5644853d8c429ccc78fc1bced16d8f571f555d 615w, https://files.realpython.com/media/environments5.6462694398e3.6fb872a5f57d.png 1231w&quot; sizes=&quot;75vw&quot; alt=&quot;Switching Environment Mode&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
3107 &lt;p&gt;Now you have a version of CPython compiled and ready to go, let&amp;rsquo;s find out how the CPython compiler works.&lt;/p&gt;
3108 &lt;h3 id=&quot;what-does-a-compiler-do&quot;&gt;What Does a Compiler Do?&lt;/h3&gt;
3109 &lt;p&gt;The purpose of a compiler is to convert one language into another. Think of a compiler like a translator. You would hire a translator to listen to you speaking in English and then speak in Japanese:&lt;/p&gt;
3110 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/t.38be306a7e83.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-75&quot; src=&quot;https://files.realpython.com/media/t.38be306a7e83.png&quot; width=&quot;960&quot; height=&quot;540&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/t.38be306a7e83.png&amp;amp;w=240&amp;amp;sig=2ad6eec49af1eaba79b83c099925e01a970d5efd 240w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/t.38be306a7e83.png&amp;amp;w=480&amp;amp;sig=033587107c8dceca7bcc292527a4c815fffb0b8d 480w, https://files.realpython.com/media/t.38be306a7e83.png 960w&quot; sizes=&quot;75vw&quot; alt=&quot;Translating from English to Japanese&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
3111 &lt;p&gt;Some compilers will compile into a low-level machine code which can be executed directly on a system. Other compilers will compile into an intermediary language, to be executed by a virtual machine.&lt;/p&gt;
3112 &lt;p&gt;One important decision to make when choosing a compiler is the system portability requirements. &lt;a href=&quot;https://en.wikipedia.org/wiki/Java_bytecode&quot;&gt;Java&lt;/a&gt; and &lt;a href=&quot;https://en.wikipedia.org/wiki/Common_Language_Runtime&quot;&gt;.NET CLR&lt;/a&gt; will compile into an Intermediary Language so that the compiled code is portable across multiple systems architectures. C, Go, C++, and Pascal will compile into a low-level executable that will only work on systems similar to the one it was compiled. &lt;/p&gt;
3113 &lt;p&gt;Because Python applications are typically distributed as source code, the role of the Python runtime is to convert the Python source code and execute it in one step. Internally, the CPython runtime does compile your code. A popular misconception is that Python is an interpreted language. It is actually compiled.&lt;/p&gt;
3114 &lt;p&gt;Python code is not compiled into machine-code. It is compiled into a special low-level intermediary language called &lt;strong&gt;bytecode&lt;/strong&gt; that only CPython understands. This code is stored in &lt;code&gt;.pyc&lt;/code&gt; files in a hidden directory and cached for execution. If you run the same Python application twice without changing the source code, it&amp;rsquo;ll always be much faster the second time. This is because it loads the compiled bytecode and executes it directly.&lt;/p&gt;
3115 &lt;h3 id=&quot;why-is-cpython-written-in-c-and-not-python&quot;&gt;Why Is CPython Written in C and Not Python?&lt;/h3&gt;
3116 &lt;p&gt;The &lt;strong&gt;C&lt;/strong&gt; in CPython is a reference to the C programming language, implying that this Python distribution is written in the C language.&lt;/p&gt;
3117 &lt;p&gt;This statement is largely true: the compiler in CPython is written in pure C. However, many of the standard library modules are written in pure Python or a combination of C and Python.&lt;/p&gt;
3118 &lt;p&gt;&lt;strong&gt;So why is CPython written in C and not Python?&lt;/strong&gt;&lt;/p&gt;
3119 &lt;p&gt;The answer is located in how compilers work. There are two types of compiler:&lt;/p&gt;
3120 &lt;ol&gt;
3121 &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Self-hosting&quot;&gt;Self-hosted compilers&lt;/a&gt;&lt;/strong&gt; are compilers written in the language they compile, such as the Go compiler.&lt;/li&gt;
3122 &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Source-to-source_compiler&quot;&gt;Source-to-source compilers&lt;/a&gt;&lt;/strong&gt; are compilers written in another language that already have a compiler.&lt;/li&gt;
3123 &lt;/ol&gt;
3124 &lt;p&gt;If you&amp;rsquo;re writing a new programming language from scratch, you need an executable application to compile your compiler! You need a compiler to execute anything, so when new languages are developed, they&amp;rsquo;re often written first in an older, more established language.&lt;/p&gt;
3125 &lt;p&gt;A good example would be the Go programming language. The first Go compiler was written in C, then once Go could be compiled, the compiler was rewritten in Go. &lt;/p&gt;
3126 &lt;p&gt;CPython kept its C heritage: many of the standard library modules, like the &lt;code&gt;ssl&lt;/code&gt; module or the &lt;code&gt;sockets&lt;/code&gt; module, are written in C to access low-level operating system APIs.
3127 The APIs in the Windows and Linux kernels for &lt;a href=&quot;https://realpython.com/python-sockets/&quot;&gt;creating network sockets&lt;/a&gt;, &lt;a href=&quot;https://realpython.com/working-with-files-in-python/&quot;&gt;working with the filesystem&lt;/a&gt; or &lt;a href=&quot;https://realpython.com/python-gui-with-wxpython/&quot;&gt;interacting with the display&lt;/a&gt; are all written in C. It made sense for Python&amp;rsquo;s extensibility layer to be focused on the C language. Later in this article, we will cover the Python Standard Library and the C modules.&lt;/p&gt;
3128 &lt;p&gt;There is a Python compiler written in Python called &lt;a href=&quot;https://pypy.org/&quot;&gt;PyPy&lt;/a&gt;. PyPy&amp;rsquo;s logo is an &lt;a href=&quot;https://en.wikipedia.org/wiki/Ouroboros&quot;&gt;Ouroboros&lt;/a&gt; to represent the self-hosting nature of the compiler.&lt;/p&gt;
3129 &lt;p&gt;Another example of a cross-compiler for Python is &lt;a href=&quot;https://www.jython.org/&quot;&gt;Jython&lt;/a&gt;. Jython is written in Java and compiles from Python source code into Java bytecode. In the same way that CPython makes it easy to import C libraries and use them from Python, Jython makes it easy to import and reference Java modules and classes.&lt;/p&gt;
3130 &lt;h3 id=&quot;the-python-language-specification&quot;&gt;The Python Language Specification&lt;/h3&gt;
3131 &lt;p&gt;Contained within the CPython source code is the definition of the Python language. This is the reference specification used by all the Python interpreters.&lt;/p&gt;
3132 &lt;p&gt;The specification is in both human-readable and machine-readable format. Inside the documentation is a detailed explanation of the Python language, what is allowed, and how each statement should behave.&lt;/p&gt;
3133 &lt;h4 id=&quot;documentation&quot;&gt;Documentation&lt;/h4&gt;
3134 &lt;p&gt;Located inside the &lt;code&gt;Doc/reference&lt;/code&gt; directory are &lt;a href=&quot;http://docutils.sourceforge.net/rst.html&quot;&gt;reStructuredText&lt;/a&gt; explanations of each of the features in the Python language. This forms the official Python reference guide on &lt;a href=&quot;https://docs.python.org/3/reference/&quot;&gt;docs.python.org&lt;/a&gt;.&lt;/p&gt;
3135 &lt;p&gt;Inside the directory are the files you need to understand the whole language, structure, and keywords:&lt;/p&gt;
3136 &lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;cpython/Doc/reference
3137 |
3138 โ”œโ”€โ”€ compound_stmts.rst
3139 โ”œโ”€โ”€ datamodel.rst
3140 โ”œโ”€โ”€ executionmodel.rst
3141 โ”œโ”€โ”€ expressions.rst
3142 โ”œโ”€โ”€ grammar.rst
3143 โ”œโ”€โ”€ import.rst
3144 โ”œโ”€โ”€ index.rst
3145 โ”œโ”€โ”€ introduction.rst
3146 โ”œโ”€โ”€ lexical_analysis.rst
3147 โ”œโ”€โ”€ simple_stmts.rst
3148 โ””โ”€โ”€ toplevel_components.rst
3149 &lt;/pre&gt;&lt;/div&gt;
3150 
3151 &lt;p&gt;Inside &lt;code&gt;compound_stmts.rst&lt;/code&gt;, the documentation for compound statements, you can see a simple example defining the &lt;code&gt;with&lt;/code&gt; statement.&lt;/p&gt;
3152 &lt;p&gt;The &lt;code&gt;with&lt;/code&gt; statement can be used in multiple ways in Python, the simplest being the &lt;a href=&quot;https://dbader.org/blog/python-context-managers-and-with-statement&quot;&gt;instantiation of a context-manager&lt;/a&gt; and a nested block of code:&lt;/p&gt;
3153 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
3154    &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
3155 &lt;/pre&gt;&lt;/div&gt;
3156 
3157 &lt;p&gt;You can assign the result to a variable using the &lt;code&gt;as&lt;/code&gt; keyword:&lt;/p&gt;
3158 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
3159    &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
3160 &lt;/pre&gt;&lt;/div&gt;
3161 
3162 &lt;p&gt;You can also chain context managers together with a comma:&lt;/p&gt;
3163 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
3164    &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
3165 &lt;/pre&gt;&lt;/div&gt;
3166 
3167 &lt;p&gt;Next, we&amp;rsquo;ll explore the computer-readable documentation of the Python language.&lt;/p&gt;
3168 &lt;h4 id=&quot;grammar&quot;&gt;Grammar&lt;/h4&gt;
3169 &lt;p&gt;The documentation contains the human-readable specification of the language, and the machine-readable specification is housed in a single file, &lt;a href=&quot;https://github.com/python/cpython/blob/master/Grammar/Grammar&quot;&gt;&lt;code&gt;Grammar/Grammar&lt;/code&gt;&lt;/a&gt;. &lt;/p&gt;
3170 &lt;p&gt;The Grammar file is written in a context-notation called &lt;a href=&quot;https://en.m.wikipedia.org/wiki/Backus%E2%80%93Naur_form&quot;&gt;Backus-Naur Form (BNF)&lt;/a&gt;. BNF is not specific to Python and is often used as the notation for grammars in many other languages.&lt;/p&gt;
3171 &lt;p&gt;The concept of grammatical structure in a programming language is inspired by &lt;a href=&quot;https://en.wikipedia.org/wiki/Syntactic_Structures&quot;&gt;Noam Chomsky&amp;rsquo;s work on Syntactic Structures&lt;/a&gt; in the 1950s!&lt;/p&gt;
3172 &lt;p&gt;Python&amp;rsquo;s grammar file uses the Extended-BNF (EBNF) specification with regular-expression syntax. So, in the grammar file you can use:&lt;/p&gt;
3173 &lt;ul&gt;
3174 &lt;li&gt;&lt;strong&gt;&lt;code&gt;*&lt;/code&gt;&lt;/strong&gt; for repetition&lt;/li&gt;
3175 &lt;li&gt;&lt;strong&gt;&lt;code&gt;+&lt;/code&gt;&lt;/strong&gt; for at-least-once repetition&lt;/li&gt;
3176 &lt;li&gt;&lt;strong&gt;&lt;code&gt;[]&lt;/code&gt;&lt;/strong&gt; for optional parts&lt;/li&gt;
3177 &lt;li&gt;&lt;strong&gt;&lt;code&gt;|&lt;/code&gt;&lt;/strong&gt; for alternatives&lt;/li&gt;
3178 &lt;li&gt;&lt;strong&gt;&lt;code&gt;()&lt;/code&gt;&lt;/strong&gt; for grouping&lt;/li&gt;
3179 &lt;/ul&gt;
3180 &lt;p&gt;If you search for the &lt;code&gt;with&lt;/code&gt; statement in the grammar file, at around line 80 you&amp;rsquo;ll see the definitions for the &lt;code&gt;with&lt;/code&gt; statement:&lt;/p&gt;
3181 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;with_stmt: &amp;#39;with&amp;#39; with_item (&amp;#39;,&amp;#39; with_item)*  &amp;#39;:&amp;#39; suite
3182 with_item: test [&amp;#39;as&amp;#39; expr]
3183 &lt;/pre&gt;&lt;/div&gt;
3184 
3185 &lt;p&gt;Anything in quotes is a string literal, which is how keywords are defined. So the &lt;code&gt;with_stmt&lt;/code&gt; is specified as:&lt;/p&gt;
3186 &lt;ol&gt;
3187 &lt;li&gt;Starting with the word &lt;code&gt;with&lt;/code&gt;&lt;/li&gt;
3188 &lt;li&gt;Followed by a &lt;code&gt;with_item&lt;/code&gt;, which is a &lt;code&gt;test&lt;/code&gt; and (optionally), the word &lt;code&gt;as&lt;/code&gt;, and an expression&lt;/li&gt;
3189 &lt;li&gt;Following one or many items, each separated by a comma&lt;/li&gt;
3190 &lt;li&gt;Ending with a &lt;code&gt;:&lt;/code&gt;&lt;/li&gt;
3191 &lt;li&gt;Followed by a &lt;code&gt;suite&lt;/code&gt;&lt;/li&gt;
3192 &lt;/ol&gt;
3193 &lt;p&gt;There are references to some other definitions in these two lines:&lt;/p&gt;
3194 &lt;ul&gt;
3195 &lt;li&gt;&lt;strong&gt;&lt;code&gt;suite&lt;/code&gt;&lt;/strong&gt; refers to a block of code with one or multiple statements&lt;/li&gt;
3196 &lt;li&gt;&lt;strong&gt;&lt;code&gt;test&lt;/code&gt;&lt;/strong&gt; refers to a simple statement that is evaluated&lt;/li&gt;
3197 &lt;li&gt;&lt;strong&gt;&lt;code&gt;expr&lt;/code&gt;&lt;/strong&gt; refers to a simple expression&lt;/li&gt;
3198 &lt;/ul&gt;
3199 &lt;p&gt;If you want to explore those in detail, the whole of the Python grammar is defined in this single file.&lt;/p&gt;
3200 &lt;p&gt;If you want to see a recent example of how grammar is used, in PEP 572 the &lt;strong&gt;colon equals&lt;/strong&gt; operator was added to the grammar file in &lt;a href=&quot;https://github.com/python/cpython/commit/8f59ee01be3d83d5513a9a3f654a237d77d80d9a#diff-cb0b9d6312c0d67f6d4aa1966766cedd&quot;&gt;this Git commit&lt;/a&gt;.&lt;/p&gt;
3201 &lt;h4 id=&quot;using-pgen&quot;&gt;Using &lt;code&gt;pgen&lt;/code&gt;&lt;/h4&gt;
3202 &lt;p&gt;The grammar file itself is never used by the Python compiler. Instead, a parser table created by a tool called &lt;code&gt;pgen&lt;/code&gt; is used. &lt;code&gt;pgen&lt;/code&gt; reads the grammar file and converts it into a parser table. If you make changes to the grammar file, you must regenerate the parser table and recompile Python.&lt;/p&gt;
3203 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
3204 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The &lt;code&gt;pgen&lt;/code&gt; application was rewritten in Python 3.8 from C to &lt;a href=&quot;https://github.com/python/cpython/blob/master/Parser/pgen/pgen.py&quot;&gt;pure Python&lt;/a&gt;.&lt;/p&gt;
3205 &lt;/div&gt;
3206 &lt;p&gt;To see &lt;code&gt;pgen&lt;/code&gt; in action, let&amp;rsquo;s change part of the Python grammar. Around line 51 you will see the definition of a &lt;code&gt;pass&lt;/code&gt; statement:&lt;/p&gt;
3207 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;pass_stmt: &amp;#39;pass&amp;#39;
3208 &lt;/pre&gt;&lt;/div&gt;
3209 
3210 &lt;p&gt;Change that line to accept the keyword &lt;code&gt;&#39;pass&#39;&lt;/code&gt; or &lt;code&gt;&#39;proceed&#39;&lt;/code&gt; as keywords:&lt;/p&gt;
3211 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;pass_stmt: &amp;#39;pass&amp;#39; | &amp;#39;proceed&amp;#39;
3212 &lt;/pre&gt;&lt;/div&gt;
3213 
3214 &lt;p&gt;Now you need to rebuild the grammar files.
3215 On macOS and Linux, run &lt;code&gt;make regen-grammar&lt;/code&gt; to run &lt;code&gt;pgen&lt;/code&gt; over the altered grammar file. For Windows, there is no officially supported way of running &lt;code&gt;pgen&lt;/code&gt;. However, you can clone &lt;a href=&quot;https://github.com/tonybaloney/cpython/tree/pcbuildregen&quot;&gt;my fork&lt;/a&gt; and run &lt;code&gt;build.bat --regen&lt;/code&gt; from within the &lt;code&gt;PCBuild&lt;/code&gt; directory.&lt;/p&gt;
3216 &lt;p&gt;You should see an output similar to this, showing that the new &lt;code&gt;Include/graminit.h&lt;/code&gt; and &lt;code&gt;Python/graminit.c&lt;/code&gt; files have been generated:&lt;/p&gt;
3217 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;# Regenerate Doc/library/token-list.inc from Grammar/Tokens
3218 # using Tools/scripts/generate_token.py
3219 ...
3220 python3 ./Tools/scripts/update_file.py ./Include/graminit.h ./Include/graminit.h.new
3221 python3 ./Tools/scripts/update_file.py ./Python/graminit.c ./Python/graminit.c.new
3222 &lt;/pre&gt;&lt;/div&gt;
3223 
3224 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
3225 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;code&gt;pgen&lt;/code&gt; works by converting the EBNF statements into a &lt;a href=&quot;https://en.wikipedia.org/wiki/Nondeterministic_finite_automaton&quot;&gt;Non-deterministic Finite Automaton (NFA)&lt;/a&gt;, which is then turned into a &lt;a href=&quot;https://en.wikipedia.org/wiki/Deterministic_finite_automaton&quot;&gt;Deterministic Finite Automaton (DFA)&lt;/a&gt;.
3226 The DFAs are used by the parser as parsing tables in a special way that&amp;rsquo;s unique to CPython. This technique was &lt;a href=&quot;http://infolab.stanford.edu/~ullman/dragon/slides1.pdf&quot;&gt;formed at Stanford University&lt;/a&gt; and developed in the 1980s, just before the advent of Python.&lt;/p&gt;
3227 &lt;/div&gt;
3228 &lt;p&gt;With the regenerated parser tables, you need to recompile CPython to see the new syntax. Use the same compilation steps you used earlier for your operating system.&lt;/p&gt;
3229 &lt;p&gt;If the code compiled successfully, you can execute your new CPython binary and start a REPL.&lt;/p&gt;
3230 &lt;p&gt;In the REPL, you can now try defining a function and instead of using the &lt;code&gt;pass&lt;/code&gt; statement, use the &lt;code&gt;proceed&lt;/code&gt; keyword alternative that you compiled into the Python grammar:&lt;/p&gt;
3231 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;Python 3.8.0b4 (tags/v3.8.0b4:d93605de72, Aug 30 2019, 10:00:03) 
3232 [Clang 10.0.1 (clang-1001.0.46.4)] on darwin
3233 Type &amp;quot;help&amp;quot;, &amp;quot;copyright&amp;quot;, &amp;quot;credits&amp;quot; or &amp;quot;license&amp;quot; for more information.
3234 &amp;gt;&amp;gt;&amp;gt; def example():
3235 ...    proceed
3236 ... 
3237 &amp;gt;&amp;gt;&amp;gt; example()
3238 &lt;/pre&gt;&lt;/div&gt;
3239 
3240 &lt;p&gt;Well done! You&amp;rsquo;ve changed the CPython syntax and compiled your own version of CPython. Ship it!&lt;/p&gt;
3241 &lt;p&gt;Next, we&amp;rsquo;ll explore tokens and their relationship to grammar.&lt;/p&gt;
3242 &lt;h4 id=&quot;tokens&quot;&gt;Tokens&lt;/h4&gt;
3243 &lt;p&gt;Alongside the grammar file in the &lt;code&gt;Grammar&lt;/code&gt; folder is a &lt;a href=&quot;https://github.com/python/cpython/blob/master/Grammar/Tokens&quot;&gt;&lt;code&gt;Tokens&lt;/code&gt;&lt;/a&gt; file, which contains each of the unique types found as a leaf node in a parse tree. We will cover parser trees in depth later.
3244 Each token also has a name and a generated unique ID. The names are used to make it simpler to refer to in the tokenizer.&lt;/p&gt;
3245 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
3246 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The &lt;code&gt;Tokens&lt;/code&gt; file is a new feature in Python 3.8.&lt;/p&gt;
3247 &lt;/div&gt;
3248 &lt;p&gt;For example, the left parenthesis is called &lt;code&gt;LPAR&lt;/code&gt;, and semicolons are called &lt;code&gt;SEMI&lt;/code&gt;. You&amp;rsquo;ll see these tokens later in the article:&lt;/p&gt;
3249 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;LPAR                    &amp;#39;(&amp;#39;
3250 RPAR                    &amp;#39;)&amp;#39;
3251 LSQB                    &amp;#39;[&amp;#39;
3252 RSQB                    &amp;#39;]&amp;#39;
3253 COLON                   &amp;#39;:&amp;#39;
3254 COMMA                   &amp;#39;,&amp;#39;
3255 SEMI                    &amp;#39;;&amp;#39;
3256 &lt;/pre&gt;&lt;/div&gt;
3257 
3258 &lt;p&gt;As with the &lt;code&gt;Grammar&lt;/code&gt; file, if you change the &lt;code&gt;Tokens&lt;/code&gt; file, you need to run &lt;code&gt;pgen&lt;/code&gt; again. &lt;/p&gt;
3259 &lt;p&gt;To see tokens in action, you can use the &lt;code&gt;tokenize&lt;/code&gt; module in CPython. Create a simple Python script called &lt;code&gt;test_tokens.py&lt;/code&gt;:&lt;/p&gt;
3260 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Hello world!&lt;/span&gt;
3261 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
3262    &lt;span class=&quot;n&quot;&gt;proceed&lt;/span&gt;
3263 &lt;/pre&gt;&lt;/div&gt;
3264 
3265 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
3266 &lt;p&gt;For the rest of this tutorial, &lt;code&gt;./python.exe&lt;/code&gt; will refer to the compiled version of CPython. However, the actual command will depend on your system.&lt;/p&gt;
3267 &lt;p&gt;For Windows:&lt;/p&gt;
3268 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt; &amp;gt; python.exe&lt;/span&gt;
3269 &lt;/pre&gt;&lt;/div&gt;
3270 
3271 &lt;p&gt;For Linux:&lt;/p&gt;
3272 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt; &amp;gt; ./python&lt;/span&gt;
3273 &lt;/pre&gt;&lt;/div&gt;
3274 
3275 &lt;p&gt;For macOS:&lt;/p&gt;
3276 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt; &amp;gt; ./python.exe&lt;/span&gt;
3277 &lt;/pre&gt;&lt;/div&gt;
3278 
3279 &lt;/div&gt;
3280 &lt;p&gt;Then pass this file through a module built into the standard library called &lt;code&gt;tokenize&lt;/code&gt;. You will see the list of tokens, by line and character. Use the &lt;code&gt;-e&lt;/code&gt; flag to output the exact token name:&lt;/p&gt;
3281 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; ./python.exe -m tokenize -e test_tokens.py
3282 
3283 &lt;span class=&quot;go&quot;&gt;0,0-0,0:            ENCODING       &amp;#39;utf-8&amp;#39;        &lt;/span&gt;
3284 &lt;span class=&quot;go&quot;&gt;1,0-1,14:           COMMENT        &amp;#39;# Hello world!&amp;#39;&lt;/span&gt;
3285 &lt;span class=&quot;go&quot;&gt;1,14-1,15:          NL             &amp;#39;\n&amp;#39;           &lt;/span&gt;
3286 &lt;span class=&quot;go&quot;&gt;2,0-2,3:            NAME           &amp;#39;def&amp;#39;          &lt;/span&gt;
3287 &lt;span class=&quot;go&quot;&gt;2,4-2,15:           NAME           &amp;#39;my_function&amp;#39;  &lt;/span&gt;
3288 &lt;span class=&quot;go&quot;&gt;2,15-2,16:          LPAR           &amp;#39;(&amp;#39;            &lt;/span&gt;
3289 &lt;span class=&quot;go&quot;&gt;2,16-2,17:          RPAR           &amp;#39;)&amp;#39;            &lt;/span&gt;
3290 &lt;span class=&quot;go&quot;&gt;2,17-2,18:          COLON          &amp;#39;:&amp;#39;            &lt;/span&gt;
3291 &lt;span class=&quot;go&quot;&gt;2,18-2,19:          NEWLINE        &amp;#39;\n&amp;#39;           &lt;/span&gt;
3292 &lt;span class=&quot;go&quot;&gt;3,0-3,3:            INDENT         &amp;#39;   &amp;#39;          &lt;/span&gt;
3293 &lt;span class=&quot;go&quot;&gt;3,3-3,7:            NAME           &amp;#39;proceed&amp;#39;         &lt;/span&gt;
3294 &lt;span class=&quot;go&quot;&gt;3,7-3,8:            NEWLINE        &amp;#39;\n&amp;#39;           &lt;/span&gt;
3295 &lt;span class=&quot;go&quot;&gt;4,0-4,0:            DEDENT         &amp;#39;&amp;#39;             &lt;/span&gt;
3296 &lt;span class=&quot;go&quot;&gt;4,0-4,0:            ENDMARKER      &amp;#39;&amp;#39;              &lt;/span&gt;
3297 &lt;/pre&gt;&lt;/div&gt;
3298 
3299 &lt;p&gt;In the output, the first column is the range of the line/column coordinates, the second column is the name of the token, and the final column is the value of the token.&lt;/p&gt;
3300 &lt;p&gt;In the output, the &lt;code&gt;tokenize&lt;/code&gt; module has implied some tokens that were not in the file. The &lt;code&gt;ENCODING&lt;/code&gt; token for &lt;code&gt;utf-8&lt;/code&gt;, and a blank line at the end, giving &lt;code&gt;DEDENT&lt;/code&gt; to close the function declaration and an &lt;code&gt;ENDMARKER&lt;/code&gt; to end the file.&lt;/p&gt;
3301 &lt;p&gt;It is best practice to have a blank line at the end of your Python source files. If you omit it, CPython adds it for you, with a tiny performance penalty.&lt;/p&gt;
3302 &lt;p&gt;The &lt;code&gt;tokenize&lt;/code&gt; module is written in pure Python and is located in &lt;a href=&quot;https://github.com/python/cpython/blob/master/Lib/tokenize.py&quot;&gt;&lt;code&gt;Lib/tokenize.py&lt;/code&gt;&lt;/a&gt; within the CPython source code.&lt;/p&gt;
3303 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
3304 &lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; There are two tokenizers in the CPython source code: one written in Python, demonstrated here, and another written in C.
3305 The tokenizer written in Python is meant as a utility, and the one written in C is used by the Python compiler. They have identical output and behavior. The version written in C is designed for performance and the module in Python is designed for debugging.&lt;/p&gt;
3306 &lt;/div&gt;
3307 &lt;p&gt;To see a verbose readout of the C tokenizer, you can run Python with the &lt;code&gt;-d&lt;/code&gt; flag. Using the &lt;code&gt;test_tokens.py&lt;/code&gt; script you created earlier, run it with the following:&lt;/p&gt;
3308 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; ./python.exe -d test_tokens.py
3309 
3310 &lt;span class=&quot;go&quot;&gt;Token NAME/&amp;#39;def&amp;#39; ... It&amp;#39;s a keyword&lt;/span&gt;
3311 &lt;span class=&quot;go&quot;&gt; DFA &amp;#39;file_input&amp;#39;, state 0: Push &amp;#39;stmt&amp;#39;&lt;/span&gt;
3312 &lt;span class=&quot;go&quot;&gt; DFA &amp;#39;stmt&amp;#39;, state 0: Push &amp;#39;compound_stmt&amp;#39;&lt;/span&gt;
3313 &lt;span class=&quot;go&quot;&gt; DFA &amp;#39;compound_stmt&amp;#39;, state 0: Push &amp;#39;funcdef&amp;#39;&lt;/span&gt;
3314 &lt;span class=&quot;go&quot;&gt; DFA &amp;#39;funcdef&amp;#39;, state 0: Shift.&lt;/span&gt;
3315 &lt;span class=&quot;go&quot;&gt;Token NAME/&amp;#39;my_function&amp;#39; ... It&amp;#39;s a token we know&lt;/span&gt;
3316 &lt;span class=&quot;go&quot;&gt; DFA &amp;#39;funcdef&amp;#39;, state 1: Shift.&lt;/span&gt;
3317 &lt;span class=&quot;go&quot;&gt;Token LPAR/&amp;#39;(&amp;#39; ... It&amp;#39;s a token we know&lt;/span&gt;
3318 &lt;span class=&quot;go&quot;&gt; DFA &amp;#39;funcdef&amp;#39;, state 2: Push &amp;#39;parameters&amp;#39;&lt;/span&gt;
3319 &lt;span class=&quot;go&quot;&gt; DFA &amp;#39;parameters&amp;#39;, state 0: Shift.&lt;/span&gt;
3320 &lt;span class=&quot;go&quot;&gt;Token RPAR/&amp;#39;)&amp;#39; ... It&amp;#39;s a token we know&lt;/span&gt;
3321 &lt;span class=&quot;go&quot;&gt; DFA &amp;#39;parameters&amp;#39;, state 1: Shift.&lt;/span&gt;
3322 &lt;span class=&quot;go&quot;&gt;  DFA &amp;#39;parameters&amp;#39;, state 2: Direct pop.&lt;/span&gt;
3323 &lt;span class=&quot;go&quot;&gt;Token COLON/&amp;#39;:&amp;#39; ... It&amp;#39;s a token we know&lt;/span&gt;
3324 &lt;span class=&quot;go&quot;&gt; DFA &amp;#39;funcdef&amp;#39;, state 3: Shift.&lt;/span&gt;
3325 &lt;span class=&quot;go&quot;&gt;Token NEWLINE/&amp;#39;&amp;#39; ... It&amp;#39;s a token we know&lt;/span&gt;
3326 &lt;span class=&quot;go&quot;&gt; DFA &amp;#39;funcdef&amp;#39;, state 5: [switch func_body_suite to suite] Push &amp;#39;suite&amp;#39;&lt;/span&gt;
3327 &lt;span class=&quot;go&quot;&gt; DFA &amp;#39;suite&amp;#39;, state 0: Shift.&lt;/span&gt;
3328 &lt;span class=&quot;go&quot;&gt;Token INDENT/&amp;#39;&amp;#39; ... It&amp;#39;s a token we know&lt;/span&gt;
3329 &lt;span class=&quot;go&quot;&gt; DFA &amp;#39;suite&amp;#39;, state 1: Shift.&lt;/span&gt;
3330 &lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;go&quot;&gt;Token NAME/&amp;#39;proceed&amp;#39; ... It&amp;#39;s a keyword&lt;/span&gt;
3331 &lt;/span&gt;&lt;span class=&quot;go&quot;&gt; DFA &amp;#39;suite&amp;#39;, state 3: Push &amp;#39;stmt&amp;#39;&lt;/span&gt;
3332 &lt;span class=&quot;go&quot;&gt;...&lt;/span&gt;
3333 &lt;span class=&quot;go&quot;&gt;  ACCEPT.&lt;/span&gt;
3334 &lt;/pre&gt;&lt;/div&gt;
3335 
3336 &lt;p&gt;In the output, you can see that it highlighted &lt;code&gt;proceed&lt;/code&gt; as a keyword. In the next chapter, we&amp;rsquo;ll see how executing the Python binary gets to the tokenizer and what happens from there to execute your code.&lt;/p&gt;
3337 &lt;p&gt;Now that you have an overview of the Python grammar and the relationship between tokens and statements, there is a way to convert the &lt;code&gt;pgen&lt;/code&gt; output into an interactive graph.&lt;/p&gt;
3338 &lt;p&gt;Here is a screenshot of the Python 3.8a2 grammar:&lt;/p&gt;
3339 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-12_at_2.31.16_pm.f36c3e99b8b4.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-12_at_2.31.16_pm.f36c3e99b8b4.png&quot; width=&quot;3258&quot; height=&quot;2248&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-12_at_2.31.16_pm.f36c3e99b8b4.png&amp;amp;w=814&amp;amp;sig=21666b4228a46a6bcc7aeca6d5263e62a3aeb6d5 814w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-12_at_2.31.16_pm.f36c3e99b8b4.png&amp;amp;w=1629&amp;amp;sig=01404157eddc09548f8cf7d4e995da9806c36fac 1629w, https://files.realpython.com/media/Screen_Shot_2019-03-12_at_2.31.16_pm.f36c3e99b8b4.png 3258w&quot; sizes=&quot;75vw&quot; alt=&quot;Python 3.8 DFA node graph&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
3340 &lt;p&gt;The Python package used to generate this graph, &lt;code&gt;instaviz&lt;/code&gt;, will be covered in a later chapter.&lt;/p&gt;
3341 &lt;h3 id=&quot;memory-management-in-cpython&quot;&gt;Memory Management in CPython&lt;/h3&gt;
3342 &lt;p&gt;Throughout this article, you will see references to a &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pyarena.c#L128&quot;&gt;&lt;code&gt;PyArena&lt;/code&gt;&lt;/a&gt; object. The arena is one of CPython&amp;rsquo;s memory management structures. The code is within &lt;code&gt;Python/pyarena.c&lt;/code&gt; and contains a wrapper around C&amp;rsquo;s memory allocation and deallocation functions.&lt;/p&gt;
3343 &lt;p&gt;In a traditionally written C program, the developer &lt;em&gt;should&lt;/em&gt; allocate memory for data structures before writing into that data. This allocation marks the memory as belonging to the process with the operating system.&lt;/p&gt;
3344 &lt;p&gt;It is also up to the developer to deallocate, or &amp;ldquo;free,&amp;rdquo; the allocated memory when its no longer being used and return it to the operating system&amp;rsquo;s block table of free memory. 
3345 If a process allocates memory for a variable, say within a function or loop, when that function has completed, the memory is not automatically given back to the operating system in C. So if it hasn&amp;rsquo;t been explicitly deallocated in the C code, it causes a memory leak. The process will continue to take more memory each time that function runs until eventually, the system runs out of memory, and crashes!&lt;/p&gt;
3346 &lt;p&gt;Python takes that responsibility away from the programmer and uses two algorithms: &lt;a href=&quot;https://realpython.com/python-memory-management/&quot;&gt;a reference counter and a garbage collector&lt;/a&gt;.&lt;/p&gt;
3347 &lt;p&gt;Whenever an interpreter is instantiated, a &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pyarena.c#L128&quot;&gt;&lt;code&gt;PyArena&lt;/code&gt;&lt;/a&gt; is created and attached one of the fields in the interpreter. During the lifecycle of a CPython interpreter, many arenas could be allocated. They are connected with a linked list. The arena stores a list of pointers to Python Objects as a &lt;code&gt;PyListObject&lt;/code&gt;. Whenever a new Python object is created, a pointer to it is added using &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pyarena.c#L203&quot;&gt;&lt;code&gt;PyArena_AddPyObject()&lt;/code&gt;&lt;/a&gt;. This function call stores a pointer in the arena&amp;rsquo;s list, &lt;code&gt;a_objects&lt;/code&gt;.&lt;/p&gt;
3348 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
3349 &lt;p&gt;Even though Python doesn&amp;rsquo;t have pointers, there are some &lt;a href=&quot;https://realpython.com/pointers-in-python/&quot;&gt;interesting techniques&lt;/a&gt; to simulate the behavior of pointers.&lt;/p&gt;
3350 &lt;/div&gt;
3351 &lt;p&gt;The &lt;code&gt;PyArena&lt;/code&gt; serves a second function, which is to allocate and reference a list of raw memory blocks. For example, a &lt;code&gt;PyList&lt;/code&gt; would need extra memory if you added thousands of additional values. The &lt;code&gt;PyList&lt;/code&gt; object&amp;rsquo;s C code does not allocate memory directly. The object gets raw blocks of memory from the &lt;code&gt;PyArena&lt;/code&gt; by calling &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pyarena.c#L180&quot;&gt;&lt;code&gt;PyArena_Malloc()&lt;/code&gt;&lt;/a&gt; from the &lt;code&gt;PyObject&lt;/code&gt; with the required memory size. This task is completed by another abstraction in &lt;code&gt;Objects/obmalloc.c&lt;/code&gt;. In the object allocation module, memory can be allocated, freed, and reallocated for a Python Object.&lt;/p&gt;
3352 &lt;p&gt;A linked list of allocated blocks is stored inside the arena, so that when an interpreter is stopped, all managed memory blocks can be deallocated in one go using &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pyarena.c#L157&quot;&gt;&lt;code&gt;PyArena_Free()&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
3353 &lt;p&gt;Take the &lt;code&gt;PyListObject&lt;/code&gt; example. If you were to &lt;code&gt;.append()&lt;/code&gt; an object to the end of a Python list, you don&amp;rsquo;t need to reallocate the memory used in the existing list beforehand. The &lt;code&gt;.append()&lt;/code&gt; method calls &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/listobject.c#L36&quot;&gt;&lt;code&gt;list_resize()&lt;/code&gt;&lt;/a&gt; which handles memory allocation for lists. Each list object keeps a list of the amount of memory allocated. If the item you&amp;rsquo;re appending will fit inside the existing free memory, it is simply added. If the list needs more memory space, it is expanded. Lists are expanded in length as 0, 4, 8, 16, 25, 35, 46, 58, 72, 88.&lt;/p&gt;
3354 &lt;p&gt;&lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/obmalloc.c#L618&quot;&gt;&lt;code&gt;PyMem_Realloc()&lt;/code&gt;&lt;/a&gt; is called to expand the memory allocated in a list. &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/obmalloc.c#L618&quot;&gt;&lt;code&gt;PyMem_Realloc()&lt;/code&gt;&lt;/a&gt; is an API wrapper for &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/obmalloc.c#L1913&quot;&gt;&lt;code&gt;pymalloc_realloc()&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
3355 &lt;p&gt;Python also has a special wrapper for the C call &lt;code&gt;malloc()&lt;/code&gt;, which sets the max size of the memory allocation to help prevent buffer overflow errors (See &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Modules/overlapped.c#L28&quot;&gt;&lt;code&gt;PyMem_RawMalloc()&lt;/code&gt;&lt;/a&gt;).&lt;/p&gt;
3356 &lt;p&gt;In summary: &lt;/p&gt;
3357 &lt;ul&gt;
3358 &lt;li&gt;Allocation of raw memory blocks is done via &lt;code&gt;PyMem_RawAlloc()&lt;/code&gt;.&lt;/li&gt;
3359 &lt;li&gt;The pointers to Python objects are stored within the &lt;code&gt;PyArena&lt;/code&gt;.&lt;/li&gt;
3360 &lt;li&gt;&lt;code&gt;PyArena&lt;/code&gt; also stores a linked-list of allocated memory blocks.&lt;/li&gt;
3361 &lt;/ul&gt;
3362 &lt;p&gt;More information on the API is detailed on the &lt;a href=&quot;https://docs.python.org/3/c-api/memory.html&quot;&gt;CPython documentation&lt;/a&gt;.&lt;/p&gt;
3363 &lt;h4 id=&quot;reference-counting&quot;&gt;Reference Counting&lt;/h4&gt;
3364 &lt;p&gt;To create a variable in Python, you have to assign a value to a &lt;em&gt;uniquely&lt;/em&gt; named variable:&lt;/p&gt;
3365 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_variable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;180392&lt;/span&gt;
3366 &lt;/pre&gt;&lt;/div&gt;
3367 
3368 &lt;p&gt;Whenever a value is assigned to a variable in Python, the name of the variable is checked within the locals and globals scope to see if it already exists.&lt;/p&gt;
3369 &lt;p&gt;Because &lt;code&gt;my_variable&lt;/code&gt; is not already within the &lt;code&gt;locals()&lt;/code&gt; or &lt;code&gt;globals()&lt;/code&gt; dictionary, this new object is created, and the value is assigned as being the numeric constant &lt;code&gt;180392&lt;/code&gt;.&lt;/p&gt;
3370 &lt;p&gt;There is now one reference to &lt;code&gt;my_variable&lt;/code&gt;, so the reference counter for &lt;code&gt;my_variable&lt;/code&gt; is incremented by 1. &lt;/p&gt;
3371 &lt;p&gt;You will see function calls &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/object.c#L239&quot;&gt;&lt;code&gt;Py_INCREF()&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/object.c#L245&quot;&gt;&lt;code&gt;Py_DECREF()&lt;/code&gt;&lt;/a&gt; throughout the C source code for CPython. These functions increment and decrement the count of references to that object.&lt;/p&gt;
3372 &lt;p&gt;References to an object are decremented when a variable falls outside of the scope in which it was declared. Scope in Python can refer to a function or method, a comprehension, or a lambda function. These are some of the more literal scopes, but there are many other implicit scopes, like passing variables to a function call.&lt;/p&gt;
3373 &lt;p&gt;The handling of incrementing and decrementing references based on the language is built into the CPython compiler and the core execution loop, &lt;code&gt;ceval.c&lt;/code&gt;, which we will cover in detail later in this article.&lt;/p&gt;
3374 &lt;p&gt;Whenever &lt;code&gt;Py_DECREF()&lt;/code&gt; is called, and the counter becomes 0, the &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/obmalloc.c#L707&quot;&gt;&lt;code&gt;PyObject_Free()&lt;/code&gt;&lt;/a&gt; function is called. For that object &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pyarena.c#L157&quot;&gt;&lt;code&gt;PyArena_Free()&lt;/code&gt;&lt;/a&gt; is called for all of the memory that was allocated. &lt;/p&gt;
3375 &lt;h4 id=&quot;garbage-collection&quot;&gt;Garbage Collection&lt;/h4&gt;
3376 &lt;p&gt;How often does your garbage get collected? Weekly, or fortnightly? &lt;/p&gt;
3377 &lt;p&gt;When you&amp;rsquo;re finished with something, you discard it and throw it in the trash. But that trash won&amp;rsquo;t get collected straight away. You need to wait for the garbage trucks to come and pick it up.&lt;/p&gt;
3378 &lt;p&gt;CPython has the same principle, using a garbage collection algorithm. CPython&amp;rsquo;s garbage collector is enabled by default, happens in the background and works to deallocate memory that&amp;rsquo;s been used for objects which are no longer in use.&lt;/p&gt;
3379 &lt;p&gt;Because the garbage collection algorithm is a lot more complex than the reference counter, it doesn&amp;rsquo;t happen all the time, otherwise, it would consume a huge amount of CPU resources. It happens periodically, after a set number of operations.&lt;/p&gt;
3380 &lt;p&gt;CPython&amp;rsquo;s standard library comes with a Python module to interface with the arena and the garbage collector, the &lt;code&gt;gc&lt;/code&gt; module. Here&amp;rsquo;s how to use the &lt;code&gt;gc&lt;/code&gt; module in debug mode:&lt;/p&gt;
3381 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;gc&lt;/span&gt;
3382 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEBUG_STATS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3383 &lt;/pre&gt;&lt;/div&gt;
3384 
3385 &lt;p&gt;This will print the statistics whenever the garbage collector is run.&lt;/p&gt;
3386 &lt;p&gt;You can get the threshold after which the garbage collector is run by calling &lt;code&gt;get_threshold()&lt;/code&gt;:&lt;/p&gt;
3387 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_threshold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
3388 &lt;span class=&quot;go&quot;&gt;(700, 10, 10)&lt;/span&gt;
3389 &lt;/pre&gt;&lt;/div&gt;
3390 
3391 &lt;p&gt;You can also get the current threshold counts:&lt;/p&gt;
3392 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
3393 &lt;span class=&quot;go&quot;&gt;(688, 1, 1)&lt;/span&gt;
3394 &lt;/pre&gt;&lt;/div&gt;
3395 
3396 &lt;p&gt;Lastly, you can run the collection algorithm manually:&lt;/p&gt;
3397 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
3398 &lt;span class=&quot;go&quot;&gt;24&lt;/span&gt;
3399 &lt;/pre&gt;&lt;/div&gt;
3400 
3401 &lt;p&gt;This will call &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Modules/gcmodule.c#L987&quot;&gt;&lt;code&gt;collect()&lt;/code&gt;&lt;/a&gt; inside the &lt;code&gt;Modules/gcmodule.c&lt;/code&gt; file which contains the implementation of the garbage collector algorithm.&lt;/p&gt;
3402 &lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
3403 &lt;p&gt;In Part 1, you covered the structure of the source code repository, how to compile from source, and the Python language specification. These core concepts will be critical in Part 2 as you dive deeper into the Python interpreter process.&lt;/p&gt;
3404 &lt;h2 h1=&quot;h1&quot; id=&quot;part-2-the-python-interpreter-process&quot;&gt;Part 2: The Python Interpreter Process&lt;/h2&gt;
3405 &lt;p&gt;Now that you&amp;rsquo;ve seen the Python grammar and memory management, you can follow the process from typing &lt;code&gt;python&lt;/code&gt; to the part where your code is executed.&lt;/p&gt;
3406 &lt;p&gt;There are five ways the &lt;code&gt;python&lt;/code&gt; binary can be called:&lt;/p&gt;
3407 &lt;ol&gt;
3408 &lt;li&gt;To run a single command with &lt;code&gt;-c&lt;/code&gt; and a Python command&lt;/li&gt;
3409 &lt;li&gt;To start a module with &lt;code&gt;-m&lt;/code&gt; and the name of a module&lt;/li&gt;
3410 &lt;li&gt;To run a file with the filename&lt;/li&gt;
3411 &lt;li&gt;To run the &lt;code&gt;stdin&lt;/code&gt; input using a shell pipe&lt;/li&gt;
3412 &lt;li&gt;To start the REPL and execute commands one at a time&lt;/li&gt;
3413 &lt;/ol&gt;
3414 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
3415 &lt;p&gt;Python has so many ways to execute scripts, it can be a little overwhelming. Darren Jones has put together a &lt;a href=&quot;https://realpython.com/courses/running-python-scripts/&quot;&gt;great course on running Python scripts&lt;/a&gt; if you want to learn more.&lt;/p&gt;
3416 &lt;/div&gt;
3417 &lt;p&gt;The three source files you need to inspect to see this process are:&lt;/p&gt;
3418 &lt;ol&gt;
3419 &lt;li&gt;&lt;strong&gt;&lt;code&gt;Programs/python.c&lt;/code&gt;&lt;/strong&gt; is a simple entry point.&lt;/li&gt;
3420 &lt;li&gt;&lt;strong&gt;&lt;code&gt;Modules/main.c&lt;/code&gt;&lt;/strong&gt; contains the code to bring together the whole process, loading configuration, executing code and clearing up memory.&lt;/li&gt;
3421 &lt;li&gt;&lt;strong&gt;&lt;code&gt;Python/initconfig.c&lt;/code&gt;&lt;/strong&gt; loads the configuration from the system environment and merges it with any command-line flags.&lt;/li&gt;
3422 &lt;/ol&gt;
3423 &lt;p&gt;This diagram shows how each of those functions is called:&lt;/p&gt;
3424 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/swim-lanes-chart-1.9fb3000aad85.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/swim-lanes-chart-1.9fb3000aad85.png&quot; width=&quot;1046&quot; height=&quot;851&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/swim-lanes-chart-1.9fb3000aad85.png&amp;amp;w=261&amp;amp;sig=8aa36cedaf32be0236896cce2c32b6c4c4ec7e05 261w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/swim-lanes-chart-1.9fb3000aad85.png&amp;amp;w=523&amp;amp;sig=a447b53d2afa96dfd204b9265c8b439bd71272d7 523w, https://files.realpython.com/media/swim-lanes-chart-1.9fb3000aad85.png 1046w&quot; sizes=&quot;75vw&quot; alt=&quot;Python run swim lane diagram&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
3425 &lt;p&gt;The execution mode is determined from the configuration.&lt;/p&gt;
3426 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
3427 &lt;p&gt;&lt;strong&gt;The CPython source code style:&lt;/strong&gt;&lt;/p&gt;
3428 &lt;p&gt;Similar to the &lt;a href=&quot;https://realpython.com/courses/writing-beautiful-python-code-pep-8/&quot;&gt;PEP8 style guide for Python code&lt;/a&gt;, there is an &lt;a href=&quot;https://www.python.org/dev/peps/pep-0007/&quot;&gt;official style guide&lt;/a&gt; for the CPython C code, designed originally in 2001 and updated for modern versions. &lt;/p&gt;
3429 &lt;p&gt;There are some naming standards which help when navigating the source code:&lt;/p&gt;
3430 &lt;ul&gt;
3431 &lt;li&gt;
3432 &lt;p&gt;Use a &lt;code&gt;Py&lt;/code&gt; prefix for public functions, never for static functions. The &lt;code&gt;Py_&lt;/code&gt; prefix is reserved for global service routines like &lt;code&gt;Py_FatalError&lt;/code&gt;. Specific groups of routines (like specific object type APIs) use a longer prefix, such as &lt;code&gt;PyString_&lt;/code&gt; for string functions.&lt;/p&gt;
3433 &lt;/li&gt;
3434 &lt;li&gt;
3435 &lt;p&gt;Public functions and variables use MixedCase with underscores, like this: &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/object.c#L924&quot;&gt;&lt;code&gt;PyObject_GetAttr&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Include/modsupport.h#L20&quot;&gt;&lt;code&gt;Py_BuildValue&lt;/code&gt;&lt;/a&gt;, &lt;code&gt;PyExc_TypeError&lt;/code&gt;.&lt;/p&gt;
3436 &lt;/li&gt;
3437 &lt;li&gt;
3438 &lt;p&gt;Occasionally an &amp;ldquo;internal&amp;rdquo; function has to be visible to the loader. We use the &lt;code&gt;_Py&lt;/code&gt; prefix for this, for example, &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/object.c#L464&quot;&gt;&lt;code&gt;_PyObject_Dump&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
3439 &lt;/li&gt;
3440 &lt;li&gt;
3441 &lt;p&gt;Macros should have a MixedCase prefix and then use upper case, for example &lt;code&gt;PyString_AS_STRING&lt;/code&gt;, &lt;code&gt;Py_PRINT_RAW&lt;/code&gt;.&lt;/p&gt;
3442 &lt;/li&gt;
3443 &lt;/ul&gt;
3444 &lt;/div&gt;
3445 &lt;h3 id=&quot;establishing-runtime-configuration&quot;&gt;Establishing Runtime Configuration&lt;/h3&gt;
3446 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/swim-lanes-chart-1.9fb3000aad85.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/swim-lanes-chart-1.9fb3000aad85.png&quot; width=&quot;1046&quot; height=&quot;851&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/swim-lanes-chart-1.9fb3000aad85.png&amp;amp;w=261&amp;amp;sig=8aa36cedaf32be0236896cce2c32b6c4c4ec7e05 261w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/swim-lanes-chart-1.9fb3000aad85.png&amp;amp;w=523&amp;amp;sig=a447b53d2afa96dfd204b9265c8b439bd71272d7 523w, https://files.realpython.com/media/swim-lanes-chart-1.9fb3000aad85.png 1046w&quot; sizes=&quot;75vw&quot; alt=&quot;Python run swim lane diagram&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
3447 &lt;p&gt;In the swimlanes, you can see that before any Python code is executed, the runtime first establishes the configuration.
3448 The configuration of the runtime is a data structure defined in &lt;code&gt;Include/cpython/initconfig.h&lt;/code&gt; named &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Include/cpython/initconfig.h#L407&quot;&gt;&lt;code&gt;PyConfig&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
3449 &lt;p&gt;The configuration data structure includes things like:&lt;/p&gt;
3450 &lt;ul&gt;
3451 &lt;li&gt;Runtime flags for various modes like debug and optimized mode&lt;/li&gt;
3452 &lt;li&gt;The execution mode, such as whether a filename was passed, &lt;code&gt;stdin&lt;/code&gt; was provided or a module name&lt;/li&gt;
3453 &lt;li&gt;Extended option, specified by &lt;code&gt;-X &amp;lt;option&amp;gt;&lt;/code&gt;&lt;/li&gt;
3454 &lt;li&gt;Environment variables for runtime settings&lt;/li&gt;
3455 &lt;/ul&gt;
3456 &lt;p&gt;The configuration data is primarily used by the CPython runtime to enable and disable various features.&lt;/p&gt;
3457 &lt;p&gt;Python also comes with several &lt;a href=&quot;https://docs.python.org/3/using/cmdline.html&quot;&gt;Command Line Interface Options&lt;/a&gt;. In Python you can enable verbose mode with the &lt;code&gt;-v&lt;/code&gt; flag. In verbose mode, Python will print messages to the screen when modules are loaded:&lt;/p&gt;
3458 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; ./python.exe -v -c &lt;span class=&quot;s2&quot;&gt;&amp;quot;print(&amp;#39;hello world&amp;#39;)&amp;quot;&lt;/span&gt;
3459 
3460 
3461 &lt;span class=&quot;gp&quot;&gt;#&lt;/span&gt; installing zipimport hook
3462 &lt;span class=&quot;go&quot;&gt;import zipimport # builtin&lt;/span&gt;
3463 &lt;span class=&quot;gp&quot;&gt;#&lt;/span&gt; installed zipimport hook
3464 &lt;span class=&quot;go&quot;&gt;...&lt;/span&gt;
3465 &lt;/pre&gt;&lt;/div&gt;
3466 
3467 &lt;p&gt;You will see a hundred lines or more with all the imports of your user site-packages and anything else in the system environment.&lt;/p&gt;
3468 &lt;p&gt;You can see the definition of this flag within &lt;code&gt;Include/cpython/initconfig.h&lt;/code&gt; inside the &lt;code&gt;struct&lt;/code&gt; for &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Include/cpython/initconfig.h#L407&quot;&gt;&lt;code&gt;PyConfig&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
3469 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;cm&quot;&gt;/* --- PyConfig ---------------------------------------------- */&lt;/span&gt;
3470 
3471 &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3472     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_config_version&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;cm&quot;&gt;/* Internal configuration version,&lt;/span&gt;
3473 &lt;span class=&quot;cm&quot;&gt;                             used for ABI compatibility */&lt;/span&gt;
3474     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_config_init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;     &lt;span class=&quot;cm&quot;&gt;/* _PyConfigInitEnum value */&lt;/span&gt;
3475 
3476     &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3477 
3478     &lt;span class=&quot;cm&quot;&gt;/* If greater than 0, enable the verbose mode: print a message each time a&lt;/span&gt;
3479 &lt;span class=&quot;cm&quot;&gt;       module is initialized, showing the place (filename or built-in module)&lt;/span&gt;
3480 &lt;span class=&quot;cm&quot;&gt;       from which it is loaded.&lt;/span&gt;
3481 
3482 &lt;span class=&quot;cm&quot;&gt;       If greater or equal to 2, print a message for each file that is checked&lt;/span&gt;
3483 &lt;span class=&quot;cm&quot;&gt;       for when searching for a module. Also provides information on module&lt;/span&gt;
3484 &lt;span class=&quot;cm&quot;&gt;       cleanup at exit.&lt;/span&gt;
3485 
3486 &lt;span class=&quot;cm&quot;&gt;       Incremented by the -v option. Set by the PYTHONVERBOSE environment&lt;/span&gt;
3487 &lt;span class=&quot;cm&quot;&gt;       variable. If set to -1 (default), inherit Py_VerboseFlag value. */&lt;/span&gt;
3488     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;verbose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3489 &lt;/pre&gt;&lt;/div&gt;
3490 
3491 &lt;p&gt;In &lt;code&gt;Python/initconfig.c&lt;/code&gt;, the logic for reading settings from environment variables and runtime command-line flags is established.&lt;/p&gt;
3492 &lt;p&gt;In the &lt;code&gt;config_read_env_vars&lt;/code&gt; function, the environment variables are read and used to assign the values for the configuration settings:&lt;/p&gt;
3493 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyStatus&lt;/span&gt;
3494 &lt;span class=&quot;nf&quot;&gt;config_read_env_vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyConfig&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3495 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3496     &lt;span class=&quot;n&quot;&gt;PyStatus&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3497     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;use_env&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;use_environment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3498 
3499     &lt;span class=&quot;cm&quot;&gt;/* Get environment variables */&lt;/span&gt;
3500 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;_Py_get_env_flag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;use_env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parser_debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;PYTHONDEBUG&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3501 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;_Py_get_env_flag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;use_env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;verbose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;PYTHONVERBOSE&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3502     &lt;span class=&quot;n&quot;&gt;_Py_get_env_flag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;use_env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimization_level&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;PYTHONOPTIMIZE&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3503     &lt;span class=&quot;n&quot;&gt;_Py_get_env_flag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;use_env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inspect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;PYTHONINSPECT&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3504 &lt;/pre&gt;&lt;/div&gt;
3505 
3506 &lt;p&gt;For the verbose setting, you can see that the value of &lt;code&gt;PYTHONVERBOSE&lt;/code&gt; is used to set the value of &lt;code&gt;&amp;amp;config-&amp;gt;verbose&lt;/code&gt;, if &lt;code&gt;PYTHONVERBOSE&lt;/code&gt; is found. If the environment variable does not exist, then the default value of &lt;code&gt;-1&lt;/code&gt; will remain.&lt;/p&gt;
3507 &lt;p&gt;Then in &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/initconfig.c#L1828&quot;&gt;&lt;code&gt;config_parse_cmdline&lt;/code&gt;&lt;/a&gt; within &lt;code&gt;initconfig.c&lt;/code&gt; again, the command-line flag is used to set the value, if provided:&lt;/p&gt;
3508 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyStatus&lt;/span&gt;
3509 &lt;span class=&quot;nf&quot;&gt;config_parse_cmdline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyConfig&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyWideStringList&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;warnoptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
3510                      &lt;span class=&quot;n&quot;&gt;Py_ssize_t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;opt_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3511 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3512 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3513 
3514         &lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3515 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3516 
3517         &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&amp;#39;v&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
3518 &lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;verbose&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3519 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3520 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3521         &lt;span class=&quot;cm&quot;&gt;/* This space reserved for other options */&lt;/span&gt;
3522 
3523         &lt;span class=&quot;k&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
3524             &lt;span class=&quot;cm&quot;&gt;/* unknown argument: parsing failed */&lt;/span&gt;
3525             &lt;span class=&quot;n&quot;&gt;config_usage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3526             &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_PyStatus_EXIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3527         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3528     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3529 &lt;/pre&gt;&lt;/div&gt;
3530 
3531 &lt;p&gt;This value is later copied to a global variable &lt;code&gt;Py_VerboseFlag&lt;/code&gt; by the &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/initconfig.c#L134&quot;&gt;&lt;code&gt;_Py_GetGlobalVariablesAsDict&lt;/code&gt;&lt;/a&gt; function.&lt;/p&gt;
3532 &lt;p&gt;Within a Python session, you can access the runtime flags, like verbose mode, quiet mode, using the &lt;code&gt;sys.flags&lt;/code&gt; named tuple.
3533 The &lt;code&gt;-X&lt;/code&gt; flags are all available inside the &lt;code&gt;sys._xoptions&lt;/code&gt; dictionary:&lt;/p&gt;
3534 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;$ ./python.exe -X dev -q       &lt;/span&gt;
3535 
3536 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
3537 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;
3538 &lt;span class=&quot;go&quot;&gt;sys.flags(debug=0, inspect=0, interactive=0, optimize=0, dont_write_bytecode=0, &lt;/span&gt;
3539 &lt;span class=&quot;go&quot;&gt; no_user_site=0, no_site=0, ignore_environment=0, verbose=0, bytes_warning=0, &lt;/span&gt;
3540 &lt;span class=&quot;go&quot;&gt; quiet=1, hash_randomization=1, isolated=0, dev_mode=True, utf8_mode=0)&lt;/span&gt;
3541 
3542 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_xoptions&lt;/span&gt;
3543 &lt;span class=&quot;go&quot;&gt;{&amp;#39;dev&amp;#39;: True}&lt;/span&gt;
3544 &lt;/pre&gt;&lt;/div&gt;
3545 
3546 &lt;p&gt;As well as the runtime configuration in &lt;code&gt;initconfig.h&lt;/code&gt;, there is also the build configuration, which is located inside &lt;code&gt;pyconfig.h&lt;/code&gt; in the root folder. This file is created dynamically in the &lt;code&gt;configure&lt;/code&gt; step in the build process, or by Visual Studio for Windows systems.&lt;/p&gt;
3547 &lt;p&gt;You can see the build configuration by running:&lt;/p&gt;
3548 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; ./python.exe -m sysconfig
3549 &lt;/pre&gt;&lt;/div&gt;
3550 
3551 &lt;h3 id=&quot;reading-filesinput&quot;&gt;Reading Files/Input&lt;/h3&gt;
3552 &lt;p&gt;Once CPython has the runtime configuration and the command-line arguments, it can establish what it needs to execute.&lt;/p&gt;
3553 &lt;p&gt;This task is handled by the &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Modules/main.c#L665&quot;&gt;&lt;code&gt;pymain_main&lt;/code&gt;&lt;/a&gt; function inside &lt;code&gt;Modules/main.c&lt;/code&gt;. Depending on the newly created &lt;code&gt;config&lt;/code&gt; instance, CPython will now execute code provided via several options.&lt;/p&gt;
3554 &lt;h4 id=&quot;input-via-c&quot;&gt;Input via &lt;code&gt;-c&lt;/code&gt;&lt;/h4&gt;
3555 &lt;p&gt;The simplest is providing CPython a command with the &lt;code&gt;-c&lt;/code&gt; option and a Python program inside quotes.&lt;/p&gt;
3556 &lt;p&gt;For example:&lt;/p&gt;
3557 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; ./python.exe -c &lt;span class=&quot;s2&quot;&gt;&amp;quot;print(&amp;#39;hi&amp;#39;)&amp;quot;&lt;/span&gt;
3558 &lt;span class=&quot;go&quot;&gt;hi&lt;/span&gt;
3559 &lt;/pre&gt;&lt;/div&gt;
3560 
3561 &lt;p&gt;Here is the full flowchart of how this happens:&lt;/p&gt;
3562 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pymain_run_command.f5da561ba7d5.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/pymain_run_command.f5da561ba7d5.png&quot; width=&quot;1041&quot; height=&quot;751&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pymain_run_command.f5da561ba7d5.png&amp;amp;w=260&amp;amp;sig=f7f802b23e900bf42b29804ee80ed0dd0eaec6a4 260w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pymain_run_command.f5da561ba7d5.png&amp;amp;w=520&amp;amp;sig=3079060f305945fd3556ce7cd453fac965a6ec27 520w, https://files.realpython.com/media/pymain_run_command.f5da561ba7d5.png 1041w&quot; sizes=&quot;75vw&quot; alt=&quot;Flow chart of pymain_run_command&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
3563 &lt;p&gt;First, the &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Modules/main.c#L240&quot;&gt;&lt;code&gt;pymain_run_command()&lt;/code&gt;&lt;/a&gt; function is executed inside &lt;code&gt;Modules/main.c&lt;/code&gt; taking the command passed in &lt;code&gt;-c&lt;/code&gt; as an argument in the C type &lt;code&gt;wchar_t*&lt;/code&gt;. The &lt;code&gt;wchar_t*&lt;/code&gt; type is often used as a low-level storage type for Unicode data across CPython as the size of the type can store UTF8 characters.&lt;/p&gt;
3564 &lt;p&gt;When converting the &lt;code&gt;wchar_t*&lt;/code&gt; to a Python string, the &lt;code&gt;Objects/unicodeobject.c&lt;/code&gt; file has a helper function &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/unicodeobject.c#L2088&quot;&gt;&lt;code&gt;PyUnicode_FromWideChar()&lt;/code&gt;&lt;/a&gt; that returns a &lt;code&gt;PyObject&lt;/code&gt;, of type &lt;code&gt;str&lt;/code&gt;. The encoding to UTF8 is then done by &lt;code&gt;PyUnicode_AsUTF8String()&lt;/code&gt; on the Python &lt;code&gt;str&lt;/code&gt; object to convert it to a Python &lt;code&gt;bytes&lt;/code&gt; object. &lt;/p&gt;
3565 &lt;p&gt;Once this is complete, &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Modules/main.c#L240&quot;&gt;&lt;code&gt;pymain_run_command()&lt;/code&gt;&lt;/a&gt; will then pass the Python bytes object to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L453&quot;&gt;&lt;code&gt;PyRun_SimpleStringFlags()&lt;/code&gt;&lt;/a&gt; for execution, but first converting the &lt;code&gt;bytes&lt;/code&gt; to a &lt;code&gt;str&lt;/code&gt; type again:&lt;/p&gt;
3566 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;
3567 &lt;span class=&quot;nf&quot;&gt;pymain_run_command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;wchar_t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyCompilerFlags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3568 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3569     &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unicode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3570     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3571 
3572     &lt;span class=&quot;n&quot;&gt;unicode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyUnicode_FromWideChar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3573     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unicode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3574         &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3575     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3576 
3577     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PySys_Audit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;cpython.run_command&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;O&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unicode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3578         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pymain_exit_err_print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
3579     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3580 
3581     &lt;span class=&quot;n&quot;&gt;bytes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyUnicode_AsUTF8String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unicode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3582     &lt;span class=&quot;n&quot;&gt;Py_DECREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unicode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3583     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bytes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3584         &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3585     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3586 
3587     &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyRun_SimpleStringFlags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyBytes_AsString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3588     &lt;span class=&quot;n&quot;&gt;Py_DECREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3589     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3590 
3591 &lt;span class=&quot;nl&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
3592     &lt;span class=&quot;n&quot;&gt;PySys_WriteStderr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Unable to decode the command from the command line:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3593     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pymain_exit_err_print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
3594 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3595 &lt;/pre&gt;&lt;/div&gt;
3596 
3597 &lt;p&gt;The conversion of &lt;code&gt;wchar_t*&lt;/code&gt; to Unicode, bytes, and then a string is roughly equivalent to the following:&lt;/p&gt;
3598 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unicode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3599 &lt;span class=&quot;n&quot;&gt;bytes_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unicode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;utf8&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
3600 &lt;span class=&quot;c1&quot;&gt;# call PyRun_SimpleStringFlags with bytes_&lt;/span&gt;
3601 &lt;/pre&gt;&lt;/div&gt;
3602 
3603 &lt;p&gt;The &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L453&quot;&gt;&lt;code&gt;PyRun_SimpleStringFlags()&lt;/code&gt;&lt;/a&gt; function is part of &lt;code&gt;Python/pythonrun.c&lt;/code&gt;. It&amp;rsquo;s purpose is to turn this simple command into a Python module and then send it on to be executed.
3604 Since a Python module needs to have &lt;code&gt;__main__&lt;/code&gt; to be executed as a standalone module, it creates that automatically:&lt;/p&gt;
3605 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;
3606 &lt;span class=&quot;nf&quot;&gt;PyRun_SimpleStringFlags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyCompilerFlags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3607 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3608     &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3609 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyImport_AddModule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;__main__&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3610 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3611         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3612 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyModule_GetDict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3613 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyRun_StringFlags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_file_input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3614 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3615         &lt;span class=&quot;n&quot;&gt;PyErr_Print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
3616         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3617     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3618     &lt;span class=&quot;n&quot;&gt;Py_DECREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3619     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3620 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3621 &lt;/pre&gt;&lt;/div&gt;
3622 
3623 &lt;p&gt;Once &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L453&quot;&gt;&lt;code&gt;PyRun_SimpleStringFlags()&lt;/code&gt;&lt;/a&gt; has created a module and a dictionary, it calls &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1008&quot;&gt;&lt;code&gt;PyRun_StringFlags()&lt;/code&gt;&lt;/a&gt;, which creates a fake filename and then calls the Python parser to create an AST from the string and return a module, &lt;code&gt;mod&lt;/code&gt;:&lt;/p&gt;
3624 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
3625 &lt;span class=&quot;nf&quot;&gt;PyRun_StringFlags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
3626                   &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;locals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyCompilerFlags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3627 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3628 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3629     &lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyParser_ASTFromStringObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3630     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3631         &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;run_mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;locals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3632     &lt;span class=&quot;n&quot;&gt;PyArena_Free&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3633     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3634 &lt;/pre&gt;&lt;/div&gt;
3635 
3636 &lt;p&gt;You&amp;rsquo;ll dive into the AST and Parser code in the next section.&lt;/p&gt;
3637 &lt;h4 id=&quot;input-via-m&quot;&gt;Input via &lt;code&gt;-m&lt;/code&gt;&lt;/h4&gt;
3638 &lt;p&gt;Another way to execute Python commands is by using the &lt;code&gt;-m&lt;/code&gt; option with the name of a module.
3639 A typical example is &lt;code&gt;python -m unittest&lt;/code&gt; to run the unittest module in the standard library.&lt;/p&gt;
3640 &lt;p&gt;Being able to execute modules as scripts were initially proposed in &lt;a href=&quot;https://www.python.org/dev/peps/pep-0338&quot;&gt;PEP 338&lt;/a&gt; and then the standard for explicit relative imports defined in &lt;a href=&quot;https://www.python.org/dev/peps/pep-0366&quot;&gt;PEP366&lt;/a&gt;. &lt;/p&gt;
3641 &lt;p&gt;The use of the &lt;code&gt;-m&lt;/code&gt; flag implies that within the module package, you want to execute whatever is inside &lt;a href=&quot;https://realpython.com/python-main-function/&quot;&gt;&lt;code&gt;__main__&lt;/code&gt;&lt;/a&gt;. It also implies that you want to search &lt;code&gt;sys.path&lt;/code&gt; for the named module.&lt;/p&gt;
3642 &lt;p&gt;This search mechanism is why you don&amp;rsquo;t need to remember where the &lt;code&gt;unittest&lt;/code&gt; module is stored on your filesystem.&lt;/p&gt;
3643 &lt;p&gt;Inside &lt;code&gt;Modules/main.c&lt;/code&gt; there is a function called when the command-line is run with the &lt;code&gt;-m&lt;/code&gt; flag. The name of the module is passed as the &lt;code&gt;modname&lt;/code&gt; argument.&lt;/p&gt;
3644 &lt;p&gt;CPython will then import a standard library module, &lt;code&gt;runpy&lt;/code&gt; and execute it using &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/call.c#L214&quot;&gt;&lt;code&gt;PyObject_Call()&lt;/code&gt;&lt;/a&gt;. The import is done using the C API function &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/import.c#L1409&quot;&gt;&lt;code&gt;PyImport_ImportModule()&lt;/code&gt;&lt;/a&gt;, found within the &lt;code&gt;Python/import.c&lt;/code&gt; file:&lt;/p&gt;
3645 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;
3646 &lt;span class=&quot;nf&quot;&gt;pymain_run_module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;wchar_t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;modname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;set_argv0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3647 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3648     &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;runpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;runmodule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;runargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3649     &lt;span class=&quot;n&quot;&gt;runpy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyImport_ImportModule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;runpy&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3650  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3651     &lt;span class=&quot;n&quot;&gt;runmodule&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject_GetAttrString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;runpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;_run_module_as_main&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3652  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3653     &lt;span class=&quot;n&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyUnicode_FromWideChar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;modname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wcslen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;modname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
3654  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3655     &lt;span class=&quot;n&quot;&gt;runargs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_BuildValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;(Oi)&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;set_argv0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3656  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3657     &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject_Call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;runmodule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;runargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3658  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3659     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3660         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pymain_exit_err_print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
3661     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3662     &lt;span class=&quot;n&quot;&gt;Py_DECREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3663     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3664 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3665 &lt;/pre&gt;&lt;/div&gt;
3666 
3667 &lt;p&gt;In this function you&amp;rsquo;ll also see 2 other C API functions: &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/call.c#L214&quot;&gt;&lt;code&gt;PyObject_Call()&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/object.c#L831&quot;&gt;&lt;code&gt;PyObject_GetAttrString()&lt;/code&gt;&lt;/a&gt;. Because &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/import.c#L1409&quot;&gt;&lt;code&gt;PyImport_ImportModule()&lt;/code&gt;&lt;/a&gt; returns a &lt;code&gt;PyObject*&lt;/code&gt;, the core object type, you need to call special functions to get attributes and to call it.&lt;/p&gt;
3668 &lt;p&gt;In Python, if you had an object and wanted to get an attribute, then you could call &lt;code&gt;getattr()&lt;/code&gt;. In the C API, this call is &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/object.c#L831&quot;&gt;&lt;code&gt;PyObject_GetAttrString()&lt;/code&gt;&lt;/a&gt;, which is found in &lt;code&gt;Objects/object.c&lt;/code&gt;. If you wanted to run a callable, you would give it parentheses, or you can run the &lt;code&gt;__call__()&lt;/code&gt; property on any Python object. The &lt;code&gt;__call__()&lt;/code&gt; method is implemented inside &lt;code&gt;Objects/object.c&lt;/code&gt;:&lt;/p&gt;
3669 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;hi!&amp;quot;&lt;/span&gt;
3670 &lt;span class=&quot;n&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;upper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;upper&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__call__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# this is the same&lt;/span&gt;
3671 &lt;/pre&gt;&lt;/div&gt;
3672 
3673 &lt;p&gt;The &lt;code&gt;runpy&lt;/code&gt; module is written in pure Python and located in &lt;code&gt;Lib/runpy.py&lt;/code&gt;.&lt;/p&gt;
3674 &lt;p&gt;Executing &lt;code&gt;python -m &amp;lt;module&amp;gt;&lt;/code&gt; is equivalent to running &lt;code&gt;python -m runpy &amp;lt;module&amp;gt;&lt;/code&gt;. The &lt;code&gt;runpy&lt;/code&gt; module was created to abstract the process of locating and executing modules on an operating system.&lt;/p&gt;
3675 &lt;p&gt;&lt;code&gt;runpy&lt;/code&gt; does a few things to run the target module:&lt;/p&gt;
3676 &lt;ul&gt;
3677 &lt;li&gt;Calls &lt;code&gt;__import__()&lt;/code&gt; for the module name you provided&lt;/li&gt;
3678 &lt;li&gt;Sets &lt;code&gt;__name__&lt;/code&gt; (the module name) to a namespace called &lt;code&gt;__main__&lt;/code&gt;&lt;/li&gt;
3679 &lt;li&gt;Executes the module within the &lt;code&gt;__main__&lt;/code&gt; namespace&lt;/li&gt;
3680 &lt;/ul&gt;
3681 &lt;p&gt;The &lt;code&gt;runpy&lt;/code&gt; module also supports executing directories and zip files.&lt;/p&gt;
3682 &lt;h4 id=&quot;input-via-filename&quot;&gt;Input via Filename&lt;/h4&gt;
3683 &lt;p&gt;If the first argument to &lt;code&gt;python&lt;/code&gt; was a filename, such as &lt;code&gt;python test.py&lt;/code&gt;, then CPython will open a file handle, similar to using &lt;code&gt;open()&lt;/code&gt; in Python and pass the handle to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L372&quot;&gt;&lt;code&gt;PyRun_SimpleFileExFlags()&lt;/code&gt;&lt;/a&gt; inside &lt;code&gt;Python/pythonrun.c&lt;/code&gt;.&lt;/p&gt;
3684 &lt;p&gt;There are 3 paths this function can take:&lt;/p&gt;
3685 &lt;ol&gt;
3686 &lt;li&gt;If the file path is a &lt;code&gt;.pyc&lt;/code&gt; file, it will call &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1145&quot;&gt;&lt;code&gt;run_pyc_file()&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
3687 &lt;li&gt;If the file path is a script file (&lt;code&gt;.py&lt;/code&gt;) it will run &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1032&quot;&gt;&lt;code&gt;PyRun_FileExFlags()&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
3688 &lt;li&gt;If the filepath is &lt;code&gt;stdin&lt;/code&gt; because the user ran &lt;code&gt;command | python&lt;/code&gt; then treat &lt;code&gt;stdin&lt;/code&gt; as a file handle and run &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1032&quot;&gt;&lt;code&gt;PyRun_FileExFlags()&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
3689 &lt;/ol&gt;
3690 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;
3691 &lt;span class=&quot;nf&quot;&gt;PyRun_SimpleFileExFlags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;FILE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;closeit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
3692                         &lt;span class=&quot;n&quot;&gt;PyCompilerFlags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3693 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3694  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3695     &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyImport_AddModule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;__main__&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3696  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3697 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maybe_pyc_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;closeit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3698 &lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3699 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;run_pyc_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pyc_fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3700 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3701         &lt;span class=&quot;cm&quot;&gt;/* When running from stdin, leave __main__.__loader__ alone */&lt;/span&gt;
3702         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strcmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;
3703             &lt;span class=&quot;n&quot;&gt;set_main_loader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;SourceFileLoader&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3704             &lt;span class=&quot;n&quot;&gt;fprintf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;python: failed to set __main__.__loader__&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3705             &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3706             &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3707         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3708 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyRun_FileExFlags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_file_input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
3709 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;                              &lt;span class=&quot;n&quot;&gt;closeit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3710 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3711  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3712     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3713 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3714 &lt;/pre&gt;&lt;/div&gt;
3715 
3716 &lt;h4 id=&quot;input-via-file-with-pyrun_fileexflags&quot;&gt;Input via File With &lt;code&gt;PyRun_FileExFlags()&lt;/code&gt;&lt;/h4&gt;
3717 &lt;p&gt;For &lt;code&gt;stdin&lt;/code&gt; and basic script files, CPython will pass the file handle to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1032&quot;&gt;&lt;code&gt;PyRun_FileExFlags()&lt;/code&gt;&lt;/a&gt; located in the &lt;code&gt;pythonrun.c&lt;/code&gt; file.&lt;/p&gt;
3718 &lt;p&gt;The purpose of &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1032&quot;&gt;&lt;code&gt;PyRun_FileExFlags()&lt;/code&gt;&lt;/a&gt; is similar to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L453&quot;&gt;&lt;code&gt;PyRun_SimpleStringFlags()&lt;/code&gt;&lt;/a&gt; used for the &lt;code&gt;-c&lt;/code&gt; input. CPython will load the file handle into &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1369&quot;&gt;&lt;code&gt;PyParser_ASTFromFileObject()&lt;/code&gt;&lt;/a&gt;. We&amp;rsquo;ll cover the Parser and AST modules in the next section.
3719 Because this is a full script, it doesn&amp;rsquo;t need the &lt;code&gt;PyImport_AddModule(&quot;__main__&quot;);&lt;/code&gt; step used by &lt;code&gt;-c&lt;/code&gt;:&lt;/p&gt;
3720 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
3721 &lt;span class=&quot;nf&quot;&gt;PyRun_FileExFlags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;FILE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename_str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
3722                   &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;locals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;closeit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyCompilerFlags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3723 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3724  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3725     &lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyParser_ASTFromFileObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
3726                                      &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3727  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3728     &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;run_mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;locals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3729 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3730 &lt;/pre&gt;&lt;/div&gt;
3731 
3732 &lt;p&gt;Identical to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L453&quot;&gt;&lt;code&gt;PyRun_SimpleStringFlags()&lt;/code&gt;&lt;/a&gt;, once &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1032&quot;&gt;&lt;code&gt;PyRun_FileExFlags()&lt;/code&gt;&lt;/a&gt; has created a Python module from the file, it sent it to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1125&quot;&gt;&lt;code&gt;run_mod()&lt;/code&gt;&lt;/a&gt; to be executed.&lt;/p&gt;
3733 &lt;p&gt;&lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1125&quot;&gt;&lt;code&gt;run_mod()&lt;/code&gt;&lt;/a&gt; is found within &lt;code&gt;Python/pythonrun.c&lt;/code&gt;, and sends the module to the AST to be compiled into a code object. Code objects are a format used to store the bytecode operations and the format kept in &lt;code&gt;.pyc&lt;/code&gt; files:&lt;/p&gt;
3734 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
3735 &lt;span class=&quot;nf&quot;&gt;run_mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mod_ty&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;locals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
3736             &lt;span class=&quot;n&quot;&gt;PyCompilerFlags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyArena&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3737 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3738     &lt;span class=&quot;n&quot;&gt;PyCodeObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3739     &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3740     &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyAST_CompileObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3741     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3742         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3743 
3744     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PySys_Audit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;exec&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;O&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3745         &lt;span class=&quot;n&quot;&gt;Py_DECREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3746         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3747     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3748 
3749     &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;run_eval_code_obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;locals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3750     &lt;span class=&quot;n&quot;&gt;Py_DECREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3751     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3752 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3753 &lt;/pre&gt;&lt;/div&gt;
3754 
3755 &lt;p&gt;We will cover the CPython compiler and bytecodes in the next section. The call to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1094&quot;&gt;&lt;code&gt;run_eval_code_obj()&lt;/code&gt;&lt;/a&gt; is a simple wrapper function that calls &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ceval.c#L716&quot;&gt;&lt;code&gt;PyEval_EvalCode()&lt;/code&gt;&lt;/a&gt; in the &lt;code&gt;Python/eval.c&lt;/code&gt; file. The &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ceval.c#L716&quot;&gt;&lt;code&gt;PyEval_EvalCode()&lt;/code&gt;&lt;/a&gt; function is the main evaluation loop for CPython, it iterates over each bytecode statement and executes it on your local machine.&lt;/p&gt;
3756 &lt;h4 id=&quot;input-via-compiled-bytecode-with-run_pyc_file&quot;&gt;Input via Compiled Bytecode With &lt;code&gt;run_pyc_file()&lt;/code&gt;&lt;/h4&gt;
3757 &lt;p&gt;In the &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L372&quot;&gt;&lt;code&gt;PyRun_SimpleFileExFlags()&lt;/code&gt;&lt;/a&gt; there was a clause for the user providing a file path to a &lt;code&gt;.pyc&lt;/code&gt; file. If the file path ended in &lt;code&gt;.pyc&lt;/code&gt; then instead of loading the file as a plain text file and parsing it, it will assume that the &lt;code&gt;.pyc&lt;/code&gt; file contains a code object written to disk. &lt;/p&gt;
3758 &lt;p&gt;The &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1145&quot;&gt;&lt;code&gt;run_pyc_file()&lt;/code&gt;&lt;/a&gt; function inside &lt;code&gt;Python/pythonrun.c&lt;/code&gt; then marshals the code object from the &lt;code&gt;.pyc&lt;/code&gt; file by using the file handle. &lt;strong&gt;Marshaling&lt;/strong&gt; is a technical term for copying the contents of a file into memory and converting them to a specific data structure. The code object data structure on the disk is the CPython compiler&amp;rsquo;s way to caching compiled code so that it doesn&amp;rsquo;t need to parse it every time the script is called:&lt;/p&gt;
3759 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
3760 &lt;span class=&quot;nf&quot;&gt;run_pyc_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;FILE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
3761              &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;locals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyCompilerFlags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3762 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3763     &lt;span class=&quot;n&quot;&gt;PyCodeObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3764     &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3765   &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3766 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyMarshal_ReadLastObjectFromFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3767 &lt;/span&gt;  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3768     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyCode_Check&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3769         &lt;span class=&quot;n&quot;&gt;Py_XDECREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3770         &lt;span class=&quot;n&quot;&gt;PyErr_SetString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyExc_RuntimeError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
3771                    &lt;span class=&quot;s&quot;&gt;&amp;quot;Bad code object in .pyc file&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3772         &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3773     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3774     &lt;span class=&quot;n&quot;&gt;fclose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3775 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyCodeObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3776 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;run_eval_code_obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;locals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3777 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3778         &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cf_flags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_flags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyCF_MASK&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3779     &lt;span class=&quot;n&quot;&gt;Py_DECREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3780     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3781 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3782 &lt;/pre&gt;&lt;/div&gt;
3783 
3784 &lt;p&gt;Once the code object has been marshaled to memory, it is sent to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1094&quot;&gt;&lt;code&gt;run_eval_code_obj()&lt;/code&gt;&lt;/a&gt;, which calls &lt;code&gt;Python/ceval.c&lt;/code&gt; to execute the code.&lt;/p&gt;
3785 &lt;h3 id=&quot;lexing-and-parsing&quot;&gt;Lexing and Parsing&lt;/h3&gt;
3786 &lt;p&gt;In the exploration of reading and executing Python files, we dived as deep as the parser and AST modules, with function calls to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1369&quot;&gt;&lt;code&gt;PyParser_ASTFromFileObject()&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
3787 &lt;p&gt;Sticking within &lt;code&gt;Python/pythonrun.c&lt;/code&gt;, the &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1369&quot;&gt;&lt;code&gt;PyParser_ASTFromFileObject()&lt;/code&gt;&lt;/a&gt; function will take a file handle, compiler flags and a &lt;code&gt;PyArena&lt;/code&gt; instance and convert the file object into a node object using &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Parser/parsetok.c#L163&quot;&gt;&lt;code&gt;PyParser_ParseFileObject()&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
3788 &lt;p&gt;With the node object, it will then convert that into a module using the AST function &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ast.c#L772&quot;&gt;&lt;code&gt;PyAST_FromNodeObject()&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
3789 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mod_ty&lt;/span&gt;
3790 &lt;span class=&quot;nf&quot;&gt;PyParser_ASTFromFileObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;FILE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
3791                            &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ps1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
3792                            &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ps2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyCompilerFlags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;errcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
3793                            &lt;span class=&quot;n&quot;&gt;PyArena&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3794 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3795     &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3796 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyParser_ParseFileObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
3797 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;                                       &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_PyParser_Grammar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
3798 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;                                       &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ps1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ps2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iflags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3799 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3800     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3801 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cf_flags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iflags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyCF_MASK&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3802 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyAST_FromNodeObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3803         &lt;span class=&quot;n&quot;&gt;PyNode_Free&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3804     &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3805     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3806 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3807 &lt;/pre&gt;&lt;/div&gt;
3808 
3809 &lt;p&gt;For &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Parser/parsetok.c#L163&quot;&gt;&lt;code&gt;PyParser_ParseFileObject()&lt;/code&gt;&lt;/a&gt; we switch to &lt;code&gt;Parser/parsetok.c&lt;/code&gt; and the parser-tokenizer stage of the CPython interpreter. This function has two important tasks:&lt;/p&gt;
3810 &lt;ol&gt;
3811 &lt;li&gt;Instantiate a tokenizer state &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Parser/tokenizer.h#L23&quot;&gt;&lt;code&gt;tok_state&lt;/code&gt;&lt;/a&gt; using &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Parser/tokenizer.h#L78&quot;&gt;&lt;code&gt;PyTokenizer_FromFile()&lt;/code&gt;&lt;/a&gt; in &lt;code&gt;Parser/tokenizer.c&lt;/code&gt;&lt;/li&gt;
3812 &lt;li&gt;Convert the tokens into a concrete parse tree (a list of &lt;code&gt;node&lt;/code&gt;) using &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Parser/parsetok.c#L232&quot;&gt;&lt;code&gt;parsetok()&lt;/code&gt;&lt;/a&gt; in &lt;code&gt;Parser/parsetok.c&lt;/code&gt; &lt;/li&gt;
3813 &lt;/ol&gt;
3814 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
3815 &lt;span class=&quot;nf&quot;&gt;PyParser_ParseFileObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;FILE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
3816                          &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;grammar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
3817                          &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ps1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ps2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
3818                          &lt;span class=&quot;n&quot;&gt;perrdetail&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;err_ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3819 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3820 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tok_state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3821 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3822 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tok&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyTokenizer_FromFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ps1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ps2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3823 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;err_ret&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;E_NOMEM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3824         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3825     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3826 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3827 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parsetok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err_ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
3828 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3829 &lt;/pre&gt;&lt;/div&gt;
3830 
3831 &lt;p&gt;&lt;code&gt;tok_state&lt;/code&gt; (defined in &lt;code&gt;Parser/tokenizer.h&lt;/code&gt;) is the data structure to store all temporary data generated by the tokenizer. It is returned to the parser-tokenizer as the data structure is required by &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Parser/parsetok.c#L232&quot;&gt;&lt;code&gt;parsetok()&lt;/code&gt;&lt;/a&gt; to develop the concrete syntax tree.&lt;/p&gt;
3832 &lt;p&gt;Inside &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Parser/parsetok.c#L232&quot;&gt;&lt;code&gt;parsetok()&lt;/code&gt;&lt;/a&gt;, it will use the &lt;code&gt;tok_state&lt;/code&gt; structure and make calls to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Parser/tokenizer.c#L1110&quot;&gt;&lt;code&gt;tok_get()&lt;/code&gt;&lt;/a&gt; in a loop until the file is exhausted and no more tokens can be found.&lt;/p&gt;
3833 &lt;p&gt;&lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Parser/tokenizer.c#L1110&quot;&gt;&lt;code&gt;tok_get()&lt;/code&gt;&lt;/a&gt;, defined in &lt;code&gt;Parser/tokenizer.c&lt;/code&gt; behaves like an iterator. It will keep returning the next token in the parse tree.&lt;/p&gt;
3834 &lt;p&gt;&lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Parser/tokenizer.c#L1110&quot;&gt;&lt;code&gt;tok_get()&lt;/code&gt;&lt;/a&gt; is one of the most complex functions in the whole CPython codebase. It has over 640 lines and includes decades of heritage with edge cases, new language features, and syntax.&lt;/p&gt;
3835 &lt;p&gt;One of the simpler examples would be the part that converts a newline break into a NEWLINE token:&lt;/p&gt;
3836 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;
3837 &lt;span class=&quot;nf&quot;&gt;tok_get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tok_state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p_start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p_end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3838 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3839 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3840     &lt;span class=&quot;cm&quot;&gt;/* Newline */&lt;/span&gt;
3841     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&amp;#39;\n&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3842         &lt;span class=&quot;n&quot;&gt;tok&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;atbol&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3843         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blankline&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tok&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;level&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3844             &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nextline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3845         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3846         &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p_start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tok&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3847         &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p_end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tok&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cur&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* Leave &amp;#39;\n&amp;#39; out of the string */&lt;/span&gt;
3848         &lt;span class=&quot;n&quot;&gt;tok&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cont_line&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3849         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tok&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;async_def&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3850             &lt;span class=&quot;cm&quot;&gt;/* We&amp;#39;re somewhere inside an &amp;#39;async def&amp;#39; function, and&lt;/span&gt;
3851 &lt;span class=&quot;cm&quot;&gt;               we&amp;#39;ve encountered a NEWLINE after its signature. */&lt;/span&gt;
3852             &lt;span class=&quot;n&quot;&gt;tok&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;async_def_nl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3853         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3854         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NEWLINE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3855     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3856 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
3857 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3858 &lt;/pre&gt;&lt;/div&gt;
3859 
3860 &lt;p&gt;In this case, &lt;code&gt;NEWLINE&lt;/code&gt; is a token, with a value defined in &lt;code&gt;Include/token.h&lt;/code&gt;. All tokens are constant &lt;code&gt;int&lt;/code&gt; values, and the &lt;code&gt;Include/token.h&lt;/code&gt; file was generated earlier when we ran &lt;code&gt;make regen-grammar&lt;/code&gt;.&lt;/p&gt;
3861 &lt;p&gt;The &lt;code&gt;node&lt;/code&gt; type returned by &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Parser/parsetok.c#L163&quot;&gt;&lt;code&gt;PyParser_ParseFileObject()&lt;/code&gt;&lt;/a&gt; is going to be essential for the next stage, converting a parse tree into an Abstract-Syntax-Tree (AST):&lt;/p&gt;
3862 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_node&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
3863     &lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt;               &lt;span class=&quot;n&quot;&gt;n_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3864     &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;                &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3865     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;n_lineno&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3866     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;n_col_offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3867     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;n_nchildren&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3868     &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_node&lt;/span&gt;        &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_child&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3869     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;n_end_lineno&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3870     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;n_end_col_offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3871 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
3872 &lt;/pre&gt;&lt;/div&gt;
3873 
3874 &lt;p&gt;Since the CST is a tree of syntax, token IDs, and symbols, it would be difficult for the compiler to make quick decisions based on the Python language.&lt;/p&gt;
3875 &lt;p&gt;That is why the next stage is to convert the CST into an AST, a much higher-level structure. This task is performed by the &lt;code&gt;Python/ast.c&lt;/code&gt; module, which has both a C and Python API.&lt;/p&gt;
3876 &lt;p&gt;Before you jump into the AST, there is a way to access the output from the parser stage. CPython has a standard library module &lt;code&gt;parser&lt;/code&gt;, which exposes the C functions with a Python API.&lt;/p&gt;
3877 &lt;p&gt;The module is documented as an implementation detail of CPython so that you won&amp;rsquo;t see it in other Python interpreters. Also the output from the functions is not that easy to read.&lt;/p&gt;
3878 &lt;p&gt;The output will be in the numeric form, using the token and symbol numbers generated by the &lt;code&gt;make regen-grammar&lt;/code&gt; stage, stored in &lt;code&gt;Include/token.h&lt;/code&gt;: &lt;/p&gt;
3879 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pprint&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pprint&lt;/span&gt;
3880 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;parser&lt;/span&gt;
3881 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parser&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;a + 1&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3882 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pprint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parser&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st2list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
3883 &lt;span class=&quot;go&quot;&gt;[258,&lt;/span&gt;
3884 &lt;span class=&quot;go&quot;&gt; [332,&lt;/span&gt;
3885 &lt;span class=&quot;go&quot;&gt;  [306,&lt;/span&gt;
3886 &lt;span class=&quot;go&quot;&gt;   [310,&lt;/span&gt;
3887 &lt;span class=&quot;go&quot;&gt;    [311,&lt;/span&gt;
3888 &lt;span class=&quot;go&quot;&gt;     [312,&lt;/span&gt;
3889 &lt;span class=&quot;go&quot;&gt;      [313,&lt;/span&gt;
3890 &lt;span class=&quot;go&quot;&gt;       [316,&lt;/span&gt;
3891 &lt;span class=&quot;go&quot;&gt;        [317,&lt;/span&gt;
3892 &lt;span class=&quot;go&quot;&gt;         [318,&lt;/span&gt;
3893 &lt;span class=&quot;go&quot;&gt;          [319,&lt;/span&gt;
3894 &lt;span class=&quot;go&quot;&gt;           [320,&lt;/span&gt;
3895 &lt;span class=&quot;go&quot;&gt;            [321, [322, [323, [324, [325, [1, &amp;#39;a&amp;#39;]]]]]],&lt;/span&gt;
3896 &lt;span class=&quot;go&quot;&gt;            [14, &amp;#39;+&amp;#39;],&lt;/span&gt;
3897 &lt;span class=&quot;go&quot;&gt;            [321, [322, [323, [324, [325, [2, &amp;#39;1&amp;#39;]]]]]]]]]]]]]]]]],&lt;/span&gt;
3898 &lt;span class=&quot;go&quot;&gt; [4, &amp;#39;&amp;#39;],&lt;/span&gt;
3899 &lt;span class=&quot;go&quot;&gt; [0, &amp;#39;&amp;#39;]]&lt;/span&gt;
3900 &lt;/pre&gt;&lt;/div&gt;
3901 
3902 &lt;p&gt;To make it easier to understand, you can take all the numbers in the &lt;code&gt;symbol&lt;/code&gt; and &lt;code&gt;token&lt;/code&gt; modules, put them into a dictionary and recursively replace the values in the output of &lt;code&gt;parser.st2list()&lt;/code&gt; with the names:&lt;/p&gt;
3903 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;symbol&lt;/span&gt;
3904 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;token&lt;/span&gt;
3905 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;parser&lt;/span&gt;
3906 
3907 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;lex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
3908     &lt;span class=&quot;n&quot;&gt;symbols&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;symbol&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;vm&quot;&gt;__dict__&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)}&lt;/span&gt;
3909     &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;vm&quot;&gt;__dict__&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)}&lt;/span&gt;
3910     &lt;span class=&quot;n&quot;&gt;lexicon&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;symbols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
3911     &lt;span class=&quot;n&quot;&gt;st&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parser&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3912     &lt;span class=&quot;n&quot;&gt;st_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parser&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st2list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3913 
3914     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
3915         &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
3916         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
3917             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
3918                 &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
3919             &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
3920                 &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lexicon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
3921                     &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lexicon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
3922                 &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
3923                     &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3924         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;
3925 
3926     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3927 &lt;/pre&gt;&lt;/div&gt;
3928 
3929 &lt;p&gt;You can run &lt;code&gt;lex()&lt;/code&gt; with a simple expression, like &lt;code&gt;a + 1&lt;/code&gt; to see how this is represented as a parser-tree:&lt;/p&gt;
3930 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pprint&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pprint&lt;/span&gt;
3931 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pprint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;a + 1&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
3932 
3933 &lt;span class=&quot;go&quot;&gt;[&amp;#39;eval_input&amp;#39;,&lt;/span&gt;
3934 &lt;span class=&quot;go&quot;&gt; [&amp;#39;testlist&amp;#39;,&lt;/span&gt;
3935 &lt;span class=&quot;go&quot;&gt;  [&amp;#39;test&amp;#39;,&lt;/span&gt;
3936 &lt;span class=&quot;go&quot;&gt;   [&amp;#39;or_test&amp;#39;,&lt;/span&gt;
3937 &lt;span class=&quot;go&quot;&gt;    [&amp;#39;and_test&amp;#39;,&lt;/span&gt;
3938 &lt;span class=&quot;go&quot;&gt;     [&amp;#39;not_test&amp;#39;,&lt;/span&gt;
3939 &lt;span class=&quot;go&quot;&gt;      [&amp;#39;comparison&amp;#39;,&lt;/span&gt;
3940 &lt;span class=&quot;go&quot;&gt;       [&amp;#39;expr&amp;#39;,&lt;/span&gt;
3941 &lt;span class=&quot;go&quot;&gt;        [&amp;#39;xor_expr&amp;#39;,&lt;/span&gt;
3942 &lt;span class=&quot;go&quot;&gt;         [&amp;#39;and_expr&amp;#39;,&lt;/span&gt;
3943 &lt;span class=&quot;go&quot;&gt;          [&amp;#39;shift_expr&amp;#39;,&lt;/span&gt;
3944 &lt;span class=&quot;go&quot;&gt;           [&amp;#39;arith_expr&amp;#39;,&lt;/span&gt;
3945 &lt;span class=&quot;go&quot;&gt;            [&amp;#39;term&amp;#39;,&lt;/span&gt;
3946 &lt;span class=&quot;go&quot;&gt;             [&amp;#39;factor&amp;#39;, [&amp;#39;power&amp;#39;, [&amp;#39;atom_expr&amp;#39;, [&amp;#39;atom&amp;#39;, [&amp;#39;NAME&amp;#39;, &amp;#39;a&amp;#39;]]]]]],&lt;/span&gt;
3947 &lt;span class=&quot;go&quot;&gt;            [&amp;#39;PLUS&amp;#39;, &amp;#39;+&amp;#39;],&lt;/span&gt;
3948 &lt;span class=&quot;go&quot;&gt;            [&amp;#39;term&amp;#39;,&lt;/span&gt;
3949 &lt;span class=&quot;go&quot;&gt;             [&amp;#39;factor&amp;#39;,&lt;/span&gt;
3950 &lt;span class=&quot;go&quot;&gt;              [&amp;#39;power&amp;#39;, [&amp;#39;atom_expr&amp;#39;, [&amp;#39;atom&amp;#39;, [&amp;#39;NUMBER&amp;#39;, &amp;#39;1&amp;#39;]]]]]]]]]]]]]]]]],&lt;/span&gt;
3951 &lt;span class=&quot;go&quot;&gt; [&amp;#39;NEWLINE&amp;#39;, &amp;#39;&amp;#39;],&lt;/span&gt;
3952 &lt;span class=&quot;go&quot;&gt; [&amp;#39;ENDMARKER&amp;#39;, &amp;#39;&amp;#39;]]&lt;/span&gt;
3953 &lt;/pre&gt;&lt;/div&gt;
3954 
3955 &lt;p&gt;In the output, you can see the symbols in lowercase, such as &lt;code&gt;&#39;test&#39;&lt;/code&gt; and the tokens in uppercase, such as &lt;code&gt;&#39;NUMBER&#39;&lt;/code&gt;.&lt;/p&gt;
3956 &lt;h3 id=&quot;abstract-syntax-trees&quot;&gt;Abstract Syntax Trees&lt;/h3&gt;
3957 &lt;p&gt;The next stage in the CPython interpreter is to convert the CST generated by the parser into something more logical that can be executed. The structure is a higher-level representation of the code, called an Abstract Syntax Tree (AST).&lt;/p&gt;
3958 &lt;p&gt;ASTs are produced inline with the CPython interpreter process, but you can also generate them in both Python using the &lt;code&gt;ast&lt;/code&gt; module in the Standard Library as well as through the C API.&lt;/p&gt;
3959 &lt;p&gt;Before diving into the C implementation of the AST, it would be useful to understand what an AST looks like for a simple piece of Python code.&lt;/p&gt;
3960 &lt;p&gt;To do this, here&amp;rsquo;s a simple app called &lt;code&gt;instaviz&lt;/code&gt; for this tutorial. It displays the AST and bytecode instructions (which we&amp;rsquo;ll cover later) in a Web UI.&lt;/p&gt;
3961 &lt;p&gt;To install &lt;code&gt;instaviz&lt;/code&gt;:&lt;/p&gt;
3962 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pip install instaviz
3963 &lt;/pre&gt;&lt;/div&gt;
3964 
3965 &lt;p&gt;Then, open up a REPL by running &lt;code&gt;python&lt;/code&gt; at the command line with no arguments:&lt;/p&gt;
3966 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;instaviz&lt;/span&gt;
3967 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
3968 &lt;span class=&quot;go&quot;&gt;       a = 1&lt;/span&gt;
3969 &lt;span class=&quot;go&quot;&gt;       b = a + 1&lt;/span&gt;
3970 &lt;span class=&quot;go&quot;&gt;       return b&lt;/span&gt;
3971 
3972 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instaviz&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
3973 &lt;/pre&gt;&lt;/div&gt;
3974 
3975 &lt;p&gt;You&amp;rsquo;ll see a notification on the command-line that a web server has started on port &lt;code&gt;8080&lt;/code&gt;. If you were using that port for something else, you can change it by calling &lt;code&gt;instaviz.show(example, port=9090)&lt;/code&gt; or another port number.&lt;/p&gt;
3976 &lt;p&gt;In the web browser, you can see the detailed breakdown of your function:&lt;/p&gt;
3977 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/screenshot.e148c89e3a9a.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/screenshot.e148c89e3a9a.png&quot; width=&quot;4802&quot; height=&quot;2566&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/screenshot.e148c89e3a9a.png&amp;amp;w=1200&amp;amp;sig=eeb7b21839b90e3726ea5db7bfa0aac05730b1fe 1200w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/screenshot.e148c89e3a9a.png&amp;amp;w=2401&amp;amp;sig=aed4aed922bbe4ef9c665a90c9244ebaa950e032 2401w, https://files.realpython.com/media/screenshot.e148c89e3a9a.png 4802w&quot; sizes=&quot;75vw&quot; alt=&quot;Instaviz screenshot&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
3978 &lt;p&gt;The bottom left graph is the function you declared in REPL, represented as an Abstract Syntax Tree. Each node in the tree is an AST type. They are found in the &lt;code&gt;ast&lt;/code&gt; module, and all inherit from &lt;code&gt;_ast.AST&lt;/code&gt;. &lt;/p&gt;
3979 &lt;p&gt;Some of the nodes have properties which link them to child nodes, unlike the CST, which has a generic child node property. &lt;/p&gt;
3980 &lt;p&gt;For example, if you click on the Assign node in the center, this links to the line &lt;code&gt;b = a + 1&lt;/code&gt;:&lt;/p&gt;
3981 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-19_at_1.24.17_pm.a5df8d873988.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border w-75&quot; src=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-19_at_1.24.17_pm.a5df8d873988.png&quot; width=&quot;2226&quot; height=&quot;1596&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-19_at_1.24.17_pm.a5df8d873988.png&amp;amp;w=556&amp;amp;sig=6a6f30034c85f411bd6c159df8aef50e899dda9c 556w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-19_at_1.24.17_pm.a5df8d873988.png&amp;amp;w=1113&amp;amp;sig=1b85cbf0bec4b114ef52d8a7bcdbfa08a1519237 1113w, https://files.realpython.com/media/Screen_Shot_2019-03-19_at_1.24.17_pm.a5df8d873988.png 2226w&quot; sizes=&quot;75vw&quot; alt=&quot;Instaviz screenshot 2&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
3982 &lt;p&gt;It has two properties:&lt;/p&gt;
3983 &lt;ol&gt;
3984 &lt;li&gt;&lt;strong&gt;&lt;code&gt;targets&lt;/code&gt;&lt;/strong&gt; is a list of names to assign. It is a list because you can assign to multiple variables with a single expression using unpacking&lt;/li&gt;
3985 &lt;li&gt;&lt;strong&gt;&lt;code&gt;value&lt;/code&gt;&lt;/strong&gt; is the value to assign, which in this case is a &lt;code&gt;BinOp&lt;/code&gt; statement, &lt;code&gt;a + 1&lt;/code&gt;.&lt;/li&gt;
3986 &lt;/ol&gt;
3987 &lt;p&gt;If you click on the &lt;code&gt;BinOp&lt;/code&gt; statement, it shows the properties of relevance:&lt;/p&gt;
3988 &lt;ul&gt;
3989 &lt;li&gt;&lt;strong&gt;&lt;code&gt;left&lt;/code&gt;:&lt;/strong&gt; the node to the left of the operator&lt;/li&gt;
3990 &lt;li&gt;&lt;strong&gt;&lt;code&gt;op&lt;/code&gt;:&lt;/strong&gt; the operator, in this case, an &lt;code&gt;Add&lt;/code&gt; node (&lt;code&gt;+&lt;/code&gt;) for addition&lt;/li&gt;
3991 &lt;li&gt;&lt;strong&gt;&lt;code&gt;right&lt;/code&gt;:&lt;/strong&gt; the node to the right of the operator&lt;/li&gt;
3992 &lt;/ul&gt;
3993 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-19_at_1.24.37_pm.21a11b49a820.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border w-75&quot; src=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-19_at_1.24.37_pm.21a11b49a820.png&quot; width=&quot;1708&quot; height=&quot;932&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-19_at_1.24.37_pm.21a11b49a820.png&amp;amp;w=427&amp;amp;sig=381d8bee4cc98dc98031abc6bc34ec376fd452b7 427w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-19_at_1.24.37_pm.21a11b49a820.png&amp;amp;w=854&amp;amp;sig=ccc0b88e9d762b2298a33ca2a03e826ee13f4193 854w, https://files.realpython.com/media/Screen_Shot_2019-03-19_at_1.24.37_pm.21a11b49a820.png 1708w&quot; sizes=&quot;75vw&quot; alt=&quot;Instaviz screenshot 3&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
3994 &lt;p&gt;Compiling an AST in C is not a straightforward task, so the &lt;code&gt;Python/ast.c&lt;/code&gt; module is over 5000 lines of code.&lt;/p&gt;
3995 &lt;p&gt;There are a few entry points, forming part of the AST&amp;rsquo;s public API. In the last section on the lexer and parser, you stopped when you&amp;rsquo;d reached the call to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ast.c#L772&quot;&gt;&lt;code&gt;PyAST_FromNodeObject()&lt;/code&gt;&lt;/a&gt;. By this stage, the Python interpreter process had created a CST in the format of &lt;code&gt;node *&lt;/code&gt; tree.&lt;/p&gt;
3996 &lt;p&gt;Jumping then into &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ast.c#L772&quot;&gt;&lt;code&gt;PyAST_FromNodeObject()&lt;/code&gt;&lt;/a&gt; inside &lt;code&gt;Python/ast.c&lt;/code&gt;, you can see it receives the &lt;code&gt;node *&lt;/code&gt; tree, the filename, compiler flags, and the &lt;code&gt;PyArena&lt;/code&gt;.&lt;/p&gt;
3997 &lt;p&gt;The return type from this function is &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Include/ast.h#L10&quot;&gt;&lt;code&gt;mod_ty&lt;/code&gt;&lt;/a&gt;, defined in &lt;code&gt;Include/Python-ast.h&lt;/code&gt;. &lt;code&gt;mod_ty&lt;/code&gt; is a container structure for one of the 5 module types in Python:&lt;/p&gt;
3998 &lt;ol&gt;
3999 &lt;li&gt;&lt;code&gt;Module&lt;/code&gt; &lt;/li&gt;
4000 &lt;li&gt;&lt;code&gt;Interactive&lt;/code&gt;&lt;/li&gt;
4001 &lt;li&gt;&lt;code&gt;Expression&lt;/code&gt;&lt;/li&gt;
4002 &lt;li&gt;&lt;code&gt;FunctionType&lt;/code&gt;&lt;/li&gt;
4003 &lt;li&gt;&lt;code&gt;Suite&lt;/code&gt;&lt;/li&gt;
4004 &lt;/ol&gt;
4005 &lt;p&gt;In &lt;code&gt;Include/Python-ast.h&lt;/code&gt; you can see that an &lt;code&gt;Expression&lt;/code&gt; type requires a field &lt;code&gt;body&lt;/code&gt;, which is an &lt;code&gt;expr_ty&lt;/code&gt; type. The &lt;code&gt;expr_ty&lt;/code&gt; type is also defined in &lt;code&gt;Include/Python-ast.h&lt;/code&gt;:&lt;/p&gt;
4006 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_mod_kind&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module_kind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Interactive_kind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression_kind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4007                  &lt;span class=&quot;n&quot;&gt;FunctionType_kind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Suite_kind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
4008 &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_mod&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4009     &lt;span class=&quot;k&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_mod_kind&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4010     &lt;span class=&quot;k&quot;&gt;union&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4011         &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4012             &lt;span class=&quot;n&quot;&gt;asdl_seq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4013             &lt;span class=&quot;n&quot;&gt;asdl_seq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type_ignores&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4014         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4015 
4016         &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4017             &lt;span class=&quot;n&quot;&gt;asdl_seq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4018         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Interactive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4019 
4020         &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4021             &lt;span class=&quot;n&quot;&gt;expr_ty&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4022         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4023 
4024         &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4025             &lt;span class=&quot;n&quot;&gt;asdl_seq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argtypes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4026             &lt;span class=&quot;n&quot;&gt;expr_ty&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;returns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4027         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FunctionType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4028 
4029         &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4030             &lt;span class=&quot;n&quot;&gt;asdl_seq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4031         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Suite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4032 
4033     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4034 &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
4035 &lt;/pre&gt;&lt;/div&gt;
4036 
4037 &lt;p&gt;The AST types are all listed in &lt;code&gt;Parser/Python.asdl&lt;/code&gt;. You will see the module types, statement types, expression types, operators, and comprehensions all listed. The names of the types in this document relate to the classes generated by the AST and the same classes named in the &lt;code&gt;ast&lt;/code&gt; standard module library.&lt;/p&gt;
4038 &lt;p&gt;The parameters and names in &lt;code&gt;Include/Python-ast.h&lt;/code&gt; correlate directly to those specified in &lt;code&gt;Parser/Python.asdl&lt;/code&gt;:&lt;/p&gt;
4039 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;-- ASDL&amp;#39;s 5 builtin types are:
4040 -- identifier, int, string, object, constant
4041 
4042 module Python
4043 {
4044     mod = Module(stmt* body, type_ignore *type_ignores)
4045         | Interactive(stmt* body)
4046 &lt;span class=&quot;hll&quot;&gt;        | Expression(expr body)
4047 &lt;/span&gt;        | FunctionType(expr* argtypes, expr returns)
4048 &lt;/pre&gt;&lt;/div&gt;
4049 
4050 &lt;p&gt;The C header file and structures are there so that the &lt;code&gt;Python/ast.c&lt;/code&gt; program can quickly generate the structures with pointers to the relevant data.&lt;/p&gt;
4051 &lt;p&gt;Looking at &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ast.c#L772&quot;&gt;&lt;code&gt;PyAST_FromNodeObject()&lt;/code&gt;&lt;/a&gt; you can see that it is essentially a &lt;code&gt;switch&lt;/code&gt; statement around the result from &lt;code&gt;TYPE(n)&lt;/code&gt;. &lt;code&gt;TYPE()&lt;/code&gt; is one of the core functions used by the AST to determine what type a node in the concrete syntax tree is. In the case of &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ast.c#L772&quot;&gt;&lt;code&gt;PyAST_FromNodeObject()&lt;/code&gt;&lt;/a&gt; it&amp;rsquo;s just looking at the first node, so it can only be one of the module types defined as &lt;code&gt;Module&lt;/code&gt;, &lt;code&gt;Interactive&lt;/code&gt;, &lt;code&gt;Expression&lt;/code&gt;, &lt;code&gt;FunctionType&lt;/code&gt;.&lt;/p&gt;
4052 &lt;p&gt;The result of &lt;code&gt;TYPE()&lt;/code&gt; will be either a symbol or token type, which we&amp;rsquo;re very familiar with by this stage.&lt;/p&gt;
4053 &lt;p&gt;For &lt;code&gt;file_input&lt;/code&gt;, the results should be a &lt;code&gt;Module&lt;/code&gt;. Modules are a series of statements, of which there are a few types. The logic to traverse the children of &lt;code&gt;n&lt;/code&gt; and create statement nodes is within &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ast.c#L4512&quot;&gt;&lt;code&gt;ast_for_stmt()&lt;/code&gt;&lt;/a&gt;. This function is called either once, if there is only 1 statement in the module, or in a loop if there are many. The resulting &lt;code&gt;Module&lt;/code&gt; is then returned with the &lt;code&gt;PyArena&lt;/code&gt;.&lt;/p&gt;
4054 &lt;p&gt;For &lt;code&gt;eval_input&lt;/code&gt;, the result should be an &lt;code&gt;Expression&lt;/code&gt;. The result from &lt;code&gt;CHILD(n ,0)&lt;/code&gt;, which is the first child of &lt;code&gt;n&lt;/code&gt; is passed to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ast.c#L3246&quot;&gt;&lt;code&gt;ast_for_testlist()&lt;/code&gt;&lt;/a&gt; which returns an &lt;code&gt;expr_ty&lt;/code&gt; type. This &lt;code&gt;expr_ty&lt;/code&gt; is sent to &lt;code&gt;Expression()&lt;/code&gt; with the PyArena to create an expression node, and then passed back as a result:&lt;/p&gt;
4055 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mod_ty&lt;/span&gt;
4056 &lt;span class=&quot;nf&quot;&gt;PyAST_FromNodeObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyCompilerFlags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4057                      &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyArena&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4058 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4059     &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4060     &lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TYPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4061         &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;file_input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4062             &lt;span class=&quot;n&quot;&gt;stmts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_Py_asdl_seq_new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_stmts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4063             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stmts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4064                 &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4065             &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NCH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4066                 &lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CHILD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4067                 &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TYPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NEWLINE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4068                     &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4069                 &lt;span class=&quot;n&quot;&gt;REQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stmt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4070                 &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_stmts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4071                 &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4072 &lt;span class=&quot;hll&quot;&gt;                    &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ast_for_stmt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4073 &lt;/span&gt;                    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4074                         &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4075                     &lt;span class=&quot;n&quot;&gt;asdl_seq_SET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stmts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4076                 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4077                 &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4078                     &lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CHILD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4079                     &lt;span class=&quot;n&quot;&gt;REQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;simple_stmt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4080                     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4081 &lt;span class=&quot;hll&quot;&gt;                        &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ast_for_stmt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CHILD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
4082 &lt;/span&gt;                        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4083                             &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4084                         &lt;span class=&quot;n&quot;&gt;asdl_seq_SET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stmts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4085                     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4086                 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4087             &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4088 
4089             &lt;span class=&quot;cm&quot;&gt;/* Type ignores are stored under the ENDMARKER in file_input. */&lt;/span&gt;
4090             &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4091 
4092 &lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stmts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type_ignores&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4093 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4094         &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;eval_input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4095             &lt;span class=&quot;n&quot;&gt;expr_ty&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;testlist_ast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4096 
4097             &lt;span class=&quot;cm&quot;&gt;/* XXX Why not comp_for here? */&lt;/span&gt;
4098 &lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;n&quot;&gt;testlist_ast&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ast_for_testlist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CHILD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
4099 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;testlist_ast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4100                 &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4101 &lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;testlist_ast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4102 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4103         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4104         &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;single_input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4105             &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4106             &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4107         &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;func_type_input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4108             &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4109         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4110     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4111 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4112 &lt;/pre&gt;&lt;/div&gt;
4113 
4114 &lt;p&gt;Inside the &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ast.c#L4512&quot;&gt;&lt;code&gt;ast_for_stmt()&lt;/code&gt;&lt;/a&gt; function, there is another &lt;code&gt;switch&lt;/code&gt; statement for each possible statement type (&lt;code&gt;simple_stmt&lt;/code&gt;, &lt;code&gt;compound_stmt&lt;/code&gt;, and so on) and the code to determine the arguments to the node class.&lt;/p&gt;
4115 &lt;p&gt;One of the simpler functions is for the power expression, i.e., &lt;code&gt;2**4&lt;/code&gt; is 2 to the power of 4. This function starts by getting the &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ast.c#L2770&quot;&gt;&lt;code&gt;ast_for_atom_expr()&lt;/code&gt;&lt;/a&gt;, which is the number &lt;code&gt;2&lt;/code&gt; in our example, then if that has one child, it returns the atomic expression. If it has more than one child, it will get the right-hand (the number &lt;code&gt;4&lt;/code&gt;) and return a &lt;code&gt;BinOp&lt;/code&gt; (binary operation) with the operator as &lt;code&gt;Pow&lt;/code&gt; (power), the left hand of &lt;code&gt;e&lt;/code&gt; (2), and the right hand of &lt;code&gt;f&lt;/code&gt; (4):&lt;/p&gt;
4116 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expr_ty&lt;/span&gt;
4117 &lt;span class=&quot;nf&quot;&gt;ast_for_power&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compiling&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4118 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4119     &lt;span class=&quot;cm&quot;&gt;/* power: atom trailer* (&amp;#39;**&amp;#39; factor)*&lt;/span&gt;
4120 &lt;span class=&quot;cm&quot;&gt;     */&lt;/span&gt;
4121     &lt;span class=&quot;n&quot;&gt;expr_ty&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4122     &lt;span class=&quot;n&quot;&gt;REQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;power&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4123     &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ast_for_atom_expr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CHILD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
4124     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4125         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4126     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NCH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4127         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4128     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TYPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CHILD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NCH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;factor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4129         &lt;span class=&quot;n&quot;&gt;expr_ty&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ast_for_expr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CHILD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NCH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
4130         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4131             &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4132         &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BinOp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Pow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LINENO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_col_offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4133                   &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_end_lineno&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_end_col_offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4134     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4135     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4136 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4137 &lt;/pre&gt;&lt;/div&gt;
4138 
4139 &lt;p&gt;You can see the result of this if you send a short function to the &lt;code&gt;instaviz&lt;/code&gt; module:&lt;/p&gt;
4140 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
4141 &lt;span class=&quot;go&quot;&gt;       2**4&lt;/span&gt;
4142 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;instaviz&lt;/span&gt;
4143 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instaviz&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4144 &lt;/pre&gt;&lt;/div&gt;
4145 
4146 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-19_at_5.34.51_pm.c3a1e8d717f5.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border w-75&quot; src=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-19_at_5.34.51_pm.c3a1e8d717f5.png&quot; width=&quot;1708&quot; height=&quot;1094&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-19_at_5.34.51_pm.c3a1e8d717f5.png&amp;amp;w=427&amp;amp;sig=68167794b71cec6447aa8fcb4d22ca738a2e2ce4 427w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-19_at_5.34.51_pm.c3a1e8d717f5.png&amp;amp;w=854&amp;amp;sig=feeef6a7ce0b14d39c57956f77320125bcce43bf 854w, https://files.realpython.com/media/Screen_Shot_2019-03-19_at_5.34.51_pm.c3a1e8d717f5.png 1708w&quot; sizes=&quot;75vw&quot; alt=&quot;Instaviz screenshot 4&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
4147 &lt;p&gt;In the UI you can also see the corresponding properties:&lt;/p&gt;
4148 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-19_at_5.36.34_pm.0067235460de.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border w-75&quot; src=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-19_at_5.36.34_pm.0067235460de.png&quot; width=&quot;1708&quot; height=&quot;630&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-19_at_5.36.34_pm.0067235460de.png&amp;amp;w=427&amp;amp;sig=91663308ef854874e262756b2a28e598d263fff5 427w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-19_at_5.36.34_pm.0067235460de.png&amp;amp;w=854&amp;amp;sig=fbfcdd33bcf685f91ebe0559b4135ead2b296d7e 854w, https://files.realpython.com/media/Screen_Shot_2019-03-19_at_5.36.34_pm.0067235460de.png 1708w&quot; sizes=&quot;75vw&quot; alt=&quot;Instaviz screenshot 5&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
4149 &lt;p&gt;In summary, each statement type and expression has a corresponding &lt;code&gt;ast_for_*()&lt;/code&gt; function to create it. The arguments are defined in &lt;code&gt;Parser/Python.asdl&lt;/code&gt; and exposed via the &lt;code&gt;ast&lt;/code&gt; module in the standard library. If an expression or statement has children, then it will call the corresponding &lt;code&gt;ast_for_*&lt;/code&gt; child function in a depth-first traversal.&lt;/p&gt;
4150 &lt;h3 id=&quot;conclusion_1&quot;&gt;Conclusion&lt;/h3&gt;
4151 &lt;p&gt;CPython&amp;rsquo;s versatility and low-level execution API make it the ideal candidate for an embedded scripting engine. You will see CPython used in many UI applications, such as Game Design, 3D graphics and system automation. &lt;/p&gt;
4152 &lt;p&gt;The interpreter process is flexible and efficient, and now you have an understanding of how it works you&amp;rsquo;re ready to understand the compiler.&lt;/p&gt;
4153 &lt;h2 h1=&quot;h1&quot; id=&quot;part-3-the-cpython-compiler-and-execution-loop&quot;&gt;Part 3: The CPython Compiler and Execution Loop&lt;/h2&gt;
4154 &lt;p&gt;In Part 2, you saw how the CPython interpreter takes an input, such as a file or string, and converts it into a logical Abstract Syntax Tree. We&amp;rsquo;re still not at the stage where this code can be executed. Next, we have to go deeper to convert the Abstract Syntax Tree into a set of sequential commands that the CPU can understand. &lt;/p&gt;
4155 &lt;h3 id=&quot;compiling&quot;&gt;Compiling&lt;/h3&gt;
4156 &lt;p&gt;Now the interpreter has an AST with the properties required for each of the operations, functions, classes, and namespaces. It is the job of the compiler to turn the AST into something the CPU can understand.&lt;/p&gt;
4157 &lt;p&gt;This compilation task is split into 2 parts:&lt;/p&gt;
4158 &lt;ol&gt;
4159 &lt;li&gt;Traverse the tree and create a control-flow-graph, which represents the logical sequence for execution&lt;/li&gt;
4160 &lt;li&gt;Convert the nodes in the CFG to smaller, executable statements, known as byte-code&lt;/li&gt;
4161 &lt;/ol&gt;
4162 &lt;p&gt;Earlier, we were looking at how files are executed, and the &lt;code&gt;PyRun_FileExFlags()&lt;/code&gt; function in &lt;code&gt;Python/pythonrun.c&lt;/code&gt;. Inside this function, we converted the &lt;code&gt;FILE&lt;/code&gt; handle into a &lt;code&gt;mod&lt;/code&gt;, of type &lt;code&gt;mod_ty&lt;/code&gt;. This task was completed by &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1369&quot;&gt;&lt;code&gt;PyParser_ASTFromFileObject()&lt;/code&gt;&lt;/a&gt;, which in turns calls the &lt;code&gt;tokenizer&lt;/code&gt;, &lt;code&gt;parser-tokenizer&lt;/code&gt; and then the AST:&lt;/p&gt;
4163 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
4164 &lt;span class=&quot;nf&quot;&gt;PyRun_FileExFlags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;FILE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename_str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4165                   &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;locals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;closeit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyCompilerFlags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4166 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4167  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4168 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyParser_ASTFromFileObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4169 &lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4170 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;run_mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;locals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4171 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4172 &lt;/pre&gt;&lt;/div&gt;
4173 
4174 &lt;p&gt;The resulting module from the call to is sent to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1125&quot;&gt;&lt;code&gt;run_mod()&lt;/code&gt;&lt;/a&gt; still in &lt;code&gt;Python/pythonrun.c&lt;/code&gt;. This is a small function that gets a &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Include/code.h#L69&quot;&gt;&lt;code&gt;PyCodeObject&lt;/code&gt;&lt;/a&gt; from &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L312&quot;&gt;&lt;code&gt;PyAST_CompileObject()&lt;/code&gt;&lt;/a&gt; and sends it on to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1094&quot;&gt;&lt;code&gt;run_eval_code_obj()&lt;/code&gt;&lt;/a&gt;. You will tackle &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1094&quot;&gt;&lt;code&gt;run_eval_code_obj()&lt;/code&gt;&lt;/a&gt; in the next section:&lt;/p&gt;
4175 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
4176 &lt;span class=&quot;nf&quot;&gt;run_mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mod_ty&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;locals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4177             &lt;span class=&quot;n&quot;&gt;PyCompilerFlags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyArena&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4178 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4179     &lt;span class=&quot;n&quot;&gt;PyCodeObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4180     &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4181 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyAST_CompileObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4182 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4183         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4184 
4185     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PySys_Audit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;exec&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;O&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4186         &lt;span class=&quot;n&quot;&gt;Py_DECREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4187         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4188     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4189 
4190 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;run_eval_code_obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;locals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4191 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;Py_DECREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4192     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4193 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4194 &lt;/pre&gt;&lt;/div&gt;
4195 
4196 &lt;p&gt;The &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L312&quot;&gt;&lt;code&gt;PyAST_CompileObject()&lt;/code&gt;&lt;/a&gt; function is the main entry point to the CPython compiler. It takes a Python module as its primary argument, along with the name of the file, the globals, locals, and the &lt;code&gt;PyArena&lt;/code&gt; all created earlier in the interpreter process.&lt;/p&gt;
4197 &lt;p&gt;We&amp;rsquo;re starting to get into the guts of the CPython compiler now, with decades of development and Computer Science theory behind it. Don&amp;rsquo;t be put off by the language. Once we break down the compiler into logical steps, it&amp;rsquo;ll make sense.&lt;/p&gt;
4198 &lt;p&gt;Before the compiler starts, a global compiler state is created. This type, &lt;code&gt;compiler&lt;/code&gt; is defined in &lt;code&gt;Python/compile.c&lt;/code&gt; and contains properties used by the compiler to remember the compiler flags, the stack, and the &lt;code&gt;PyArena&lt;/code&gt;:&lt;/p&gt;
4199 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compiler&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4200     &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4201     &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;symtable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4202     &lt;span class=&quot;n&quot;&gt;PyFutureFeatures&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_future&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* pointer to module&amp;#39;s __future__ */&lt;/span&gt;
4203     &lt;span class=&quot;n&quot;&gt;PyCompilerFlags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4204 
4205     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c_optimize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;              &lt;span class=&quot;cm&quot;&gt;/* optimization level */&lt;/span&gt;
4206     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c_interactive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;           &lt;span class=&quot;cm&quot;&gt;/* true if in interactive mode */&lt;/span&gt;
4207     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c_nestlevel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4208     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c_do_not_emit_bytecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;cm&quot;&gt;/* The compiler won&amp;#39;t emit any bytecode&lt;/span&gt;
4209 &lt;span class=&quot;cm&quot;&gt;                                    if this value is different from zero.&lt;/span&gt;
4210 &lt;span class=&quot;cm&quot;&gt;                                    This can be used to temporarily visit&lt;/span&gt;
4211 &lt;span class=&quot;cm&quot;&gt;                                    nodes without emitting bytecode to&lt;/span&gt;
4212 &lt;span class=&quot;cm&quot;&gt;                                    check only errors. */&lt;/span&gt;
4213 
4214     &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_const_cache&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;     &lt;span class=&quot;cm&quot;&gt;/* Python dict holding all constants,&lt;/span&gt;
4215 &lt;span class=&quot;cm&quot;&gt;                                    including names tuple */&lt;/span&gt;
4216     &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compiler_unit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* compiler state for current block */&lt;/span&gt;
4217     &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;           &lt;span class=&quot;cm&quot;&gt;/* Python list holding compiler_unit ptrs */&lt;/span&gt;
4218     &lt;span class=&quot;n&quot;&gt;PyArena&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;            &lt;span class=&quot;cm&quot;&gt;/* pointer to memory allocation arena */&lt;/span&gt;
4219 &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
4220 &lt;/pre&gt;&lt;/div&gt;
4221 
4222 &lt;p&gt;Inside &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L312&quot;&gt;&lt;code&gt;PyAST_CompileObject()&lt;/code&gt;&lt;/a&gt;, there are 11 main steps happening:&lt;/p&gt;
4223 &lt;ol&gt;
4224 &lt;li&gt;Create an empty &lt;code&gt;__doc__&lt;/code&gt; property to the module if it doesn&amp;rsquo;t exist.&lt;/li&gt;
4225 &lt;li&gt;Create an empty &lt;code&gt;__annotations__&lt;/code&gt; property to the module if it doesn&amp;rsquo;t exist.&lt;/li&gt;
4226 &lt;li&gt;Set the filename of the global compiler state to the filename argument.&lt;/li&gt;
4227 &lt;li&gt;Set the memory allocation arena for the compiler to the one used by the interpreter.&lt;/li&gt;
4228 &lt;li&gt;Copy any &lt;code&gt;__future__&lt;/code&gt; flags in the module to the future flags in the compiler.&lt;/li&gt;
4229 &lt;li&gt;Merge runtime flags provided by the command-line or environment variables.&lt;/li&gt;
4230 &lt;li&gt;Enable any &lt;code&gt;__future__&lt;/code&gt; features in the compiler.&lt;/li&gt;
4231 &lt;li&gt;Set the optimization level to the provided argument, or default.&lt;/li&gt;
4232 &lt;li&gt;Build a symbol table from the module object.&lt;/li&gt;
4233 &lt;li&gt;Run the compiler with the compiler state and return the code object.&lt;/li&gt;
4234 &lt;li&gt;Free any allocated memory by the compiler.&lt;/li&gt;
4235 &lt;/ol&gt;
4236 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyCodeObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
4237 &lt;span class=&quot;nf&quot;&gt;PyAST_CompileObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mod_ty&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyCompilerFlags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4238                    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;optimize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyArena&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4239 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4240     &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compiler&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4241     &lt;span class=&quot;n&quot;&gt;PyCodeObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4242     &lt;span class=&quot;n&quot;&gt;PyCompilerFlags&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;local_flags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_PyCompilerFlags_INIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4243     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;merged&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4244     &lt;span class=&quot;n&quot;&gt;PyConfig&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_PyInterpreterState_GET_UNSAFE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4245 &lt;span class=&quot;hll&quot;&gt;
4246 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__doc__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4247         &lt;span class=&quot;n&quot;&gt;__doc__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyUnicode_InternFromString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;__doc__&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4248         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__doc__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4249             &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4250     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4251 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__annotations__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4252 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;__annotations__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyUnicode_InternFromString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;__annotations__&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4253         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__annotations__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4254             &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4255     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4256     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compiler_init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
4257         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4258 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;Py_INCREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4259 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_filename&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4260 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_arena&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4261 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_future&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyFuture_FromASTObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4262     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_future&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4263         &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;finally&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4264     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4265         &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;local_flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4266 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4267 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;merged&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_future&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ff_features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cf_flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4268 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_future&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ff_features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;merged&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4269     &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cf_flags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;merged&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4270 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_flags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4271 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_optimize&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimize&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;optimization_level&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;optimize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4272     &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_nestlevel&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4273     &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_do_not_emit_bytecode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4274 
4275     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_PyAST_Optimize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arena&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_optimize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4276         &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;finally&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4277 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4278 &lt;/span&gt;
4279     &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_st&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PySymtable_BuildObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_future&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4280     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_st&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4281         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyErr_Occurred&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
4282             &lt;span class=&quot;n&quot;&gt;PyErr_SetString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyExc_SystemError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;no symtable&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4283         &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;finally&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4284 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4285 &lt;/span&gt;
4286     &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compiler_mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4287 
4288  &lt;span class=&quot;nl&quot;&gt;finally&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4289     &lt;span class=&quot;n&quot;&gt;compiler_free&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4290     &lt;span class=&quot;n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyErr_Occurred&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
4291     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4292 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4293 &lt;/pre&gt;&lt;/div&gt;
4294 
4295 &lt;h4 id=&quot;future-flags-and-compiler-flags&quot;&gt;Future Flags and Compiler Flags&lt;/h4&gt;
4296 &lt;p&gt;Before the compiler runs, there are two types of flags to toggle the features inside the compiler. These come from two places:&lt;/p&gt;
4297 &lt;ol&gt;
4298 &lt;li&gt;The interpreter state, which may have been command-line options, set in &lt;code&gt;pyconfig.h&lt;/code&gt; or via environment variables&lt;/li&gt;
4299 &lt;li&gt;The use of &lt;code&gt;__future__&lt;/code&gt; statements inside the actual source code of the module&lt;/li&gt;
4300 &lt;/ol&gt;
4301 &lt;p&gt;To distinguish the two types of flags, think that the &lt;code&gt;__future__&lt;/code&gt; flags are required because of the syntax or features in that specific module. For example, Python 3.7 introduced delayed evaluation of type hints through the &lt;code&gt;annotations&lt;/code&gt; future flag:&lt;/p&gt;
4302 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;__future__&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;annotations&lt;/span&gt;
4303 &lt;/pre&gt;&lt;/div&gt;
4304 
4305 &lt;p&gt;The code after this statement might use unresolved type hints, so the &lt;code&gt;__future__&lt;/code&gt; statement is required. Otherwise, the module wouldn&amp;rsquo;t import. It would be unmaintainable to manually request that the person importing the module enable this specific compiler flag.&lt;/p&gt;
4306 &lt;p&gt;The other compiler flags are specific to the environment, so they might change the way the code executes or the way the compiler runs, but they shouldn&amp;rsquo;t link to the source in the same way that &lt;code&gt;__future__&lt;/code&gt; statements do.&lt;/p&gt;
4307 &lt;p&gt;One example of a compiler flag would be the &lt;a href=&quot;https://docs.python.org/3/using/cmdline.html#cmdoption-o&quot;&gt;&lt;code&gt;-O&lt;/code&gt; flag for optimizing the use of &lt;code&gt;assert&lt;/code&gt; statements&lt;/a&gt;. This flag disables any &lt;code&gt;assert&lt;/code&gt; statements, which may have been put in the code for &lt;a href=&quot;https://realpython.com/python-debugging-pdb/&quot;&gt;debugging purposes&lt;/a&gt;.
4308 It can also be enabled with the &lt;code&gt;PYTHONOPTIMIZE=1&lt;/code&gt; environment variable setting.&lt;/p&gt;
4309 &lt;h4 id=&quot;symbol-tables&quot;&gt;Symbol Tables&lt;/h4&gt;
4310 &lt;p&gt;In &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L312&quot;&gt;&lt;code&gt;PyAST_CompileObject()&lt;/code&gt;&lt;/a&gt; there was a reference to a &lt;code&gt;symtable&lt;/code&gt; and a call to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/symtable.c#L262&quot;&gt;&lt;code&gt;PySymtable_BuildObject()&lt;/code&gt;&lt;/a&gt; with the module to be executed.&lt;/p&gt;
4311 &lt;p&gt;The purpose of the symbol table is to provide a list of namespaces, globals, and locals for the compiler to use for referencing and resolving scopes.&lt;/p&gt;
4312 &lt;p&gt;The &lt;code&gt;symtable&lt;/code&gt; structure in &lt;code&gt;Include/symtable.h&lt;/code&gt; is well documented, so it&amp;rsquo;s clear what each of the fields is for. There should be one symtable instance for the compiler, so namespacing becomes essential. &lt;/p&gt;
4313 &lt;p&gt;If you create a function called &lt;code&gt;resolve_names()&lt;/code&gt; in one module and declare another function with the same name in another module, you want to be sure which one is called. The symtable serves this purpose, as well as ensuring that variables declared within a narrow scope don&amp;rsquo;t automatically become globals (after all, this isn&amp;rsquo;t JavaScript):&lt;/p&gt;
4314 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;symtable&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4315     &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st_filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;          &lt;span class=&quot;cm&quot;&gt;/* name of file being compiled,&lt;/span&gt;
4316 &lt;span class=&quot;cm&quot;&gt;                                       decoded from the filesystem encoding */&lt;/span&gt;
4317     &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_symtable_entry&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st_cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* current symbol table entry */&lt;/span&gt;
4318     &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_symtable_entry&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st_top&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* symbol table entry for module */&lt;/span&gt;
4319     &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st_blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;            &lt;span class=&quot;cm&quot;&gt;/* dict: map AST node addresses&lt;/span&gt;
4320 &lt;span class=&quot;cm&quot;&gt;                                     *       to symbol table entries */&lt;/span&gt;
4321     &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st_stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;             &lt;span class=&quot;cm&quot;&gt;/* list: stack of namespace info */&lt;/span&gt;
4322     &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st_global&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;            &lt;span class=&quot;cm&quot;&gt;/* borrowed ref to st_top-&amp;gt;ste_symbols */&lt;/span&gt;
4323     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;st_nblocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;                 &lt;span class=&quot;cm&quot;&gt;/* number of blocks used. kept for&lt;/span&gt;
4324 &lt;span class=&quot;cm&quot;&gt;                                       consistency with the corresponding&lt;/span&gt;
4325 &lt;span class=&quot;cm&quot;&gt;                                       compiler structure */&lt;/span&gt;
4326     &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st_private&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;           &lt;span class=&quot;cm&quot;&gt;/* name of current class or NULL */&lt;/span&gt;
4327     &lt;span class=&quot;n&quot;&gt;PyFutureFeatures&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st_future&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;    &lt;span class=&quot;cm&quot;&gt;/* module&amp;#39;s future features that affect&lt;/span&gt;
4328 &lt;span class=&quot;cm&quot;&gt;                                       the symbol table */&lt;/span&gt;
4329     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recursion_depth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;            &lt;span class=&quot;cm&quot;&gt;/* current recursion depth */&lt;/span&gt;
4330     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recursion_limit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;            &lt;span class=&quot;cm&quot;&gt;/* recursion limit */&lt;/span&gt;
4331 &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
4332 &lt;/pre&gt;&lt;/div&gt;
4333 
4334 &lt;p&gt;Some of the symbol table API is exposed via &lt;a href=&quot;https://docs.python.org/3/library/symtable.html&quot;&gt;the &lt;code&gt;symtable&lt;/code&gt; module&lt;/a&gt; in the standard library. You can provide an expression or a module an receive a &lt;code&gt;symtable.SymbolTable&lt;/code&gt; instance.&lt;/p&gt;
4335 &lt;p&gt;You can provide a string with a Python expression and the &lt;code&gt;compile_type&lt;/code&gt; of &lt;code&gt;&quot;eval&quot;&lt;/code&gt;, or a module, function or class, and the &lt;code&gt;compile_mode&lt;/code&gt; of &lt;code&gt;&quot;exec&quot;&lt;/code&gt; to get a symbol table.&lt;/p&gt;
4336 &lt;p&gt;Looping over the elements in the table we can see some of the public and private fields and their types:&lt;/p&gt;
4337 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;symtable&lt;/span&gt;
4338 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;symtable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;symtable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;b + 1&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;test.py&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compile_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;eval&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4339 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;symbol&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;vm&quot;&gt;__dict__&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;symbol&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_symbols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()]&lt;/span&gt;
4340 &lt;span class=&quot;go&quot;&gt;[{&amp;#39;_Symbol__name&amp;#39;: &amp;#39;b&amp;#39;, &amp;#39;_Symbol__flags&amp;#39;: 6160, &amp;#39;_Symbol__scope&amp;#39;: 3, &amp;#39;_Symbol__namespaces&amp;#39;: ()}]&lt;/span&gt;
4341 &lt;/pre&gt;&lt;/div&gt;
4342 
4343 &lt;p&gt;The C code behind this is all within &lt;code&gt;Python/symtable.c&lt;/code&gt; and the primary interface is the &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/symtable.c#L262&quot;&gt;&lt;code&gt;PySymtable_BuildObject()&lt;/code&gt;&lt;/a&gt; function.&lt;/p&gt;
4344 &lt;p&gt;Similar to the top-level AST function we covered earlier, the &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/symtable.c#L262&quot;&gt;&lt;code&gt;PySymtable_BuildObject()&lt;/code&gt;&lt;/a&gt; function switches between the &lt;code&gt;mod_ty&lt;/code&gt; possible types (Module, Expression, Interactive, Suite, FunctionType), and visits each of the statements inside them.&lt;/p&gt;
4345 &lt;p&gt;Remember, &lt;code&gt;mod_ty&lt;/code&gt; is an AST instance, so the will now recursively explore the nodes and branches of the tree and add entries to the symtable:&lt;/p&gt;
4346 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;symtable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
4347 &lt;span class=&quot;nf&quot;&gt;PySymtable_BuildObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mod_ty&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyFutureFeatures&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;future&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4348 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4349 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;symtable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;symtable_new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
4350 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;asdl_seq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4351     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4352     &lt;span class=&quot;n&quot;&gt;PyThreadState&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tstate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4353     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recursion_limit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_GetRecursionLimit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
4354 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4355     &lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st_top&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st_cur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4356     &lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4357     &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Module_kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4358         &lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4359         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asdl_seq_LEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4360 &lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;symtable_visit_stmt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4361 &lt;/span&gt;                        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stmt_ty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asdl_seq_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
4362                 &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4363         &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4364     &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Expression_kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4365         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4366     &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Interactive_kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4367         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4368     &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Suite_kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4369         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4370     &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;FunctionType_kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4371         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4372     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4373     &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4374 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4375 &lt;/pre&gt;&lt;/div&gt;
4376 
4377 &lt;p&gt;So for a module, &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/symtable.c#L262&quot;&gt;&lt;code&gt;PySymtable_BuildObject()&lt;/code&gt;&lt;/a&gt; will loop through each statement in the module and call &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/symtable.c#L1176&quot;&gt;&lt;code&gt;symtable_visit_stmt()&lt;/code&gt;&lt;/a&gt;. The &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/symtable.c#L1176&quot;&gt;&lt;code&gt;symtable_visit_stmt()&lt;/code&gt;&lt;/a&gt; is a huge &lt;code&gt;switch&lt;/code&gt; statement with a case for each statement type (defined in &lt;code&gt;Parser/Python.asdl&lt;/code&gt;).&lt;/p&gt;
4378 &lt;p&gt;For each statement type, there is specific logic to that statement type. For example, a function definition has particular logic for:&lt;/p&gt;
4379 &lt;ol&gt;
4380 &lt;li&gt;If the recursion depth is beyond the limit, raise a recursion depth error&lt;/li&gt;
4381 &lt;li&gt;The name of the function to be added as a local variable&lt;/li&gt;
4382 &lt;li&gt;The default values for sequential arguments to be resolved&lt;/li&gt;
4383 &lt;li&gt;The default values for keyword arguments to be resolved &lt;/li&gt;
4384 &lt;li&gt;Any annotations for the arguments or the return type are resolved&lt;/li&gt;
4385 &lt;li&gt;Any function decorators are resolved&lt;/li&gt;
4386 &lt;li&gt;The code block with the contents of the function is visited in &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/symtable.c#L973&quot;&gt;&lt;code&gt;symtable_enter_block()&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
4387 &lt;li&gt;The arguments are visited&lt;/li&gt;
4388 &lt;li&gt;The body of the function is visited&lt;/li&gt;
4389 &lt;/ol&gt;
4390 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
4391 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If you&amp;rsquo;ve ever wondered why Python&amp;rsquo;s default arguments are mutable, the reason is in this function. You can see they are a pointer to the variable in the symtable. No extra work is done to copy any values to an immutable type.&lt;/p&gt;
4392 &lt;/div&gt;
4393 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;
4394 &lt;span class=&quot;nf&quot;&gt;symtable_visit_stmt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;symtable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stmt_ty&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4395 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4396 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;recursion_depth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;recursion_limit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;                          &lt;span class=&quot;c1&quot;&gt;// 1.&lt;/span&gt;
4397 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;PyErr_SetString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyExc_RecursionError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4398                         &lt;span class=&quot;s&quot;&gt;&amp;quot;maximum recursion depth exceeded during compilation&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4399         &lt;span class=&quot;n&quot;&gt;VISIT_QUIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4400     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4401     &lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4402     &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;FunctionDef_kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4403 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;symtable_add_def&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FunctionDef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DEF_LOCAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;// 2.&lt;/span&gt;
4404 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;VISIT_QUIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4405 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FunctionDef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;defaults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;                                    &lt;span class=&quot;c1&quot;&gt;// 3.&lt;/span&gt;
4406 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;VISIT_SEQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FunctionDef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;defaults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4407 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FunctionDef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kw_defaults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;                                 &lt;span class=&quot;c1&quot;&gt;// 4.&lt;/span&gt;
4408 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;VISIT_SEQ_WITH_NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FunctionDef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kw_defaults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4409 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;symtable_visit_annotations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FunctionDef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;           &lt;span class=&quot;c1&quot;&gt;// 5.&lt;/span&gt;
4410 &lt;/span&gt;                                        &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FunctionDef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;returns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
4411             &lt;span class=&quot;n&quot;&gt;VISIT_QUIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4412 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FunctionDef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decorator_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;                                    &lt;span class=&quot;c1&quot;&gt;// 6.&lt;/span&gt;
4413 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;VISIT_SEQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FunctionDef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decorator_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4414 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;symtable_enter_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FunctionDef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;                    &lt;span class=&quot;c1&quot;&gt;// 7.&lt;/span&gt;
4415 &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;FunctionBlock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lineno&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4416                                   &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;col_offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
4417             &lt;span class=&quot;n&quot;&gt;VISIT_QUIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4418 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;n&quot;&gt;VISIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FunctionDef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                            &lt;span class=&quot;c1&quot;&gt;// 8.&lt;/span&gt;
4419 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;n&quot;&gt;VISIT_SEQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stmt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FunctionDef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                             &lt;span class=&quot;c1&quot;&gt;// 9.&lt;/span&gt;
4420 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;symtable_exit_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
4421             &lt;span class=&quot;n&quot;&gt;VISIT_QUIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4422         &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4423     &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;ClassDef_kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4424         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4425     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4426     &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Return_kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4427         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4428     &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Delete_kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4429         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4430     &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Assign_kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4431         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4432     &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;AnnAssign_kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4433         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4434 &lt;/pre&gt;&lt;/div&gt;
4435 
4436 &lt;p&gt;Once the resulting symtable has been created, it is sent back to be used for the compiler.&lt;/p&gt;
4437 &lt;h4 id=&quot;core-compilation-process&quot;&gt;Core Compilation Process&lt;/h4&gt;
4438 &lt;p&gt;Now that the &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L312&quot;&gt;&lt;code&gt;PyAST_CompileObject()&lt;/code&gt;&lt;/a&gt; has a compiler state, a symtable, and a module in the form of the AST, the actual compilation can begin.&lt;/p&gt;
4439 &lt;p&gt;The purpose of the core compiler is to:&lt;/p&gt;
4440 &lt;ul&gt;
4441 &lt;li&gt;Convert the state, symtable, and AST into a &lt;a href=&quot;https://en.wikipedia.org/wiki/Control-flow_graph&quot;&gt;Control-Flow-Graph (CFG)&lt;/a&gt;&lt;/li&gt;
4442 &lt;li&gt;Protect the execution stage from runtime exceptions by catching any logic and code errors and raising them here&lt;/li&gt;
4443 &lt;/ul&gt;
4444 &lt;p&gt;You can call the CPython compiler in Python code by calling the built-in function &lt;code&gt;compile()&lt;/code&gt;. It returns a &lt;code&gt;code object&lt;/code&gt; instance:&lt;/p&gt;
4445 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;b+1&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;test.py&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;eval&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4446 &lt;span class=&quot;go&quot;&gt;&amp;lt;code object &amp;lt;module&amp;gt; at 0x10f222780, file &amp;quot;test.py&amp;quot;, line 1&amp;gt;&lt;/span&gt;
4447 &lt;/pre&gt;&lt;/div&gt;
4448 
4449 &lt;p&gt;The same as with the &lt;code&gt;symtable()&lt;/code&gt; function, a simple expression should have a mode of &lt;code&gt;&#39;eval&#39;&lt;/code&gt; and a module, function, or class should have a mode of &lt;code&gt;&#39;exec&#39;&lt;/code&gt;.&lt;/p&gt;
4450 &lt;p&gt;The compiled code can be found in the &lt;code&gt;co_code&lt;/code&gt; property of the code object:&lt;/p&gt;
4451 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_code&lt;/span&gt;
4452 &lt;span class=&quot;go&quot;&gt;b&amp;#39;e\x00d\x00\x17\x00S\x00&amp;#39;&lt;/span&gt;
4453 &lt;/pre&gt;&lt;/div&gt;
4454 
4455 &lt;p&gt;There is also a &lt;code&gt;dis&lt;/code&gt; module in the standard library, which disassembles the bytecode instructions and can print them on the screen or give you a list of &lt;code&gt;Instruction&lt;/code&gt; instances.&lt;/p&gt;
4456 &lt;p&gt;If you import &lt;code&gt;dis&lt;/code&gt; and give the &lt;code&gt;dis()&lt;/code&gt; function the code object&amp;rsquo;s &lt;code&gt;co_code&lt;/code&gt; property it disassembles it and prints the instructions on the REPL:&lt;/p&gt;
4457 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dis&lt;/span&gt;
4458 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4459 &lt;span class=&quot;go&quot;&gt;          0 LOAD_NAME                0 (0)&lt;/span&gt;
4460 &lt;span class=&quot;go&quot;&gt;          2 LOAD_CONST               0 (0)&lt;/span&gt;
4461 &lt;span class=&quot;go&quot;&gt;          4 BINARY_ADD&lt;/span&gt;
4462 &lt;span class=&quot;go&quot;&gt;          6 RETURN_VALUE&lt;/span&gt;
4463 &lt;/pre&gt;&lt;/div&gt;
4464 
4465 &lt;p&gt;&lt;code&gt;LOAD_NAME&lt;/code&gt;, &lt;code&gt;LOAD_CONST&lt;/code&gt;, &lt;code&gt;BINARY_ADD&lt;/code&gt;, and &lt;code&gt;RETURN_VALUE&lt;/code&gt; are all bytecode instructions. They&amp;rsquo;re called bytecode because, in binary form, they were a byte long. However, since Python 3.6 the storage format was changed to a &lt;code&gt;word&lt;/code&gt;, so now they&amp;rsquo;re technically wordcode, not bytecode.&lt;/p&gt;
4466 &lt;p&gt;The &lt;a href=&quot;https://docs.python.org/3/library/dis.html#python-bytecode-instructions&quot;&gt;full list of bytecode instructions&lt;/a&gt; is available for each version of Python, and it does change between versions. For example, in Python 3.7, some new bytecode instructions were introduced to speed up execution of specific method calls.&lt;/p&gt;
4467 &lt;p&gt;In an earlier section, we explored the &lt;code&gt;instaviz&lt;/code&gt; package. This included a visualization of the code object type by running the compiler. It also displays the Bytecode operations inside the code objects.&lt;/p&gt;
4468 &lt;p&gt;Execute instaviz again to see the code object and bytecode for a function defined on the REPL:&lt;/p&gt;
4469 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;instaviz&lt;/span&gt;
4470 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
4471 &lt;span class=&quot;go&quot;&gt;       a = 1&lt;/span&gt;
4472 &lt;span class=&quot;go&quot;&gt;       b = a + 1&lt;/span&gt;
4473 &lt;span class=&quot;go&quot;&gt;       return b&lt;/span&gt;
4474 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instaviz&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4475 &lt;/pre&gt;&lt;/div&gt;
4476 
4477 &lt;p&gt;If we now jump into &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L1782&quot;&gt;&lt;code&gt;compiler_mod()&lt;/code&gt;&lt;/a&gt;, a function used to switch to different compiler functions depending on the module type. We&amp;rsquo;ll assume that &lt;code&gt;mod&lt;/code&gt; is a &lt;code&gt;Module&lt;/code&gt;. The module is compiled into the compiler state and then &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L5971&quot;&gt;&lt;code&gt;assemble()&lt;/code&gt;&lt;/a&gt; is run to create a &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Include/code.h#L69&quot;&gt;&lt;code&gt;PyCodeObject&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
4478 &lt;p&gt;The new code object is returned back to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L312&quot;&gt;&lt;code&gt;PyAST_CompileObject()&lt;/code&gt;&lt;/a&gt; and sent on for execution:&lt;/p&gt;
4479 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyCodeObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
4480 &lt;span class=&quot;nf&quot;&gt;compiler_mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compiler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mod_ty&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4481 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4482 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;PyCodeObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4483 &lt;/span&gt;    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addNone&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4484     &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4485     &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4486     &lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4487     &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Module_kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4488 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compiler_body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4489 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;compiler_exit_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4490             &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4491         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4492         &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4493     &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Interactive_kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4494         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4495     &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Expression_kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4496         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4497     &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;Suite_kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4498         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4499     &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4500 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assemble&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addNone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4501 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;compiler_exit_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4502 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4503 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4504 &lt;/pre&gt;&lt;/div&gt;
4505 
4506 &lt;p&gt;The &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L1743&quot;&gt;&lt;code&gt;compiler_body()&lt;/code&gt;&lt;/a&gt; function has some optimization flags and then loops over each statement in the module and visits it, similar to how the &lt;code&gt;symtable&lt;/code&gt; functions worked:&lt;/p&gt;
4507 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;
4508 &lt;span class=&quot;nf&quot;&gt;compiler_body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compiler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asdl_seq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stmts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4509 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4510     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4511     &lt;span class=&quot;n&quot;&gt;stmt_ty&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4512     &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;docstring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4513     &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4514 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asdl_seq_LEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stmts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4515 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;n&quot;&gt;VISIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stmt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stmt_ty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asdl_seq_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stmts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
4516 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4517 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4518 &lt;/pre&gt;&lt;/div&gt;
4519 
4520 &lt;p&gt;The statement type is determined through a call to the &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Include/asdl.h#L32&quot;&gt;&lt;code&gt;asdl_seq_GET()&lt;/code&gt;&lt;/a&gt; function, which looks at the AST node&amp;rsquo;s type.&lt;/p&gt;
4521 &lt;p&gt;Through some smart macros, &lt;code&gt;VISIT&lt;/code&gt; calls a function in &lt;code&gt;Python/compile.c&lt;/code&gt; for each statement type:&lt;/p&gt;
4522 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;#define VISIT(C, TYPE, V) {\&lt;/span&gt;
4523 &lt;span class=&quot;cp&quot;&gt;    if (!compiler_visit_ ## TYPE((C), (V))) \&lt;/span&gt;
4524 &lt;span class=&quot;cp&quot;&gt;        return 0; \&lt;/span&gt;
4525 &lt;span class=&quot;cp&quot;&gt;}&lt;/span&gt;
4526 &lt;/pre&gt;&lt;/div&gt;
4527 
4528 &lt;p&gt;For a &lt;code&gt;stmt&lt;/code&gt; (the category for a statement) the compiler will then drop into &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L3310&quot;&gt;&lt;code&gt;compiler_visit_stmt()&lt;/code&gt;&lt;/a&gt; and switch through all of the potential statement types found in &lt;code&gt;Parser/Python.asdl&lt;/code&gt;:&lt;/p&gt;
4529 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;
4530 &lt;span class=&quot;nf&quot;&gt;compiler_visit_stmt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compiler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stmt_ty&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4531 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4532     &lt;span class=&quot;n&quot;&gt;Py_ssize_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4533 
4534     &lt;span class=&quot;cm&quot;&gt;/* Always assign a lineno to the next instruction for a stmt. */&lt;/span&gt;
4535     &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u_lineno&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lineno&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4536     &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u_col_offset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;col_offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4537     &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u_lineno_set&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4538 
4539     &lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4540     &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;FunctionDef_kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4541         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compiler_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4542     &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;ClassDef_kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4543         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compiler_class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4544     &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4545     &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;For_kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4546         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compiler_for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4547     &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4548     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4549 
4550     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4551 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4552 &lt;/pre&gt;&lt;/div&gt;
4553 
4554 &lt;p&gt;As an example, let&amp;rsquo;s focus on the &lt;code&gt;For&lt;/code&gt; statement, in Python is the:&lt;/p&gt;
4555 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iterable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4556     &lt;span class=&quot;c1&quot;&gt;# block&lt;/span&gt;
4557 &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# optional if iterable is False&lt;/span&gt;
4558     &lt;span class=&quot;c1&quot;&gt;# block&lt;/span&gt;
4559 &lt;/pre&gt;&lt;/div&gt;
4560 
4561 &lt;p&gt;If the statement is a &lt;code&gt;For&lt;/code&gt; type, it calls &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L2651&quot;&gt;&lt;code&gt;compiler_for()&lt;/code&gt;&lt;/a&gt;. There is an equivalent &lt;code&gt;compiler_*()&lt;/code&gt; function for all of the statement and expression types. The more straightforward types create the bytecode instructions inline, some of the more complex statement types call other functions.&lt;/p&gt;
4562 &lt;p&gt;Many of the statements can have sub-statements. A &lt;code&gt;for&lt;/code&gt; loop has a body, but you can also have complex expressions in the assignment and the iterator.&lt;/p&gt;
4563 &lt;p&gt;The compiler&amp;rsquo;s &lt;code&gt;compiler_&lt;/code&gt; statements sends blocks to the compiler state. These blocks contain instructions, the instruction data structure in &lt;code&gt;Python/compile.c&lt;/code&gt; has the opcode, any arguments, and the target block (if this is a jump instruction), it also contains the line number.&lt;/p&gt;
4564 &lt;p&gt;For jump statements, they can either be absolute or relative jump statements. Jump statements are used to &amp;ldquo;jump&amp;rdquo; from one operation to another. Absolute jump statements specify the exact operation number in the compiled code object, whereas relative jump statements specify the jump target relative to another operation:&lt;/p&gt;
4565 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4566     &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;i_jabs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4567     &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;i_jrel&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4568     &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i_opcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4569     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i_oparg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4570     &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;basicblock_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* target block (if jump instruction) */&lt;/span&gt;
4571     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i_lineno&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4572 &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
4573 &lt;/pre&gt;&lt;/div&gt;
4574 
4575 &lt;p&gt;So a frame block (of type &lt;code&gt;basicblock&lt;/code&gt;), contains the following fields:&lt;/p&gt;
4576 &lt;ul&gt;
4577 &lt;li&gt;A &lt;code&gt;b_list&lt;/code&gt; pointer, the link to a list of blocks for the compiler state&lt;/li&gt;
4578 &lt;li&gt;A list of instructions &lt;code&gt;b_instr&lt;/code&gt;, with both the allocated list size &lt;code&gt;b_ialloc&lt;/code&gt;, and the number used &lt;code&gt;b_iused&lt;/code&gt;&lt;/li&gt;
4579 &lt;li&gt;The next block after this one &lt;code&gt;b_next&lt;/code&gt;&lt;/li&gt;
4580 &lt;li&gt;Whether the block has been &amp;ldquo;seen&amp;rdquo; by the assembler when traversing depth-first&lt;/li&gt;
4581 &lt;li&gt;If this block has a &lt;code&gt;RETURN_VALUE&lt;/code&gt; opcode (&lt;code&gt;b_return&lt;/code&gt;)&lt;/li&gt;
4582 &lt;li&gt;The depth of the stack when this block was entered (&lt;code&gt;b_startdepth&lt;/code&gt;)&lt;/li&gt;
4583 &lt;li&gt;The instruction offset for the assembler&lt;/li&gt;
4584 &lt;/ul&gt;
4585 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;basicblock_&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4586     &lt;span class=&quot;cm&quot;&gt;/* Each basicblock in a compilation unit is linked via b_list in the&lt;/span&gt;
4587 &lt;span class=&quot;cm&quot;&gt;       reverse order that the block are allocated.  b_list points to the next&lt;/span&gt;
4588 &lt;span class=&quot;cm&quot;&gt;       block, not to be confused with b_next, which is next by control flow. */&lt;/span&gt;
4589     &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;basicblock_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4590     &lt;span class=&quot;cm&quot;&gt;/* number of instructions used */&lt;/span&gt;
4591     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b_iused&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4592     &lt;span class=&quot;cm&quot;&gt;/* length of instruction array (b_instr) */&lt;/span&gt;
4593     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b_ialloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4594     &lt;span class=&quot;cm&quot;&gt;/* pointer to an array of instructions, initially NULL */&lt;/span&gt;
4595     &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b_instr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4596     &lt;span class=&quot;cm&quot;&gt;/* If b_next is non-NULL, it is a pointer to the next&lt;/span&gt;
4597 &lt;span class=&quot;cm&quot;&gt;       block reached by normal control flow. */&lt;/span&gt;
4598     &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;basicblock_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b_next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4599     &lt;span class=&quot;cm&quot;&gt;/* b_seen is used to perform a DFS of basicblocks. */&lt;/span&gt;
4600     &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;b_seen&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4601     &lt;span class=&quot;cm&quot;&gt;/* b_return is true if a RETURN_VALUE opcode is inserted. */&lt;/span&gt;
4602     &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;b_return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4603     &lt;span class=&quot;cm&quot;&gt;/* depth of stack upon entry of block, computed by stackdepth() */&lt;/span&gt;
4604     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b_startdepth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4605     &lt;span class=&quot;cm&quot;&gt;/* instruction offset for block, computed by assemble_jump_offsets() */&lt;/span&gt;
4606     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b_offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4607 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;basicblock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4608 &lt;/pre&gt;&lt;/div&gt;
4609 
4610 &lt;p&gt;The &lt;code&gt;For&lt;/code&gt; statement is somewhere in the middle in terms of complexity. There are 15 steps in the compilation of a &lt;code&gt;For&lt;/code&gt; statement with the &lt;code&gt;for &amp;lt;target&amp;gt; in &amp;lt;iterator&amp;gt;:&lt;/code&gt; syntax:&lt;/p&gt;
4611 &lt;ol&gt;
4612 &lt;li&gt;Create a new code block called &lt;code&gt;start&lt;/code&gt;, this allocates memory and creates a &lt;code&gt;basicblock&lt;/code&gt; pointer&lt;/li&gt;
4613 &lt;li&gt;Create a new code block called &lt;code&gt;cleanup&lt;/code&gt;&lt;/li&gt;
4614 &lt;li&gt;Create a new code block called &lt;code&gt;end&lt;/code&gt;&lt;/li&gt;
4615 &lt;li&gt;Push a frame block of type &lt;code&gt;FOR_LOOP&lt;/code&gt; to the stack with &lt;code&gt;start&lt;/code&gt; as the entry block and &lt;code&gt;end&lt;/code&gt; as the exit block&lt;/li&gt;
4616 &lt;li&gt;Visit the iterator expression, which adds any operations for the iterator&lt;/li&gt;
4617 &lt;li&gt;Add the &lt;code&gt;GET_ITER&lt;/code&gt; operation to the compiler state&lt;/li&gt;
4618 &lt;li&gt;Switch to the &lt;code&gt;start&lt;/code&gt; block&lt;/li&gt;
4619 &lt;li&gt;Call &lt;code&gt;ADDOP_JREL&lt;/code&gt; which calls &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L1413&quot;&gt;&lt;code&gt;compiler_addop_j()&lt;/code&gt;&lt;/a&gt; to add the &lt;code&gt;FOR_ITER&lt;/code&gt; operation with an argument of the &lt;code&gt;cleanup&lt;/code&gt; block&lt;/li&gt;
4620 &lt;li&gt;Visit the &lt;code&gt;target&lt;/code&gt; and add any special code, like tuple unpacking, to the &lt;code&gt;start&lt;/code&gt; block&lt;/li&gt;
4621 &lt;li&gt;Visit each statement in the body of the for loop&lt;/li&gt;
4622 &lt;li&gt;Call &lt;code&gt;ADDOP_JABS&lt;/code&gt; which calls &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L1413&quot;&gt;&lt;code&gt;compiler_addop_j()&lt;/code&gt;&lt;/a&gt; to add the &lt;code&gt;JUMP_ABSOLUTE&lt;/code&gt; operation which indicates after the body is executed, jumps back to the start of the loop&lt;/li&gt;
4623 &lt;li&gt;Move to the &lt;code&gt;cleanup&lt;/code&gt; block&lt;/li&gt;
4624 &lt;li&gt;Pop the &lt;code&gt;FOR_LOOP&lt;/code&gt; frame block off the stack&lt;/li&gt;
4625 &lt;li&gt;Visit the statements inside the &lt;code&gt;else&lt;/code&gt; section of the for loop&lt;/li&gt;
4626 &lt;li&gt;Use the &lt;code&gt;end&lt;/code&gt; block&lt;/li&gt;
4627 &lt;/ol&gt;
4628 &lt;p&gt;Referring back to the &lt;code&gt;basicblock&lt;/code&gt; structure. You can see how in the compilation of the for statement, the various blocks are created and pushed into the compiler&amp;rsquo;s frame block and stack:&lt;/p&gt;
4629 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;
4630 &lt;span class=&quot;nf&quot;&gt;compiler_for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compiler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stmt_ty&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4631 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4632     &lt;span class=&quot;n&quot;&gt;basicblock&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cleanup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4633 
4634 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compiler_new_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                       &lt;span class=&quot;c1&quot;&gt;// 1.&lt;/span&gt;
4635 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;cleanup&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compiler_new_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                     &lt;span class=&quot;c1&quot;&gt;// 2.&lt;/span&gt;
4636 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compiler_new_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                         &lt;span class=&quot;c1&quot;&gt;// 3.&lt;/span&gt;
4637 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cleanup&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4638         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4639 
4640 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compiler_push_fblock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FOR_LOOP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// 4.&lt;/span&gt;
4641 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4642 
4643 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;VISIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;For&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                       &lt;span class=&quot;c1&quot;&gt;// 5.&lt;/span&gt;
4644 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;ADDOP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GET_ITER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                                  &lt;span class=&quot;c1&quot;&gt;// 6.&lt;/span&gt;
4645 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;compiler_use_next_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                   &lt;span class=&quot;c1&quot;&gt;// 7.&lt;/span&gt;
4646 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;ADDOP_JREL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FOR_ITER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cleanup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                    &lt;span class=&quot;c1&quot;&gt;// 8.&lt;/span&gt;
4647 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;VISIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;For&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                     &lt;span class=&quot;c1&quot;&gt;// 9.&lt;/span&gt;
4648 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;VISIT_SEQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stmt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;For&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                   &lt;span class=&quot;c1&quot;&gt;// 10.&lt;/span&gt;
4649 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;ADDOP_JABS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;JUMP_ABSOLUTE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                 &lt;span class=&quot;c1&quot;&gt;// 11.&lt;/span&gt;
4650 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;compiler_use_next_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cleanup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                 &lt;span class=&quot;c1&quot;&gt;// 12.&lt;/span&gt;
4651 &lt;/span&gt;
4652 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;compiler_pop_fblock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FOR_LOOP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;// 13.&lt;/span&gt;
4653 &lt;/span&gt;
4654 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;VISIT_SEQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stmt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;For&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;orelse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                 &lt;span class=&quot;c1&quot;&gt;// 14.&lt;/span&gt;
4655 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;compiler_use_next_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                     &lt;span class=&quot;c1&quot;&gt;// 15.&lt;/span&gt;
4656 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4657 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4658 &lt;/pre&gt;&lt;/div&gt;
4659 
4660 &lt;p&gt;Depending on the type of operation, there are different arguments required. For example, we used &lt;code&gt;ADDOP_JABS&lt;/code&gt; and &lt;code&gt;ADDOP_JREL&lt;/code&gt; here, which refer to &amp;ldquo;&lt;strong&gt;ADD&lt;/strong&gt; &lt;strong&gt;O&lt;/strong&gt;peration with &lt;strong&gt;J&lt;/strong&gt;ump to a &lt;strong&gt;REL&lt;/strong&gt;ative position&amp;rdquo; and &amp;ldquo;&lt;strong&gt;ADD&lt;/strong&gt; &lt;strong&gt;O&lt;/strong&gt;peration with &lt;strong&gt;J&lt;/strong&gt;ump to an &lt;strong&gt;ABS&lt;/strong&gt;olute position&amp;rdquo;. This is referring to the &lt;code&gt;APPOP_JREL&lt;/code&gt; and &lt;code&gt;ADDOP_JABS&lt;/code&gt; macros which call &lt;code&gt;compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)&lt;/code&gt; and set the &lt;code&gt;absolute&lt;/code&gt; argument to 0 and 1 respectively.&lt;/p&gt;
4661 &lt;p&gt;There are some other macros, like &lt;code&gt;ADDOP_I&lt;/code&gt; calls &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L1383&quot;&gt;&lt;code&gt;compiler_addop_i()&lt;/code&gt;&lt;/a&gt; which add an operation with an integer argument, or &lt;code&gt;ADDOP_O&lt;/code&gt; calls &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L1345&quot;&gt;&lt;code&gt;compiler_addop_o()&lt;/code&gt;&lt;/a&gt; which adds an operation with a &lt;code&gt;PyObject&lt;/code&gt; argument. &lt;/p&gt;
4662 &lt;p&gt;Once these stages have completed, the compiler has a list of frame blocks, each containing a list of instructions and a pointer to the next block.&lt;/p&gt;
4663 &lt;h4 id=&quot;assembly&quot;&gt;Assembly&lt;/h4&gt;
4664 &lt;p&gt;With the compiler state, the assembler performs a &amp;ldquo;depth-first-search&amp;rdquo; of the blocks and merge the instructions into a single bytecode sequence. The assembler state is declared in &lt;code&gt;Python/compile.c&lt;/code&gt;:&lt;/p&gt;
4665 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assembler&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4666     &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_bytecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;cm&quot;&gt;/* string containing bytecode */&lt;/span&gt;
4667     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a_offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;              &lt;span class=&quot;cm&quot;&gt;/* offset into bytecode */&lt;/span&gt;
4668     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a_nblocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;             &lt;span class=&quot;cm&quot;&gt;/* number of reachable blocks */&lt;/span&gt;
4669     &lt;span class=&quot;n&quot;&gt;basicblock&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_postorder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* list of blocks in dfs postorder */&lt;/span&gt;
4670     &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_lnotab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;    &lt;span class=&quot;cm&quot;&gt;/* string containing lnotab */&lt;/span&gt;
4671     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a_lnotab_off&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;      &lt;span class=&quot;cm&quot;&gt;/* offset into lnotab */&lt;/span&gt;
4672     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a_lineno&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;              &lt;span class=&quot;cm&quot;&gt;/* last lineno of emitted instruction */&lt;/span&gt;
4673     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a_lineno_off&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;      &lt;span class=&quot;cm&quot;&gt;/* bytecode offset of last lineno */&lt;/span&gt;
4674 &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
4675 &lt;/pre&gt;&lt;/div&gt;
4676 
4677 &lt;p&gt;The &lt;code&gt;assemble()&lt;/code&gt; function has a few tasks:&lt;/p&gt;
4678 &lt;ul&gt;
4679 &lt;li&gt;Calculate the number of blocks for memory allocation&lt;/li&gt;
4680 &lt;li&gt;Ensure that every block that falls off the end returns &lt;code&gt;None&lt;/code&gt;, this is why every function returns &lt;code&gt;None&lt;/code&gt;, whether or not a &lt;code&gt;return&lt;/code&gt; statement exists&lt;/li&gt;
4681 &lt;li&gt;Resolve any jump statements offsets that were marked as relative&lt;/li&gt;
4682 &lt;li&gt;Call &lt;code&gt;dfs()&lt;/code&gt; to perform a depth-first-search of the blocks&lt;/li&gt;
4683 &lt;li&gt;Emit all the instructions to the compiler&lt;/li&gt;
4684 &lt;li&gt;Call &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L5854&quot;&gt;&lt;code&gt;makecode()&lt;/code&gt;&lt;/a&gt; with the compiler state to generate the &lt;code&gt;PyCodeObject&lt;/code&gt;&lt;/li&gt;
4685 &lt;/ul&gt;
4686 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyCodeObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
4687 &lt;span class=&quot;nf&quot;&gt;assemble&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compiler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addNone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4688 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4689     &lt;span class=&quot;n&quot;&gt;basicblock&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;entryblock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4690     &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assembler&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4691     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nblocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4692     &lt;span class=&quot;n&quot;&gt;PyCodeObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4693 
4694     &lt;span class=&quot;cm&quot;&gt;/* Make sure every block that falls off the end returns None.&lt;/span&gt;
4695 &lt;span class=&quot;cm&quot;&gt;       XXX NEXT_BLOCK() isn&amp;#39;t quite right, because if the last&lt;/span&gt;
4696 &lt;span class=&quot;cm&quot;&gt;       block ends with a jump or return b_next shouldn&amp;#39;t set.&lt;/span&gt;
4697 &lt;span class=&quot;cm&quot;&gt;     */&lt;/span&gt;
4698     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u_curblock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b_return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4699         &lt;span class=&quot;n&quot;&gt;NEXT_BLOCK&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4700 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addNone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4701 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;n&quot;&gt;ADDOP_LOAD_CONST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4702 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;n&quot;&gt;ADDOP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;RETURN_VALUE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4703 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4704     &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4705 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;dfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;entryblock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nblocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4706 &lt;/span&gt;
4707     &lt;span class=&quot;cm&quot;&gt;/* Can&amp;#39;t modify the bytecode after computing jump offsets. */&lt;/span&gt;
4708 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;assemble_jump_offsets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4709 &lt;/span&gt;
4710     &lt;span class=&quot;cm&quot;&gt;/* Emit code in reverse postorder from dfs. */&lt;/span&gt;
4711 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_nblocks&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4712 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_postorder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
4713 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b_iused&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4714 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assemble_emit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b_instr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
4715 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;                &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4716 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4717 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4718 
4719     &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;makecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4720 &lt;span class=&quot;hll&quot;&gt; &lt;span class=&quot;nl&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4721 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;assemble_free&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4722     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4723 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4724 &lt;/pre&gt;&lt;/div&gt;
4725 
4726 &lt;p&gt;The depth-first-search is performed by the &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L5397&quot;&gt;&lt;code&gt;dfs()&lt;/code&gt;&lt;/a&gt; function in &lt;code&gt;Python/compile.c&lt;/code&gt;, which follows the the &lt;code&gt;b_next&lt;/code&gt; pointers in each of the blocks, marks them as seen by toggling &lt;code&gt;b_seen&lt;/code&gt; and then adds them to the assemblers &lt;code&gt;**a_postorder&lt;/code&gt; list in reverse order.&lt;/p&gt;
4727 &lt;p&gt;The function loops back over the assembler&amp;rsquo;s post-order list and for each block, if it has a jump operation, recursively call &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L5397&quot;&gt;&lt;code&gt;dfs()&lt;/code&gt;&lt;/a&gt; for that jump:&lt;/p&gt;
4728 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;
4729 &lt;span class=&quot;nf&quot;&gt;dfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compiler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;basicblock&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assembler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4730 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4731     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4732 
4733     &lt;span class=&quot;cm&quot;&gt;/* Get rid of recursion for normal control flow.&lt;/span&gt;
4734 &lt;span class=&quot;cm&quot;&gt;       Since the number of blocks is limited, unused space in a_postorder&lt;/span&gt;
4735 &lt;span class=&quot;cm&quot;&gt;       (from a_nblocks to end) can be used as a stack for still not ordered&lt;/span&gt;
4736 &lt;span class=&quot;cm&quot;&gt;       blocks. */&lt;/span&gt;
4737     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b_seen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b_next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4738         &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b_seen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4739         &lt;span class=&quot;n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_nblocks&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4740         &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_postorder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4741     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4742     &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4743         &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_postorder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
4744         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b_iused&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4745             &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b_instr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
4746             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i_jrel&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i_jabs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4747                 &lt;span class=&quot;n&quot;&gt;dfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4748         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4749         &lt;span class=&quot;n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_nblocks&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4750         &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_postorder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_nblocks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4751     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4752 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4753 &lt;/pre&gt;&lt;/div&gt;
4754 
4755 &lt;h4 id=&quot;creating-a-code-object&quot;&gt;Creating a Code Object&lt;/h4&gt;
4756 &lt;p&gt;The task of &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L5854&quot;&gt;&lt;code&gt;makecode()&lt;/code&gt;&lt;/a&gt; is to go through the compiler state, some of the assembler&amp;rsquo;s properties and to put these into a &lt;code&gt;PyCodeObject&lt;/code&gt; by calling &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/codeobject.c#L246&quot;&gt;&lt;code&gt;PyCode_New()&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
4757 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/codeobject.9c054576627c.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/codeobject.9c054576627c.png&quot; width=&quot;201&quot; height=&quot;550&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/codeobject.9c054576627c.png&amp;amp;w=50&amp;amp;sig=9d1c4ff65adb0d6d578b775ca93a88843a3742c1 50w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/codeobject.9c054576627c.png&amp;amp;w=100&amp;amp;sig=076765eedb49d9e4e944629435b5a0fc10942c4c 100w, https://files.realpython.com/media/codeobject.9c054576627c.png 201w&quot; sizes=&quot;75vw&quot; alt=&quot;PyCodeObject structure&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
4758 &lt;p&gt;The variable names, constants are put as properties to the code object:&lt;/p&gt;
4759 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyCodeObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
4760 &lt;span class=&quot;nf&quot;&gt;makecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compiler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assembler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4761 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4762 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4763 
4764     &lt;span class=&quot;n&quot;&gt;consts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;consts_dict_keys_inorder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u_consts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4765     &lt;span class=&quot;n&quot;&gt;names&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dict_keys_inorder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u_names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4766     &lt;span class=&quot;n&quot;&gt;varnames&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dict_keys_inorder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u_varnames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4767 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4768     &lt;span class=&quot;n&quot;&gt;cellvars&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dict_keys_inorder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u_cellvars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4769 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4770     &lt;span class=&quot;n&quot;&gt;freevars&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dict_keys_inorder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u_freevars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyTuple_GET_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cellvars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
4771 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4772     &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compute_code_flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4773     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4774         &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4775 
4776     &lt;span class=&quot;n&quot;&gt;bytecode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyCode_Optimize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_bytecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;consts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_lnotab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4777 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4778     &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyCode_NewWithPosOnlyArgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;posonlyargcount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;posorkeywordargcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4779                                    &lt;span class=&quot;n&quot;&gt;posonlyargcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kwonlyargcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlocals_int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
4780                                    &lt;span class=&quot;n&quot;&gt;maxdepth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bytecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;consts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4781                                    &lt;span class=&quot;n&quot;&gt;varnames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;freevars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cellvars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4782                                    &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u_firstlineno&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_lnotab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4783 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4784     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4785 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4786 &lt;/pre&gt;&lt;/div&gt;
4787 
4788 &lt;p&gt;You may also notice that the bytecode is sent to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/peephole.c#L230&quot;&gt;&lt;code&gt;PyCode_Optimize()&lt;/code&gt;&lt;/a&gt; before it is sent to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/codeobject.c#L106&quot;&gt;&lt;code&gt;PyCode_NewWithPosOnlyArgs()&lt;/code&gt;&lt;/a&gt;. This function is part of the bytecode optimization process in &lt;code&gt;Python/peephole.c&lt;/code&gt;.&lt;/p&gt;
4789 &lt;p&gt;The peephole optimizer goes through the bytecode instructions and in certain scenarios, replace them with other instructions. For example, there is an optimizer called &amp;ldquo;constant unfolding&amp;rdquo;, so if you put the following statement into your script:&lt;/p&gt;
4790 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;
4791 &lt;/pre&gt;&lt;/div&gt;
4792 
4793 &lt;p&gt;It optimizes that to:&lt;/p&gt;
4794 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;
4795 &lt;/pre&gt;&lt;/div&gt;
4796 
4797 &lt;p&gt;Because 1 and 5 are constant values, so the result should always be the same.&lt;/p&gt;
4798 &lt;h4 id=&quot;conclusion_2&quot;&gt;Conclusion&lt;/h4&gt;
4799 &lt;p&gt;We can pull together all of these stages with the instaviz module:&lt;/p&gt;
4800 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;instaviz&lt;/span&gt;
4801 
4802 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
4803     &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;
4804     &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;
4805     &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
4806     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4807         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4808     &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
4809         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4810     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;
4811 
4812 
4813 &lt;span class=&quot;n&quot;&gt;instaviz&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4814 &lt;/pre&gt;&lt;/div&gt;
4815 
4816 &lt;p&gt;Will produce an AST graph:&lt;/p&gt;
4817 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-20_at_3.18.32_pm.4d9a0ea827ff.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-20_at_3.18.32_pm.4d9a0ea827ff.png&quot; width=&quot;2788&quot; height=&quot;1554&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-20_at_3.18.32_pm.4d9a0ea827ff.png&amp;amp;w=697&amp;amp;sig=9106a1bc23c5cc07f2ef159968c1ba2155a6562d 697w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-20_at_3.18.32_pm.4d9a0ea827ff.png&amp;amp;w=1394&amp;amp;sig=244bebb56c46d0b1eef97b8332afb6bfaa5e3648 1394w, https://files.realpython.com/media/Screen_Shot_2019-03-20_at_3.18.32_pm.4d9a0ea827ff.png 2788w&quot; sizes=&quot;75vw&quot; alt=&quot;Instaviz screenshot 6&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
4818 &lt;p&gt;With bytecode instructions in sequence:&lt;/p&gt;
4819 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-20_at_3.17.54_pm.6ea8ea532015.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-20_at_3.17.54_pm.6ea8ea532015.png&quot; width=&quot;2536&quot; height=&quot;1592&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-20_at_3.17.54_pm.6ea8ea532015.png&amp;amp;w=634&amp;amp;sig=f499889305c84679bdf07256f978975bbbc98c03 634w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-20_at_3.17.54_pm.6ea8ea532015.png&amp;amp;w=1268&amp;amp;sig=8d86595bce0f05c8dc45c3db3c0bff9b2d9cb0a9 1268w, https://files.realpython.com/media/Screen_Shot_2019-03-20_at_3.17.54_pm.6ea8ea532015.png 2536w&quot; sizes=&quot;75vw&quot; alt=&quot;Instaviz screenshot 7&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
4820 &lt;p&gt;Also, the code object with the variable names, constants, and binary &lt;code&gt;co_code&lt;/code&gt;:&lt;/p&gt;
4821 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-20_at_3.17.41_pm.231a0678f142.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-20_at_3.17.41_pm.231a0678f142.png&quot; width=&quot;2098&quot; height=&quot;940&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-20_at_3.17.41_pm.231a0678f142.png&amp;amp;w=524&amp;amp;sig=6daa3f3b9841eabbbf87d73886b81d346cdb33b3 524w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-20_at_3.17.41_pm.231a0678f142.png&amp;amp;w=1049&amp;amp;sig=7b074cc948021547f3da60e6bf0be3747585c6c8 1049w, https://files.realpython.com/media/Screen_Shot_2019-03-20_at_3.17.41_pm.231a0678f142.png 2098w&quot; sizes=&quot;75vw&quot; alt=&quot;Instaviz screenshot 8&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
4822 &lt;h3 id=&quot;execution&quot;&gt;Execution&lt;/h3&gt;
4823 &lt;p&gt;In &lt;code&gt;Python/pythonrun.c&lt;/code&gt; we broke out just before the call to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1094&quot;&gt;&lt;code&gt;run_eval_code_obj()&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
4824 &lt;p&gt;This call takes a code object, either fetched from the marshaled &lt;code&gt;.pyc&lt;/code&gt; file, or compiled through the AST and compiler stages.&lt;/p&gt;
4825 &lt;p&gt;&lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/pythonrun.c#L1094&quot;&gt;&lt;code&gt;run_eval_code_obj()&lt;/code&gt;&lt;/a&gt; will pass the globals, locals, &lt;code&gt;PyArena&lt;/code&gt;, and compiled &lt;code&gt;PyCodeObject&lt;/code&gt; to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ceval.c#L716&quot;&gt;&lt;code&gt;PyEval_EvalCode()&lt;/code&gt;&lt;/a&gt; in &lt;code&gt;Python/ceval.c&lt;/code&gt;.&lt;/p&gt;
4826 &lt;p&gt;This stage forms the execution component of CPython. Each of the bytecode operations is taken and executed using a &lt;a href=&quot;http://www.cs.uwm.edu/classes/cs315/Bacon/Lecture/HTML/ch10s07.html&quot;&gt;&amp;ldquo;Stack Frame&amp;rdquo; based system&lt;/a&gt;.&lt;/p&gt;
4827 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
4828 &lt;p&gt;&lt;strong&gt;What is a Stack Frame?&lt;/strong&gt;&lt;/p&gt;
4829 &lt;p&gt;Stack Frames are a data type used by many runtimes, not just Python, that allows functions to be called and variables to be returned between functions. Stack Frames also contain arguments, local variables, and other state information.&lt;/p&gt;
4830 &lt;p&gt;Typically, a Stack Frame exists for every function call, and they are stacked in sequence. You can see CPython&amp;rsquo;s frame stack anytime an exception is unhandled and the stack is printed on the screen.&lt;/p&gt;
4831 &lt;/div&gt;
4832 &lt;p&gt;&lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ceval.c#L716&quot;&gt;&lt;code&gt;PyEval_EvalCode()&lt;/code&gt;&lt;/a&gt; is the public API for evaluating a code object. The logic for evaluation is split between &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ceval.c#L4045&quot;&gt;&lt;code&gt;_PyEval_EvalCodeWithName()&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ceval.c#L745&quot;&gt;&lt;code&gt;_PyEval_EvalFrameDefault()&lt;/code&gt;&lt;/a&gt;, which are both in &lt;code&gt;ceval.c&lt;/code&gt;.&lt;/p&gt;
4833 &lt;p&gt;The public API &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ceval.c#L716&quot;&gt;&lt;code&gt;PyEval_EvalCode()&lt;/code&gt;&lt;/a&gt; will construct an execution frame from the top of the stack by calling &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ceval.c#L4045&quot;&gt;&lt;code&gt;_PyEval_EvalCodeWithName()&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
4834 &lt;p&gt;The construction of the first execution frame has many steps:&lt;/p&gt;
4835 &lt;ol&gt;
4836 &lt;li&gt;Keyword and positional arguments are resolved.&lt;/li&gt;
4837 &lt;li&gt;The use of &lt;code&gt;*args&lt;/code&gt; and &lt;code&gt;**kwargs&lt;/code&gt; in function definitions are resolved.&lt;/li&gt;
4838 &lt;li&gt;Arguments are added as local variables to the scope.&lt;/li&gt;
4839 &lt;li&gt;Co-routines and &lt;a href=&quot;https://realpython.com/introduction-to-python-generators/&quot;&gt;Generators&lt;/a&gt; are created, including the Asynchronous Generators.&lt;/li&gt;
4840 &lt;/ol&gt;
4841 &lt;p&gt;The frame object looks like this:&lt;/p&gt;
4842 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/PyFrameObject.8616eee0503e.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/PyFrameObject.8616eee0503e.png&quot; width=&quot;161&quot; height=&quot;408&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/PyFrameObject.8616eee0503e.png&amp;amp;w=40&amp;amp;sig=5c85bcc7939e61d207a19cf82d23cab3f73ec760 40w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/PyFrameObject.8616eee0503e.png&amp;amp;w=80&amp;amp;sig=25333bbe22791650facbac4288bb9f49065fb014 80w, https://files.realpython.com/media/PyFrameObject.8616eee0503e.png 161w&quot; sizes=&quot;75vw&quot; alt=&quot;PyFrameObject structure&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
4843 &lt;p&gt;Let&amp;rsquo;s step through those sequences.&lt;/p&gt;
4844 &lt;h4 id=&quot;1-constructing-thread-state&quot;&gt;1. Constructing Thread State&lt;/h4&gt;
4845 &lt;p&gt;Before a frame can be executed, it needs to be referenced from a thread. CPython can have many threads running at any one time within a single interpreter. An Interpreter state includes a list of those threads as a linked list. The thread structure is called &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Include/pystate.h#L23&quot;&gt;&lt;code&gt;PyThreadState&lt;/code&gt;&lt;/a&gt;, and there are many references throughout &lt;code&gt;ceval.c&lt;/code&gt;.&lt;/p&gt;
4846 &lt;p&gt;Here is the structure of the thread state object:&lt;/p&gt;
4847 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/PyThreadState.20467f3689b7.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/PyThreadState.20467f3689b7.png&quot; width=&quot;201&quot; height=&quot;208&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/PyThreadState.20467f3689b7.png&amp;amp;w=50&amp;amp;sig=90efd8d98ffa8ad9ed8b233c1e73fa469e4db4ac 50w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/PyThreadState.20467f3689b7.png&amp;amp;w=100&amp;amp;sig=e5d4a21dbc5cce9c1017a6d1805cf9eedf03ac9c 100w, https://files.realpython.com/media/PyThreadState.20467f3689b7.png 201w&quot; sizes=&quot;75vw&quot; alt=&quot;PyThreadState structure&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
4848 &lt;h4 id=&quot;2-constructing-frames&quot;&gt;2. Constructing Frames&lt;/h4&gt;
4849 &lt;p&gt;The input to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ceval.c#L716&quot;&gt;&lt;code&gt;PyEval_EvalCode()&lt;/code&gt;&lt;/a&gt; and therefore &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ceval.c#L4045&quot;&gt;&lt;code&gt;_PyEval_EvalCodeWithName()&lt;/code&gt;&lt;/a&gt; has arguments for:&lt;/p&gt;
4850 &lt;ul&gt;
4851 &lt;li&gt;&lt;strong&gt;&lt;code&gt;_co&lt;/code&gt;:&lt;/strong&gt; a &lt;code&gt;PyCodeObject&lt;/code&gt;&lt;/li&gt;
4852 &lt;li&gt;&lt;strong&gt;&lt;code&gt;globals&lt;/code&gt;:&lt;/strong&gt; a &lt;code&gt;PyDict&lt;/code&gt; with variable names as keys and their values&lt;/li&gt;
4853 &lt;li&gt;&lt;strong&gt;&lt;code&gt;locals&lt;/code&gt;:&lt;/strong&gt; a &lt;code&gt;PyDict&lt;/code&gt; with variable names as keys and their values&lt;/li&gt;
4854 &lt;/ul&gt;
4855 &lt;p&gt;The other arguments are optional, and not used for the basic API:&lt;/p&gt;
4856 &lt;ul&gt;
4857 &lt;li&gt;&lt;strong&gt;&lt;code&gt;args&lt;/code&gt;:&lt;/strong&gt; a &lt;code&gt;PyTuple&lt;/code&gt; with positional argument values in order, and &lt;code&gt;argcount&lt;/code&gt; for the number of values&lt;/li&gt;
4858 &lt;li&gt;&lt;strong&gt;&lt;code&gt;kwnames&lt;/code&gt;:&lt;/strong&gt; a list of keyword argument names&lt;/li&gt;
4859 &lt;li&gt;&lt;strong&gt;&lt;code&gt;kwargs&lt;/code&gt;:&lt;/strong&gt; a list of keyword argument values, and &lt;code&gt;kwcount&lt;/code&gt; for the number of them&lt;/li&gt;
4860 &lt;li&gt;&lt;strong&gt;&lt;code&gt;defs&lt;/code&gt;:&lt;/strong&gt; a list of default values for positional arguments, and &lt;code&gt;defcount&lt;/code&gt; for the length&lt;/li&gt;
4861 &lt;li&gt;&lt;strong&gt;&lt;code&gt;kwdefs&lt;/code&gt;:&lt;/strong&gt; a dictionary with the default values for keyword arguments&lt;/li&gt;
4862 &lt;li&gt;&lt;strong&gt;&lt;code&gt;closure&lt;/code&gt;:&lt;/strong&gt; a tuple with strings to merge into the code objects &lt;code&gt;co_freevars&lt;/code&gt; field&lt;/li&gt;
4863 &lt;li&gt;&lt;strong&gt;&lt;code&gt;name&lt;/code&gt;:&lt;/strong&gt; the name for this evaluation statement as a string&lt;/li&gt;
4864 &lt;li&gt;&lt;strong&gt;&lt;code&gt;qualname&lt;/code&gt;:&lt;/strong&gt; the qualified name for this evaluation statement as a string&lt;/li&gt;
4865 &lt;/ul&gt;
4866 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
4867 &lt;span class=&quot;nf&quot;&gt;_PyEval_EvalCodeWithName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;locals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4868            &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_ssize_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4869            &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwnames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4870            &lt;span class=&quot;n&quot;&gt;Py_ssize_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kwcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kwstep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4871            &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;defs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_ssize_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;defcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4872            &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwdefs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;closure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4873            &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;qualname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4874 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4875     &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4876 
4877     &lt;span class=&quot;n&quot;&gt;PyThreadState&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tstate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_PyThreadState_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
4878     &lt;span class=&quot;n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tstate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4879 
4880     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;globals&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4881         &lt;span class=&quot;n&quot;&gt;_PyErr_SetString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tstate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyExc_SystemError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
4882                          &lt;span class=&quot;s&quot;&gt;&amp;quot;PyEval_EvalCodeEx: NULL globals&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4883         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4884     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4885 
4886     &lt;span class=&quot;cm&quot;&gt;/* Create the frame */&lt;/span&gt;
4887     &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_PyFrame_New_NoTrack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tstate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;locals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4888     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4889         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4890     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4891     &lt;span class=&quot;n&quot;&gt;fastlocals&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_localsplus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4892     &lt;span class=&quot;n&quot;&gt;freevars&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_localsplus&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_nlocals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4893 &lt;/pre&gt;&lt;/div&gt;
4894 
4895 &lt;h4 id=&quot;3-converting-keyword-parameters-to-a-dictionary&quot;&gt;3. Converting Keyword Parameters to a Dictionary&lt;/h4&gt;
4896 &lt;p&gt;If the function definition contained a &lt;code&gt;**kwargs&lt;/code&gt; style catch-all for keyword arguments, then a new dictionary is created, and the values are copied across. The &lt;code&gt;kwargs&lt;/code&gt; name is then set as a variable, like in this example:&lt;/p&gt;
4897 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
4898     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;extra&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# this would resolve to a dictionary key&lt;/span&gt;
4899 &lt;/pre&gt;&lt;/div&gt;
4900 
4901 &lt;p&gt;The logic for creating a keyword argument dictionary is in the next part of &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ceval.c#L4045&quot;&gt;_PyEval_EvalCodeWithName()&lt;/a&gt;:&lt;/p&gt;
4902 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;    &lt;span class=&quot;cm&quot;&gt;/* Create a dictionary for keyword parameters (**kwargs) */&lt;/span&gt;
4903     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_flags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CO_VARKEYWORDS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4904         &lt;span class=&quot;n&quot;&gt;kwdict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyDict_New&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
4905         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwdict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4906             &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4907         &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4908         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_flags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CO_VARARGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4909             &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4910         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4911         &lt;span class=&quot;n&quot;&gt;SETLOCAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kwdict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4912     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4913     &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4914         &lt;span class=&quot;n&quot;&gt;kwdict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4915     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4916 &lt;/pre&gt;&lt;/div&gt;
4917 
4918 &lt;p&gt;The &lt;code&gt;kwdict&lt;/code&gt; variable will reference a &lt;code&gt;PyDictObject&lt;/code&gt; if any keyword arguments were found.&lt;/p&gt;
4919 &lt;h4 id=&quot;4-converting-positional-arguments-into-variables&quot;&gt;4. Converting Positional Arguments Into Variables&lt;/h4&gt;
4920 &lt;p&gt;Next, each of the positional arguments (if provided) are set as local variables:&lt;/p&gt;
4921 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;    &lt;span class=&quot;cm&quot;&gt;/* Copy all positional arguments into local variables */&lt;/span&gt;
4922     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argcount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_argcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4923         &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_argcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4924     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4925     &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4926         &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4927     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4928     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4929         &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
4930         &lt;span class=&quot;n&quot;&gt;Py_INCREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4931         &lt;span class=&quot;n&quot;&gt;SETLOCAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4932     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4933 &lt;/pre&gt;&lt;/div&gt;
4934 
4935 &lt;p&gt;At the end of the loop, you&amp;rsquo;ll see a call to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ceval.c#L1005&quot;&gt;&lt;code&gt;SETLOCAL()&lt;/code&gt;&lt;/a&gt; with the value, so if a positional argument is defined with a value, that is available within this scope: &lt;/p&gt;
4936 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arg1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
4937     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arg1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# both args are already local variables.&lt;/span&gt;
4938 &lt;/pre&gt;&lt;/div&gt;
4939 
4940 &lt;p&gt;Also, the reference counter for those variables is incremented, so the garbage collector won&amp;rsquo;t remove them until the frame has evaluated.&lt;/p&gt;
4941 &lt;h4 id=&quot;5-packing-positional-arguments-into-args&quot;&gt;5. Packing Positional Arguments Into &lt;code&gt;*args&lt;/code&gt;&lt;/h4&gt;
4942 &lt;p&gt;Similar to &lt;code&gt;**kwargs&lt;/code&gt;, a function argument prepended with a &lt;code&gt;*&lt;/code&gt; can be set to catch all remaining positional arguments. This argument is a tuple and the &lt;code&gt;*args&lt;/code&gt; name is set as a local variable: &lt;/p&gt;
4943 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;    &lt;span class=&quot;cm&quot;&gt;/* Pack other positional arguments into the *args argument */&lt;/span&gt;
4944     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_flags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CO_VARARGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4945         &lt;span class=&quot;n&quot;&gt;u&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_PyTuple_FromArray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argcount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4946         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4947             &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4948         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4949         &lt;span class=&quot;n&quot;&gt;SETLOCAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
4950     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4951 &lt;/pre&gt;&lt;/div&gt;
4952 
4953 &lt;h4 id=&quot;6-loading-keyword-arguments&quot;&gt;6. Loading Keyword Arguments&lt;/h4&gt;
4954 &lt;p&gt;If the function was called with keyword arguments and values, the &lt;code&gt;kwdict&lt;/code&gt; dictionary created in step 4 is now filled with any remaining keyword arguments passed by the caller that doesn&amp;rsquo;t resolve to named arguments or positional arguments.&lt;/p&gt;
4955 &lt;p&gt;For example, the &lt;code&gt;e&lt;/code&gt; argument was neither positional or named, so it is added to &lt;code&gt;**remaining&lt;/code&gt;:&lt;/p&gt;
4956 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remaining&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
4957 &lt;span class=&quot;go&quot;&gt;       print(a, b, c, d, remaining)&lt;/span&gt;
4958 
4959 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4960 &lt;span class=&quot;go&quot;&gt;(1, 2, 3, 4, {&amp;#39;e&amp;#39;: 5})&lt;/span&gt;
4961 &lt;/pre&gt;&lt;/div&gt;
4962 
4963 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
4964 &lt;p&gt;&lt;strong&gt;Positional-only arguments&lt;/strong&gt; is a new feature in Python 3.8. Introduced in &lt;a href=&quot;https://www.python.org/dev/peps/pep-0570/&quot;&gt;PEP570&lt;/a&gt;, positional-only arguments are a way of stopping users of your API from using positional arguments with a keyword syntax.&lt;/p&gt;
4965 &lt;p&gt;For example, this simple function converts Farenheit to Celcius. Note, the use of &lt;code&gt;/&lt;/code&gt; as a special argument seperates positional-only arguments from the other arguments.&lt;/p&gt;
4966 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;to_celcius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;farenheit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
4967     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;farenheit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;
4968 &lt;/pre&gt;&lt;/div&gt;
4969 
4970 &lt;p&gt;All arguments to the left of &lt;code&gt;/&lt;/code&gt; must be called only as a positional argument, and arguments to the right can be called as either positional or keyword arguments:&lt;/p&gt;
4971 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_celcius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;110&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4972 &lt;/pre&gt;&lt;/div&gt;
4973 
4974 &lt;p&gt;Calling the function using a keyword argument to a positional-only argument will raise a &lt;code&gt;TypeError&lt;/code&gt;:&lt;/p&gt;
4975 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_celcius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;farenheit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;110&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
4976 &lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
4977   File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
4978 &lt;span class=&quot;gr&quot;&gt;TypeError&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;to_celcius() got some positional-only arguments passed as keyword arguments: &amp;#39;farenheit&amp;#39;&lt;/span&gt;
4979 &lt;/pre&gt;&lt;/div&gt;
4980 
4981 &lt;/div&gt;
4982 &lt;p&gt;The resolution of the keyword argument dictionary values comes after the unpacking of all other arguments. The PEP570 positional-only arguments are shown by starting the keyword-argument loop at &lt;code&gt;co_posonlyargcount&lt;/code&gt;. If the &lt;code&gt;/&lt;/code&gt; symbol was used on the 3rd argument, the value of &lt;code&gt;co_posonlyargcount&lt;/code&gt; would be &lt;code&gt;2&lt;/code&gt;.
4983 &lt;code&gt;PyDict_SetItem()&lt;/code&gt; is called for each remaining argument to add it to the &lt;code&gt;locals&lt;/code&gt; dictionary, so when executing, each of the keyword arguments are scoped local variables:&lt;/p&gt;
4984 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kwcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kwstep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4985         &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_varnames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4986         &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keyword&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kwnames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
4987         &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
4988         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
4989 
4990         &lt;span class=&quot;cm&quot;&gt;/* Speed hack: do raw pointer compares. As names are&lt;/span&gt;
4991 &lt;span class=&quot;cm&quot;&gt;           normally interned this should almost always hit. */&lt;/span&gt;
4992         &lt;span class=&quot;n&quot;&gt;co_varnames&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyTupleObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_varnames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ob_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4993 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_posonlyargcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4994 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;co_varnames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
4995             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keyword&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
4996                 &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kw_found&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
4997             &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4998         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
4999 
5000         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwdict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5001 
5002             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_posonlyargcount&lt;/span&gt;
5003                 &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;positional_only_passed_as_keyword&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tstate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
5004                                                      &lt;span class=&quot;n&quot;&gt;kwcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kwnames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
5005             &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5006                 &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5007             &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5008 
5009             &lt;span class=&quot;n&quot;&gt;_PyErr_Format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tstate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyExc_TypeError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
5010                           &lt;span class=&quot;s&quot;&gt;&amp;quot;%U() got an unexpected keyword argument &amp;#39;%S&amp;#39;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
5011                           &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keyword&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5012             &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5013         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5014 
5015         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyDict_SetItem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwdict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keyword&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5016             &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5017         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5018         &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5019 
5020       &lt;span class=&quot;nl&quot;&gt;kw_found&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
5021         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5022         &lt;span class=&quot;n&quot;&gt;Py_INCREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5023         &lt;span class=&quot;n&quot;&gt;SETLOCAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5024     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5025     &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5026 &lt;/pre&gt;&lt;/div&gt;
5027 
5028 &lt;p&gt;At the end of the loop, you&amp;rsquo;ll see a call to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ceval.c#L1005&quot;&gt;&lt;code&gt;SETLOCAL()&lt;/code&gt;&lt;/a&gt; with the value. If a keyword argument is defined with a value, that is available within this scope:&lt;/p&gt;
5029 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arg1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example_kwarg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
5030     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;example_kwarg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# example_kwarg is already a local variable.&lt;/span&gt;
5031 &lt;/pre&gt;&lt;/div&gt;
5032 
5033 &lt;h4 id=&quot;7-adding-missing-positional-arguments&quot;&gt;7. Adding Missing Positional Arguments&lt;/h4&gt;
5034 &lt;p&gt;Any positional arguments provided to a function call that are not in the list of positional arguments are added to a &lt;code&gt;*args&lt;/code&gt; tuple if this tuple does not exist, a failure is raised:&lt;/p&gt;
5035 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;    &lt;span class=&quot;cm&quot;&gt;/* Add missing positional arguments (copy default values from defs) */&lt;/span&gt;
5036     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argcount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_argcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5037         &lt;span class=&quot;n&quot;&gt;Py_ssize_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_argcount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;defcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5038         &lt;span class=&quot;n&quot;&gt;Py_ssize_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;missing&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5039         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5040             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GETLOCAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5041                 &lt;span class=&quot;n&quot;&gt;missing&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5042             &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5043         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5044         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;missing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5045             &lt;span class=&quot;n&quot;&gt;missing_arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;missing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;defcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fastlocals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5046             &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5047         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5048         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5049             &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5050         &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
5051             &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5052         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;defcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5053             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GETLOCAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5054                 &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;defs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
5055                 &lt;span class=&quot;n&quot;&gt;Py_INCREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5056                 &lt;span class=&quot;n&quot;&gt;SETLOCAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5057             &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5058         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5059     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5060 &lt;/pre&gt;&lt;/div&gt;
5061 
5062 &lt;h4 id=&quot;8-adding-missing-keyword-arguments&quot;&gt;8. Adding Missing Keyword Arguments&lt;/h4&gt;
5063 &lt;p&gt;Any keyword arguments provided to a function call that are not in the list of named keyword arguments are added to a &lt;code&gt;**kwargs&lt;/code&gt; dictionary if this dictionary does not exist, a failure is raised:&lt;/p&gt;
5064 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;    &lt;span class=&quot;cm&quot;&gt;/* Add missing keyword arguments (copy default values from kwdefs) */&lt;/span&gt;
5065     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_kwonlyargcount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5066         &lt;span class=&quot;n&quot;&gt;Py_ssize_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;missing&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5067         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_argcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5068             &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5069             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GETLOCAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5070                 &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5071             &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyTuple_GET_ITEM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_varnames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5072             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwdefs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5073                 &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyDict_GetItemWithError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwdefs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5074                 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5075             &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5076             &lt;span class=&quot;n&quot;&gt;missing&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5077         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5078         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5079     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5080 &lt;/pre&gt;&lt;/div&gt;
5081 
5082 &lt;h4 id=&quot;9-collapsing-closures&quot;&gt;9. Collapsing Closures&lt;/h4&gt;
5083 &lt;p&gt;Any closure names are added to the code object&amp;rsquo;s list of free variable names:&lt;/p&gt;
5084 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;    &lt;span class=&quot;cm&quot;&gt;/* Copy closure variables to free variables */&lt;/span&gt;
5085     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyTuple_GET_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_freevars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5086         &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyTuple_GET_ITEM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;closure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5087         &lt;span class=&quot;n&quot;&gt;Py_INCREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5088         &lt;span class=&quot;n&quot;&gt;freevars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyTuple_GET_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_cellvars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5089     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5090 &lt;/pre&gt;&lt;/div&gt;
5091 
5092 &lt;h4 id=&quot;10-creating-generators-coroutines-and-asynchronous-generators&quot;&gt;10. Creating Generators, Coroutines, and Asynchronous Generators&lt;/h4&gt;
5093 &lt;p&gt;If the evaluated code object has a flag that it is a generator, coroutine or async generator, then a new frame is created using one of the unique methods in the Generator, Coroutine or Async libraries and the current frame is added as a property.&lt;/p&gt;
5094 &lt;p&gt;The new frame is then returned, and the original frame is not evaluated. The frame is only evaluated when the generator/coroutine/async method is called on to execute its target:&lt;/p&gt;
5095 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;    &lt;span class=&quot;cm&quot;&gt;/* Handle generator/coroutine/asynchronous generator */&lt;/span&gt;
5096     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_flags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CO_GENERATOR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CO_COROUTINE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CO_ASYNC_GENERATOR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5097         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5098 
5099         &lt;span class=&quot;cm&quot;&gt;/* Create a new generator that owns the ready to run frame&lt;/span&gt;
5100 &lt;span class=&quot;cm&quot;&gt;         * and return that as the value. */&lt;/span&gt;
5101         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_coro&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5102             &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyCoro_New&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qualname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5103         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_flags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CO_ASYNC_GENERATOR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5104             &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyAsyncGen_New&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qualname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5105         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5106             &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyGen_NewWithQualName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qualname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5107         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5108         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5109 
5110         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5111     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5112 &lt;/pre&gt;&lt;/div&gt;
5113 
5114 &lt;p&gt;Lastly, &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ceval.c#L738&quot;&gt;&lt;code&gt;PyEval_EvalFrameEx()&lt;/code&gt;&lt;/a&gt; is called with the new frame:&lt;/p&gt;
5115 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;retval&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyEval_EvalFrameEx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5116     &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5117 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5118 &lt;/pre&gt;&lt;/div&gt;
5119 
5120 &lt;h4 id=&quot;frame-execution&quot;&gt;Frame Execution&lt;/h4&gt;
5121 &lt;p&gt;As covered earlier in the compiler and AST chapters, the code object contains a binary encoding of the bytecode to be executed. It also contains a list of variables and a symbol table.&lt;/p&gt;
5122 &lt;p&gt;The local and global variables are determined at runtime based on how that function, module, or block was called. This information is added to the frame by the &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ceval.c#L4045&quot;&gt;&lt;code&gt;_PyEval_EvalCodeWithName()&lt;/code&gt;&lt;/a&gt; function. There are other usages of frames, like the coroutine decorator, which dynamically generates a frame with the target as a variable.&lt;/p&gt;
5123 &lt;p&gt;The public API, &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ceval.c#L738&quot;&gt;&lt;code&gt;PyEval_EvalFrameEx()&lt;/code&gt;&lt;/a&gt; calls the interpreter&amp;rsquo;s configured frame evaluation function in the &lt;code&gt;eval_frame&lt;/code&gt; property. Frame evaluation was &lt;a href=&quot;https://www.python.org/dev/peps/pep-0523/&quot;&gt;made pluggable in Python 3.7 with PEP 523&lt;/a&gt;.&lt;/p&gt;
5124 &lt;p&gt;&lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ceval.c#L745&quot;&gt;&lt;code&gt;_PyEval_EvalFrameDefault()&lt;/code&gt;&lt;/a&gt; is the default function, and it is unusual to use anything other than this. &lt;/p&gt;
5125 &lt;p&gt;Frames are executed in the main execution loop inside &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ceval.c#L745&quot;&gt;&lt;code&gt;_PyEval_EvalFrameDefault()&lt;/code&gt;&lt;/a&gt;. This function is central function that brings everything together and brings your code to life. It contains decades of optimization since even a single line of code can have a significant impact on performance for the whole of CPython.&lt;/p&gt;
5126 &lt;p&gt;Everything that gets executed in CPython goes through this function.&lt;/p&gt;
5127 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
5128 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Something you might notice when reading &lt;code&gt;ceval.c&lt;/code&gt;, is how many times C macros have been used. C Macros are a way of having DRY-compliant code without the overhead of making function calls. The compiler converts the macros into C code and then compile the generated code. &lt;/p&gt;
5129 &lt;p&gt;If you want to see the expanded code, you can run &lt;code&gt;gcc -E&lt;/code&gt; on Linux and macOS:&lt;/p&gt;
5130 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; gcc -E Python/ceval.c
5131 &lt;/pre&gt;&lt;/div&gt;
5132 
5133 &lt;p&gt;Alternatively, &lt;a href=&quot;https://realpython.com/python-development-visual-studio-code/&quot;&gt;Visual Studio Code&lt;/a&gt; can do inline macro expansion once you have installed the official C/C++ extension:&lt;/p&gt;
5134 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-25_at_3.33.40_pm.c240b1a46e99.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-25_at_3.33.40_pm.c240b1a46e99.png&quot; width=&quot;1866&quot; height=&quot;622&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-25_at_3.33.40_pm.c240b1a46e99.png&amp;amp;w=466&amp;amp;sig=28bc3a2f62e499a92fdac8fbd951c6ab5a18d361 466w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-25_at_3.33.40_pm.c240b1a46e99.png&amp;amp;w=933&amp;amp;sig=ec297187398d1a9b390bdadedd2c96855b2dfacc 933w, https://files.realpython.com/media/Screen_Shot_2019-03-25_at_3.33.40_pm.c240b1a46e99.png 1866w&quot; sizes=&quot;75vw&quot; alt=&quot;C Macro expansion with VScode&quot;/&gt;&lt;/a&gt;
5135 &lt;/p&gt;
5136 &lt;/div&gt;
5137 &lt;p&gt;We can step through frame execution in Python 3.7 and beyond by enabling the tracing attribute on the current thread.&lt;/p&gt;
5138 &lt;p&gt;This code example sets the global tracing function to a function called &lt;code&gt;trace()&lt;/code&gt; that gets the stack from the current frame, prints the disassembled opcodes to the screen, and some extra information for debugging:&lt;/p&gt;
5139 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
5140 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dis&lt;/span&gt;
5141 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;traceback&lt;/span&gt;
5142 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;io&lt;/span&gt;
5143 
5144 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
5145    &lt;span class=&quot;n&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_trace_opcodes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;
5146    &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;traceback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extract_stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5147    &lt;span class=&quot;n&quot;&gt;pad&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;   &amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;|&amp;quot;&lt;/span&gt;
5148    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;opcode&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
5149       &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StringIO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
5150          &lt;span class=&quot;n&quot;&gt;dis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;disco&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_lasti&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5151          &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getvalue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5152          &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{pad}{l}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
5153    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;call&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
5154       &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{pad}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Calling &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{frame.f_code}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5155    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;return&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
5156       &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{pad}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Returning &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{args}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5157    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;line&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
5158       &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{pad}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Changing line to &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{frame.f_lineno}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5159    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
5160       &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{pad}{frame}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{event}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{args}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;)&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5161    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{pad}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;----------------------------------&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5162    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;
5163 &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;settrace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5164 
5165 &lt;span class=&quot;c1&quot;&gt;# Run some code for a demo&lt;/span&gt;
5166 &lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;quot;-&amp;quot;.join([letter for letter in &amp;quot;hello&amp;quot;])&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5167 &lt;/pre&gt;&lt;/div&gt;
5168 
5169 &lt;p&gt;This prints the code within each stack and point to the next operation before it is executed. When a frame returns a value, the return statement is printed:&lt;/p&gt;
5170 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-25_at_1.21.07_pm.7b03e9032f62.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/Screen_Shot_2019-03-25_at_1.21.07_pm.7b03e9032f62.png&quot; width=&quot;2572&quot; height=&quot;1686&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-25_at_1.21.07_pm.7b03e9032f62.png&amp;amp;w=643&amp;amp;sig=6d6106abaf9fb5f3d7e45e09320c7575b3a0e77c 643w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screen_Shot_2019-03-25_at_1.21.07_pm.7b03e9032f62.png&amp;amp;w=1286&amp;amp;sig=c41bf26bd15641fded6a31125b0cb5b630496647 1286w, https://files.realpython.com/media/Screen_Shot_2019-03-25_at_1.21.07_pm.7b03e9032f62.png 2572w&quot; sizes=&quot;75vw&quot; alt=&quot;Evaluating frame with tracing&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
5171 &lt;p&gt;The full list of instructions is available on the &lt;a href=&quot;https://docs.python.org/3/library/dis.html#python-bytecode-instructions&quot;&gt;&lt;code&gt;dis&lt;/code&gt; module&lt;/a&gt; documentation. &lt;/p&gt;
5172 &lt;h4 id=&quot;the-value-stack&quot;&gt;The Value Stack&lt;/h4&gt;
5173 &lt;p&gt;Inside the core evaluation loop, a value stack is created. This stack is a list of pointers to sequential &lt;code&gt;PyObject&lt;/code&gt; instances.&lt;/p&gt;
5174 &lt;p&gt;One way to think of the value stack is like a wooden peg on which you can stack cylinders. You would only add or remove one item at a time. This is done using the &lt;code&gt;PUSH(a)&lt;/code&gt; macro, where &lt;code&gt;a&lt;/code&gt; is a pointer to a &lt;code&gt;PyObject&lt;/code&gt;.&lt;/p&gt;
5175 &lt;p&gt;For example, if you created a &lt;code&gt;PyLong&lt;/code&gt; with the value 10 and pushed it onto the value stack:&lt;/p&gt;
5176 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyLong_FromLong&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5177 &lt;span class=&quot;n&quot;&gt;PUSH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5178 &lt;/pre&gt;&lt;/div&gt;
5179 
5180 &lt;p&gt;This action would have the following effect:&lt;/p&gt;
5181 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/stacks_push.0c755d83b347.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid &quot; src=&quot;https://files.realpython.com/media/stacks_push.0c755d83b347.png&quot; width=&quot;822&quot; height=&quot;279&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/stacks_push.0c755d83b347.png&amp;amp;w=205&amp;amp;sig=d3ce00e768e8c7f89ef321ea039b159131b39d8a 205w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/stacks_push.0c755d83b347.png&amp;amp;w=411&amp;amp;sig=65cf700de41fd59781082f03c5d347fd891ac5a9 411w, https://files.realpython.com/media/stacks_push.0c755d83b347.png 822w&quot; sizes=&quot;75vw&quot; alt=&quot;PUSH()&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
5182 &lt;p&gt;In the next operation, to fetch that value, you would use the &lt;code&gt;POP()&lt;/code&gt; macro to take the top value from the stack:&lt;/p&gt;
5183 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;POP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// a is PyLongObject with a value of 10&lt;/span&gt;
5184 &lt;/pre&gt;&lt;/div&gt;
5185 
5186 &lt;p&gt;This action would return the top value and end up with an empty value stack:&lt;/p&gt;
5187 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/stacks_pop.85872baaa8c3.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid &quot; src=&quot;https://files.realpython.com/media/stacks_pop.85872baaa8c3.png&quot; width=&quot;778&quot; height=&quot;226&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/stacks_pop.85872baaa8c3.png&amp;amp;w=194&amp;amp;sig=887719d9b65d3ef671e2310aae15904e04acba28 194w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/stacks_pop.85872baaa8c3.png&amp;amp;w=389&amp;amp;sig=6950fcd6370022df61559d12b74c6b685a61e238 389w, https://files.realpython.com/media/stacks_pop.85872baaa8c3.png 778w&quot; sizes=&quot;75vw&quot; alt=&quot;POP()&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
5188 &lt;p&gt;If you were to add 2 values to the stack:&lt;/p&gt;
5189 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyLong_FromLong&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5190 &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyLong_FromLong&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5191 &lt;span class=&quot;n&quot;&gt;PUSH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5192 &lt;span class=&quot;n&quot;&gt;PUSH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5193 &lt;/pre&gt;&lt;/div&gt;
5194 
5195 &lt;p&gt;They would end up in the order in which they were added, so &lt;code&gt;a&lt;/code&gt; would be pushed to the second position in the stack:&lt;/p&gt;
5196 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/stacks_pushpush.e052996db029.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid &quot; src=&quot;https://files.realpython.com/media/stacks_pushpush.e052996db029.png&quot; width=&quot;783&quot; height=&quot;226&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/stacks_pushpush.e052996db029.png&amp;amp;w=195&amp;amp;sig=47cc1b6b30f314f1589eb9c8188efcd3eb0ef79e 195w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/stacks_pushpush.e052996db029.png&amp;amp;w=391&amp;amp;sig=a0813bcc81d314c3f2f0eb146634ba187d447634 391w, https://files.realpython.com/media/stacks_pushpush.e052996db029.png 783w&quot; sizes=&quot;75vw&quot; alt=&quot;PUSH();PUSH()&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
5197 &lt;p&gt;If you were to fetch the top value in the stack, you would get a pointer to &lt;code&gt;b&lt;/code&gt; because it is at the top:&lt;/p&gt;
5198 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/stacks_pop2.af5068718f92.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid &quot; src=&quot;https://files.realpython.com/media/stacks_pop2.af5068718f92.png&quot; width=&quot;789&quot; height=&quot;226&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/stacks_pop2.af5068718f92.png&amp;amp;w=197&amp;amp;sig=1856b32d5f95f58eb2ad60a549165f85bf2b4d7a 197w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/stacks_pop2.af5068718f92.png&amp;amp;w=394&amp;amp;sig=ce3ba993ee2fa8cb037bb84144c73f95450954ac 394w, https://files.realpython.com/media/stacks_pop2.af5068718f92.png 789w&quot; sizes=&quot;75vw&quot; alt=&quot;POP();&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
5199 &lt;p&gt;If you need to fetch the pointer to the top value in the stack without popping it, you can use the &lt;code&gt;PEEK(v)&lt;/code&gt; operation, where &lt;code&gt;v&lt;/code&gt; is the stack position:&lt;/p&gt;
5200 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PEEK&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5201 &lt;/pre&gt;&lt;/div&gt;
5202 
5203 &lt;p&gt;0 represents the top of the stack, 1 would be the second position:&lt;/p&gt;
5204 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/stacks_peek.b00bde86bc7b.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid &quot; src=&quot;https://files.realpython.com/media/stacks_peek.b00bde86bc7b.png&quot; width=&quot;802&quot; height=&quot;174&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/stacks_peek.b00bde86bc7b.png&amp;amp;w=200&amp;amp;sig=fac4fef886154a979f9abe7316e9468179c14c71 200w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/stacks_peek.b00bde86bc7b.png&amp;amp;w=401&amp;amp;sig=463aeb044f65a4c446a37c71cd86ae71564a2ec6 401w, https://files.realpython.com/media/stacks_peek.b00bde86bc7b.png 802w&quot; sizes=&quot;75vw&quot; alt=&quot;PEEK()&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
5205 &lt;p&gt;To clone the value at the top of the stack, the &lt;code&gt;DUP_TWO()&lt;/code&gt; macro can be used, or by using the &lt;code&gt;DUP_TWO&lt;/code&gt; opcode:&lt;/p&gt;
5206 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DUP_TOP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
5207 &lt;/pre&gt;&lt;/div&gt;
5208 
5209 &lt;p&gt;This action would copy the value at the top to form 2 pointers to the same object:&lt;/p&gt;
5210 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/stacks_duptop.9b2eafe67375.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid &quot; src=&quot;https://files.realpython.com/media/stacks_duptop.9b2eafe67375.png&quot; width=&quot;799&quot; height=&quot;177&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/stacks_duptop.9b2eafe67375.png&amp;amp;w=199&amp;amp;sig=3d40e1422f0aa5f3b50b8b174d02f0fe5df54364 199w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/stacks_duptop.9b2eafe67375.png&amp;amp;w=399&amp;amp;sig=11e2d5a69b3dd385c9201d7ab4eeb902f8fa20c9 399w, https://files.realpython.com/media/stacks_duptop.9b2eafe67375.png 799w&quot; sizes=&quot;75vw&quot; alt=&quot;DUP_TOP()&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
5211 &lt;p&gt;There is a rotation macro &lt;code&gt;ROT_TWO&lt;/code&gt; that swaps the first and second values:&lt;/p&gt;
5212 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/stacks_rottwo.2990d9b3ecc7.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid &quot; src=&quot;https://files.realpython.com/media/stacks_rottwo.2990d9b3ecc7.png&quot; width=&quot;803&quot; height=&quot;175&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/stacks_rottwo.2990d9b3ecc7.png&amp;amp;w=200&amp;amp;sig=e3e88549e9801f463358ad1e8b5635ba56b77049 200w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/stacks_rottwo.2990d9b3ecc7.png&amp;amp;w=401&amp;amp;sig=d2c11f545336f13fa57bc1588f57b7e7f8dc049f 401w, https://files.realpython.com/media/stacks_rottwo.2990d9b3ecc7.png 803w&quot; sizes=&quot;75vw&quot; alt=&quot;ROT_TWO()&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
5213 &lt;p&gt;Each of the opcodes have a predefined &amp;ldquo;stack effect,&amp;rdquo; calculated by the &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/compile.c#L878&quot;&gt;&lt;code&gt;stack_effect()&lt;/code&gt;&lt;/a&gt; function inside &lt;code&gt;Python/compile.c&lt;/code&gt;. This function returns the delta in the number of values inside the stack for each opcode.&lt;/p&gt;
5214 &lt;h4 id=&quot;example-adding-an-item-to-a-list&quot;&gt;Example: Adding an Item to a List&lt;/h4&gt;
5215 &lt;p&gt;In Python, when you create a list, the &lt;code&gt;.append()&lt;/code&gt; method is available on the list object:&lt;/p&gt;
5216 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
5217 &lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5218 &lt;/pre&gt;&lt;/div&gt;
5219 
5220 &lt;p&gt;Where &lt;code&gt;obj&lt;/code&gt; is an object, you want to append to the end of the list.&lt;/p&gt;
5221 &lt;p&gt;There are 2 operations involved in this operation. &lt;code&gt;LOAD_FAST&lt;/code&gt;, to load the object &lt;code&gt;obj&lt;/code&gt; to the top of the value stack from the list of &lt;code&gt;locals&lt;/code&gt; in the frame, and &lt;code&gt;LIST_APPEND&lt;/code&gt; to add the object.&lt;/p&gt;
5222 &lt;p&gt;First exploring &lt;code&gt;LOAD_FAST&lt;/code&gt;, there are 5 steps:&lt;/p&gt;
5223 &lt;ol&gt;
5224 &lt;li&gt;
5225 &lt;p&gt;The pointer to &lt;code&gt;obj&lt;/code&gt; is loaded from &lt;code&gt;GETLOCAL()&lt;/code&gt;, where the variable to load is the operation argument. The list of variable pointers is stored in &lt;code&gt;fastlocals&lt;/code&gt;, which is a copy of the PyFrame attribute &lt;code&gt;f_localsplus&lt;/code&gt;. The operation argument is a number, pointing to the index in the &lt;code&gt;fastlocals&lt;/code&gt; array pointer. This means that the loading of a local is simply a copy of the pointer instead of having to look up the variable name.&lt;/p&gt;
5226 &lt;/li&gt;
5227 &lt;li&gt;
5228 &lt;p&gt;If variable no longer exists, an unbound local variable error is raised.&lt;/p&gt;
5229 &lt;/li&gt;
5230 &lt;li&gt;
5231 &lt;p&gt;The reference counter for &lt;code&gt;value&lt;/code&gt; (in our case, &lt;code&gt;obj&lt;/code&gt;) is increased by 1.&lt;/p&gt;
5232 &lt;/li&gt;
5233 &lt;li&gt;
5234 &lt;p&gt;The pointer to &lt;code&gt;obj&lt;/code&gt; is pushed to the top of the value stack.&lt;/p&gt;
5235 &lt;/li&gt;
5236 &lt;li&gt;
5237 &lt;p&gt;The &lt;code&gt;FAST_DISPATCH&lt;/code&gt; macro is called, if tracing is enabled, the loop goes over again (with all the tracing), if tracing is not enabled, a &lt;code&gt;goto&lt;/code&gt; is called to &lt;code&gt;fast_next_opcode&lt;/code&gt;, which jumps back to the top of the loop for the next instruction.&lt;/p&gt;
5238 &lt;/li&gt;
5239 &lt;/ol&gt;
5240 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt; 
5241     &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TARGET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LOAD_FAST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5242         &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GETLOCAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;oparg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                 &lt;span class=&quot;c1&quot;&gt;// 1.&lt;/span&gt;
5243         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5244             &lt;span class=&quot;n&quot;&gt;format_exc_check_arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
5245                 &lt;span class=&quot;n&quot;&gt;PyExc_UnboundLocalError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
5246                 &lt;span class=&quot;n&quot;&gt;UNBOUNDLOCAL_ERROR_MSG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
5247                 &lt;span class=&quot;n&quot;&gt;PyTuple_GetItem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_varnames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;oparg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
5248             &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;                                    &lt;span class=&quot;c1&quot;&gt;// 2.&lt;/span&gt;
5249         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5250         &lt;span class=&quot;n&quot;&gt;Py_INCREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                                  &lt;span class=&quot;c1&quot;&gt;// 3.&lt;/span&gt;
5251         &lt;span class=&quot;n&quot;&gt;PUSH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                                       &lt;span class=&quot;c1&quot;&gt;// 4.&lt;/span&gt;
5252         &lt;span class=&quot;n&quot;&gt;FAST_DISPATCH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                                   &lt;span class=&quot;c1&quot;&gt;// 5.&lt;/span&gt;
5253     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5254  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5255 &lt;/pre&gt;&lt;/div&gt;
5256 
5257 &lt;p&gt;Now the pointer to &lt;code&gt;obj&lt;/code&gt; is at the top of the value stack. The next instruction &lt;code&gt;LIST_APPEND&lt;/code&gt; is run.&lt;/p&gt;
5258 &lt;p&gt;Many of the bytecode operations are referencing the base types, like PyUnicode, PyNumber. For example, &lt;code&gt;LIST_APPEND&lt;/code&gt; appends an object to the end of a list. To achieve this, it pops the pointer from the value stack and returns the pointer to the last object in the stack. The macro is a shortcut for: &lt;/p&gt;
5259 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*--&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stack_pointer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5260 &lt;/pre&gt;&lt;/div&gt;
5261 
5262 &lt;p&gt;Now the pointer to &lt;code&gt;obj&lt;/code&gt; is stored as &lt;code&gt;v&lt;/code&gt;. The list pointer is loaded from &lt;code&gt;PEEK(oparg)&lt;/code&gt;.&lt;/p&gt;
5263 &lt;p&gt;Then the C API for Python lists is called for &lt;code&gt;list&lt;/code&gt; and &lt;code&gt;v&lt;/code&gt;. The code for this is inside &lt;code&gt;Objects/listobject.c&lt;/code&gt;, which we go into in the next chapter.&lt;/p&gt;
5264 &lt;p&gt;A call to &lt;code&gt;PREDICT&lt;/code&gt; is made, which guesses that the next operation will be &lt;code&gt;JUMP_ABSOLUTE&lt;/code&gt;. The &lt;code&gt;PREDICT&lt;/code&gt; macro has compiler-generated &lt;code&gt;goto&lt;/code&gt; statements for each of the potential operations&amp;rsquo; &lt;code&gt;case&lt;/code&gt; statements. This means the CPU can jump to that instruction and not have to go through the loop again:&lt;/p&gt;
5265 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5266         &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TARGET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LIST_APPEND&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5267             &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;POP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
5268             &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PEEK&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;oparg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5269             &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5270 &lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyList_Append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5271 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;Py_DECREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5272             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5273                 &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5274             &lt;span class=&quot;n&quot;&gt;PREDICT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;JUMP_ABSOLUTE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5275             &lt;span class=&quot;n&quot;&gt;DISPATCH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
5276         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5277  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5278 &lt;/pre&gt;&lt;/div&gt;
5279 
5280 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
5281 &lt;p&gt;&lt;strong&gt;Opcode predictions:&lt;/strong&gt; 
5282 Some opcodes tend to come in pairs thus making it possible to predict the second code when the first is run.  For example, &lt;code&gt;COMPARE_OP&lt;/code&gt; is often followed by &lt;code&gt;POP_JUMP_IF_FALSE&lt;/code&gt; or &lt;code&gt;POP_JUMP_IF_TRUE&lt;/code&gt;.&lt;/p&gt;
5283 &lt;p&gt;&amp;ldquo;Verifying the prediction costs a single high-speed test of a register variable against a constant.  If the pairing was good, then the processor&amp;rsquo;s own internal branch predication has a high likelihood of success, resulting in a nearly zero-overhead transition to the next opcode.  A successful prediction saves a trip through the eval-loop including its unpredictable switch-case branch.  Combined with the processor&amp;rsquo;s internal branch prediction, a successful PREDICT has the effect of making the two opcodes run as if they were a single new opcode with the bodies combined.&amp;rdquo;&lt;/p&gt;
5284 &lt;p&gt;If collecting opcode statistics, you have two choices:&lt;/p&gt;
5285 &lt;ol&gt;
5286 &lt;li&gt;Keep the predictions turned-on and interpret the results as if some opcodes had been combined&lt;/li&gt;
5287 &lt;li&gt;Turn off predictions so that the opcode frequency counter updates for both opcodes&lt;/li&gt;
5288 &lt;/ol&gt;
5289 &lt;p&gt;Opcode prediction is disabled with threaded code since the latter allows the CPU to record separate branch prediction information for each opcode.&lt;/p&gt;
5290 &lt;/div&gt;
5291 &lt;p&gt;Some of the operations, such as &lt;code&gt;CALL_FUNCTION&lt;/code&gt;, &lt;code&gt;CALL_METHOD&lt;/code&gt;, have an operation argument referencing another compiled function. In these cases, another frame is pushed to the frame stack in the thread, and the evaluation loop is run for that function until the function completes. Each time a new frame is created and pushed onto the stack, the value of the frame&amp;rsquo;s &lt;code&gt;f_back&lt;/code&gt; is set to the current frame before the new one is created.&lt;/p&gt;
5292 &lt;p&gt;This nesting of frames is clear when you see a stack trace, take this example script:&lt;/p&gt;
5293 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;function2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
5294   &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;RuntimeError&lt;/span&gt;
5295 
5296 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;function1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
5297   &lt;span class=&quot;n&quot;&gt;function2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
5298 
5299 &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vm&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;__main__&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
5300   &lt;span class=&quot;n&quot;&gt;function1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
5301 &lt;/pre&gt;&lt;/div&gt;
5302 
5303 &lt;p&gt;Calling this on the command line will give you:&lt;/p&gt;
5304 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; ./python.exe example_stack.py
5305 
5306 &lt;span class=&quot;go&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
5307 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;example_stack.py&amp;quot;, line 8, in &amp;lt;module&amp;gt;&lt;/span&gt;
5308 &lt;span class=&quot;go&quot;&gt;    function1()&lt;/span&gt;
5309 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;example_stack.py&amp;quot;, line 5, in function1&lt;/span&gt;
5310 &lt;span class=&quot;go&quot;&gt;    function2()&lt;/span&gt;
5311 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;example_stack.py&amp;quot;, line 2, in function2&lt;/span&gt;
5312 &lt;span class=&quot;go&quot;&gt;    raise RuntimeError&lt;/span&gt;
5313 &lt;span class=&quot;go&quot;&gt;RuntimeError&lt;/span&gt;
5314 &lt;/pre&gt;&lt;/div&gt;
5315 
5316 &lt;p&gt;In &lt;code&gt;traceback.py&lt;/code&gt;, the &lt;code&gt;walk_stack()&lt;/code&gt; function used to print trace backs:&lt;/p&gt;
5317 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;walk_stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
5318     &lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;Walk a stack yielding the frame and line number for each frame.&lt;/span&gt;
5319 
5320 &lt;span class=&quot;sd&quot;&gt;    This will follow f.f_back from the given frame. If no frame is given, the&lt;/span&gt;
5321 &lt;span class=&quot;sd&quot;&gt;    current stack is used. Usually used with StackSummary.extract.&lt;/span&gt;
5322 &lt;span class=&quot;sd&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
5323     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
5324         &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_getframe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_back&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_back&lt;/span&gt;
5325     &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
5326         &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_lineno&lt;/span&gt;
5327         &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_back&lt;/span&gt;
5328 &lt;/pre&gt;&lt;/div&gt;
5329 
5330 &lt;p&gt;Here you can see that the current frame, fetched by calling &lt;code&gt;sys._getframe()&lt;/code&gt; and the parent&amp;rsquo;s parent is set as the frame, because you don&amp;rsquo;t want to see the call to &lt;code&gt;walk_stack()&lt;/code&gt; or &lt;code&gt;print_trace()&lt;/code&gt; in the trace back, so those function frames are skipped.&lt;/p&gt;
5331 &lt;p&gt;Then the &lt;code&gt;f_back&lt;/code&gt; pointer is followed to the top.&lt;/p&gt;
5332 &lt;p&gt;&lt;code&gt;sys._getframe()&lt;/code&gt; is the Python API to get the &lt;code&gt;frame&lt;/code&gt; attribute of the current thread.&lt;/p&gt;
5333 &lt;p&gt;Here is how that frame stack would look visually, with 3 frames each with its code object and a thread state pointing to the current frame:&lt;/p&gt;
5334 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Frame_Stack_Example.20728854763c.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/Frame_Stack_Example.20728854763c.png&quot; width=&quot;841&quot; height=&quot;1556&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Frame_Stack_Example.20728854763c.png&amp;amp;w=210&amp;amp;sig=bb2e7a72a4363554566003b8412f81c843a8edc2 210w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Frame_Stack_Example.20728854763c.png&amp;amp;w=420&amp;amp;sig=17eddfb3f22ccab1056545af3055470669c4ce3f 420w, https://files.realpython.com/media/Frame_Stack_Example.20728854763c.png 841w&quot; sizes=&quot;75vw&quot; alt=&quot;Example frame stack&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
5335 &lt;h3 id=&quot;conclusion_3&quot;&gt;Conclusion&lt;/h3&gt;
5336 &lt;p&gt;In this Part, you explored the most complex element of CPython: the compiler. The original author of Python, Guido van Rossum, made the statement that CPython&amp;rsquo;s compiler should be &amp;ldquo;dumb&amp;rdquo; so that people can understand it.&lt;/p&gt;
5337 &lt;p&gt;By breaking down the compilation process into small, logical steps, it is far easier to understand.&lt;/p&gt;
5338 &lt;p&gt;In the next chapter, we connect the compilation process with the basis of all Python code, the &lt;code&gt;object&lt;/code&gt;.&lt;/p&gt;
5339 &lt;h2 h1=&quot;h1&quot; id=&quot;part-4-objects-in-cpython&quot;&gt;Part 4: Objects in CPython&lt;/h2&gt;
5340 &lt;p&gt;CPython comes with a collection of basic types like strings, lists, tuples, dictionaries, and objects.&lt;/p&gt;
5341 &lt;p&gt;All of these types are built-in. You don&amp;rsquo;t need to import any libraries, even from the standard library. Also, the instantiation of these built-in types has some handy shortcuts.&lt;/p&gt;
5342 &lt;p&gt;For example, to create a new list, you can call:&lt;/p&gt;
5343 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lst&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
5344 &lt;/pre&gt;&lt;/div&gt;
5345 
5346 &lt;p&gt;Or, you can use square brackets:&lt;/p&gt;
5347 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lst&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
5348 &lt;/pre&gt;&lt;/div&gt;
5349 
5350 &lt;p&gt;Strings can be instantiated from a string-literal by using either double or single quotes. We explored the grammar definitions earlier that cause the compiler to interpret double quotes as a string literal. &lt;/p&gt;
5351 &lt;p&gt;All types in Python inherit from &lt;code&gt;object&lt;/code&gt;, a built-in base type. Even strings, tuples, and list inherit from &lt;code&gt;object&lt;/code&gt;. During the walk-through of the C code, you have read lots of references to &lt;code&gt;PyObject*&lt;/code&gt;, the C-API structure for an &lt;code&gt;object&lt;/code&gt;.&lt;/p&gt;
5352 &lt;p&gt;Because C is not object-oriented &lt;a href=&quot;https://realpython.com/python3-object-oriented-programming/&quot;&gt;like Python&lt;/a&gt;, objects in C don&amp;rsquo;t inherit from one another. &lt;code&gt;PyObject&lt;/code&gt; is the data structure for the beginning of the Python object&amp;rsquo;s memory.&lt;/p&gt;
5353 &lt;p&gt;Much of the base object API is declared in &lt;code&gt;Objects/object.c&lt;/code&gt;, like the function &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/object.c#L505&quot;&gt;&lt;code&gt;PyObject_Repr&lt;/code&gt;&lt;/a&gt;, which the built-in &lt;code&gt;repr()&lt;/code&gt; function. You will also find &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/object.c#L810&quot;&gt;&lt;code&gt;PyObject_Hash()&lt;/code&gt;&lt;/a&gt; and other APIs.&lt;/p&gt;
5354 &lt;p&gt;All of these functions can be overridden in a custom object by implementing &amp;ldquo;dunder&amp;rdquo; methods on a Python object:&lt;/p&gt;
5355 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; 
5356     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
5357         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;
5358         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
5359 
5360     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__repr__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
5361         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;lt;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{0}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; id=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{1}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5362 &lt;/pre&gt;&lt;/div&gt;
5363 
5364 &lt;p&gt;This code is implemented in &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/object.c#L505&quot;&gt;&lt;code&gt;PyObject_Repr()&lt;/code&gt;&lt;/a&gt;, inside &lt;code&gt;Objects/object.c&lt;/code&gt;. The type of the target object, &lt;code&gt;v&lt;/code&gt; will be inferred through a call to &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Include/object.h#L122&quot;&gt;&lt;code&gt;Py_TYPE()&lt;/code&gt;&lt;/a&gt; and if the &lt;code&gt;tp_repr&lt;/code&gt; field is set, then the function pointer is called.
5365 If the &lt;code&gt;tp_repr&lt;/code&gt; field is not set, i.e. the object doesn&amp;rsquo;t declare a custom &lt;code&gt;__repr__&lt;/code&gt; method, then the default behavior is run, which is to return &lt;code&gt;&quot;&amp;lt;%s object at %p&amp;gt;&quot;&lt;/code&gt; with the type name and the ID:&lt;/p&gt;
5366 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
5367 &lt;span class=&quot;nf&quot;&gt;PyObject_Repr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5368 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5369     &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5370     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyErr_CheckSignals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
5371         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5372 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5373     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5374         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyUnicode_FromString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&amp;lt;NULL&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5375 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Py_TYPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tp_repr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5376 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyUnicode_FromFormat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&amp;lt;%s object at %p&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
5377                                     &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ob_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tp_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5378 
5379 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5380 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5381 &lt;/pre&gt;&lt;/div&gt;
5382 
5383 &lt;p&gt;The ob_type field for a given &lt;code&gt;PyObject*&lt;/code&gt; will point to the data structure &lt;code&gt;PyTypeObject&lt;/code&gt;, defined in &lt;code&gt;Include/cpython/object.h&lt;/code&gt;.
5384 This data-structure lists all the built-in functions, as fields and the arguments they should receive.&lt;/p&gt;
5385 &lt;p&gt;Take &lt;code&gt;tp_repr&lt;/code&gt; as an example:&lt;/p&gt;
5386 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_typeobject&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5387     &lt;span class=&quot;n&quot;&gt;PyObject_VAR_HEAD&lt;/span&gt;
5388     &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tp_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* For printing, in format &amp;quot;&amp;lt;module&amp;gt;.&amp;lt;name&amp;gt;&amp;quot; */&lt;/span&gt;
5389     &lt;span class=&quot;n&quot;&gt;Py_ssize_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tp_basicsize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tp_itemsize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* For allocation */&lt;/span&gt;
5390 
5391     &lt;span class=&quot;cm&quot;&gt;/* Methods to implement standard operations */&lt;/span&gt;
5392 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5393 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;reprfunc&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tp_repr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5394 &lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
5395 
5396 &lt;p&gt;Where &lt;code&gt;reprfunc&lt;/code&gt; is a &lt;code&gt;typedef&lt;/code&gt; for &lt;code&gt;PyObject *(*reprfunc)(PyObject *);&lt;/code&gt;, a function that takes 1 pointer to &lt;code&gt;PyObject&lt;/code&gt; (&lt;code&gt;self&lt;/code&gt;).&lt;/p&gt;
5397 &lt;p&gt;Some of the dunder APIs are optional, because they only apply to certain types, like numbers:&lt;/p&gt;
5398 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;    &lt;span class=&quot;cm&quot;&gt;/* Method suites for standard classes */&lt;/span&gt;
5399 
5400     &lt;span class=&quot;n&quot;&gt;PyNumberMethods&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tp_as_number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5401     &lt;span class=&quot;n&quot;&gt;PySequenceMethods&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tp_as_sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5402     &lt;span class=&quot;n&quot;&gt;PyMappingMethods&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tp_as_mapping&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5403 &lt;/pre&gt;&lt;/div&gt;
5404 
5405 &lt;p&gt;A sequence, like a list would implement the following methods:&lt;/p&gt;
5406 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5407     &lt;span class=&quot;n&quot;&gt;lenfunc&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// len(v)&lt;/span&gt;
5408     &lt;span class=&quot;n&quot;&gt;binaryfunc&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sq_concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// v + x&lt;/span&gt;
5409     &lt;span class=&quot;n&quot;&gt;ssizeargfunc&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sq_repeat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// for x in v&lt;/span&gt;
5410     &lt;span class=&quot;n&quot;&gt;ssizeargfunc&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sq_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// v[x]&lt;/span&gt;
5411     &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;was_sq_slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// v[x:y:z]&lt;/span&gt;
5412     &lt;span class=&quot;n&quot;&gt;ssizeobjargproc&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sq_ass_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// v[x] = z&lt;/span&gt;
5413     &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;was_sq_ass_slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// v[x:y] = z&lt;/span&gt;
5414     &lt;span class=&quot;n&quot;&gt;objobjproc&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sq_contains&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// x in v&lt;/span&gt;
5415 
5416     &lt;span class=&quot;n&quot;&gt;binaryfunc&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sq_inplace_concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5417     &lt;span class=&quot;n&quot;&gt;ssizeargfunc&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sq_inplace_repeat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5418 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PySequenceMethods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5419 &lt;/pre&gt;&lt;/div&gt;
5420 
5421 &lt;p&gt;All of these built-in functions are called the &lt;a href=&quot;https://docs.python.org/3/reference/datamodel.html&quot;&gt;Python Data Model&lt;/a&gt;. One of the great resources for the Python Data Model is &lt;a href=&quot;https://www.oreilly.com/library/view/fluent-python/9781491946237/&quot;&gt;&amp;ldquo;Fluent Python&amp;rdquo; by Luciano Ramalho&lt;/a&gt;.&lt;/p&gt;
5422 &lt;h3 id=&quot;base-object-type&quot;&gt;Base Object Type&lt;/h3&gt;
5423 &lt;p&gt;In &lt;code&gt;Objects/object.c&lt;/code&gt;, the base implementation of &lt;code&gt;object&lt;/code&gt; type is written as pure C code. There are some concrete implementations of basic logic, like shallow comparisons.&lt;/p&gt;
5424 &lt;p&gt;Not all methods in a Python object are part of the Data Model, so that a Python object can contain attributes (either class or instance attributes) and methods.&lt;/p&gt;
5425 &lt;p&gt;A simple way to think of a Python object is consisting of 2 things:&lt;/p&gt;
5426 &lt;ol&gt;
5427 &lt;li&gt;The core data model, with pointers to compiled functions&lt;/li&gt;
5428 &lt;li&gt;A dictionary with any custom attributes and methods&lt;/li&gt;
5429 &lt;/ol&gt;
5430 &lt;p&gt;The core data model is defined in the &lt;code&gt;PyTypeObject&lt;/code&gt;, and the functions are defined in:&lt;/p&gt;
5431 &lt;ul&gt;
5432 &lt;li&gt;&lt;code&gt;Objects/object.c&lt;/code&gt; for the built-in methods&lt;/li&gt;
5433 &lt;li&gt;&lt;code&gt;Objects/boolobject.c&lt;/code&gt; for the &lt;code&gt;bool&lt;/code&gt; type&lt;/li&gt;
5434 &lt;li&gt;&lt;code&gt;Objects/bytearrayobject.c&lt;/code&gt; for the &lt;code&gt;byte[]&lt;/code&gt; type&lt;/li&gt;
5435 &lt;li&gt;&lt;code&gt;Objects/bytesobjects.c&lt;/code&gt; for the &lt;code&gt;bytes&lt;/code&gt; type&lt;/li&gt;
5436 &lt;li&gt;&lt;code&gt;Objects/cellobject.c&lt;/code&gt; for the &lt;code&gt;cell&lt;/code&gt; type&lt;/li&gt;
5437 &lt;li&gt;&lt;code&gt;Objects/classobject.c&lt;/code&gt; for the abstract &lt;code&gt;class&lt;/code&gt; type, used in meta-programming&lt;/li&gt;
5438 &lt;li&gt;&lt;code&gt;Objects/codeobject.c&lt;/code&gt; used for the built-in &lt;code&gt;code&lt;/code&gt; object type&lt;/li&gt;
5439 &lt;li&gt;&lt;code&gt;Objects/complexobject.c&lt;/code&gt; for a complex numeric type&lt;/li&gt;
5440 &lt;li&gt;&lt;code&gt;Objects/iterobject.c&lt;/code&gt; for an iterator&lt;/li&gt;
5441 &lt;li&gt;&lt;code&gt;Objects/listobject.c&lt;/code&gt; for the &lt;code&gt;list&lt;/code&gt; type&lt;/li&gt;
5442 &lt;li&gt;&lt;code&gt;Objects/longobject.c&lt;/code&gt; for the &lt;code&gt;long&lt;/code&gt; numeric type&lt;/li&gt;
5443 &lt;li&gt;&lt;code&gt;Objects/memoryobject.c&lt;/code&gt; for the base memory type&lt;/li&gt;
5444 &lt;li&gt;&lt;code&gt;Objects/methodobject.c&lt;/code&gt; for the class method type&lt;/li&gt;
5445 &lt;li&gt;&lt;code&gt;Objects/moduleobject.c&lt;/code&gt; for a module type&lt;/li&gt;
5446 &lt;li&gt;&lt;code&gt;Objects/namespaceobject.c&lt;/code&gt; for a namespace type&lt;/li&gt;
5447 &lt;li&gt;&lt;code&gt;Objects/odictobject.c&lt;/code&gt; for an ordered dictionary type&lt;/li&gt;
5448 &lt;li&gt;&lt;code&gt;Objects/rangeobject.c&lt;/code&gt; for a range generator&lt;/li&gt;
5449 &lt;li&gt;&lt;code&gt;Objects/setobject.c&lt;/code&gt; for a &lt;code&gt;set&lt;/code&gt; type&lt;/li&gt;
5450 &lt;li&gt;&lt;code&gt;Objects/sliceobject.c&lt;/code&gt; for a slice reference type&lt;/li&gt;
5451 &lt;li&gt;&lt;code&gt;Objects/structseq.c&lt;/code&gt; for a &lt;a href=&quot;https://docs.python.org/3/library/struct.html#struct.Struct&quot;&gt;&lt;code&gt;struct.Struct&lt;/code&gt;&lt;/a&gt; type&lt;/li&gt;
5452 &lt;li&gt;&lt;code&gt;Objects/tupleobject.c&lt;/code&gt; for a &lt;code&gt;tuple&lt;/code&gt; type&lt;/li&gt;
5453 &lt;li&gt;&lt;code&gt;Objects/typeobject.c&lt;/code&gt; for a &lt;code&gt;type&lt;/code&gt; type&lt;/li&gt;
5454 &lt;li&gt;&lt;code&gt;Objects/unicodeobject.c&lt;/code&gt; for a &lt;code&gt;str&lt;/code&gt; type&lt;/li&gt;
5455 &lt;li&gt;&lt;code&gt;Objects/weakrefobject.c&lt;/code&gt; for a &lt;a href=&quot;https://docs.python.org/3/library/weakref.html&quot;&gt;&lt;code&gt;weakref&lt;/code&gt; object&lt;/a&gt;&lt;/li&gt;
5456 &lt;/ul&gt;
5457 &lt;p&gt;We&amp;rsquo;re going to dive into 3 of these types: &lt;/p&gt;
5458 &lt;ol&gt;
5459 &lt;li&gt;Booleans &lt;/li&gt;
5460 &lt;li&gt;Integers&lt;/li&gt;
5461 &lt;li&gt;Generators&lt;/li&gt;
5462 &lt;/ol&gt;
5463 &lt;p&gt;Booleans and Integers have a lot in common, so we&amp;rsquo;ll cover those first.&lt;/p&gt;
5464 &lt;h3 id=&quot;the-bool-and-long-integer-type&quot;&gt;The Bool and Long Integer Type&lt;/h3&gt;
5465 &lt;p&gt;The &lt;code&gt;bool&lt;/code&gt; type is the most straightforward implementation of the built-in types. It inherits from &lt;code&gt;long&lt;/code&gt; and has the predefined constants, &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Include/boolobject.h#L22&quot;&gt;&lt;code&gt;Py_True&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Include/boolobject.h#L21&quot;&gt;&lt;code&gt;Py_False&lt;/code&gt;&lt;/a&gt;. These constants are immutable instances, created on the instantiation of the Python interpreter.&lt;/p&gt;
5466 &lt;p&gt;Inside &lt;code&gt;Objects/boolobject.c&lt;/code&gt;, you can see the helper function to create a &lt;code&gt;bool&lt;/code&gt; instance from a number:&lt;/p&gt;
5467 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;PyBool_FromLong&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5468 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5469     &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5470 
5471     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5472         &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5473     &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
5474         &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5475     &lt;span class=&quot;n&quot;&gt;Py_INCREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5476     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5477 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5478 &lt;/pre&gt;&lt;/div&gt;
5479 
5480 &lt;p&gt;This function uses the C evaluation of a numeric type to assign &lt;code&gt;Py_True&lt;/code&gt; or &lt;code&gt;Py_False&lt;/code&gt; to a result and increment the reference counters.&lt;/p&gt;
5481 &lt;p&gt;The numeric functions for &lt;code&gt;and&lt;/code&gt;, &lt;code&gt;xor&lt;/code&gt;, and &lt;code&gt;or&lt;/code&gt; are implemented, but addition, subtraction, and division are dereferenced from the base long type since it would make no sense to divide two boolean values.&lt;/p&gt;
5482 &lt;p&gt;The implementation of &lt;code&gt;and&lt;/code&gt; for a &lt;code&gt;bool&lt;/code&gt; value checks if &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are booleans, then check their references to &lt;code&gt;Py_True&lt;/code&gt;, otherwise, are cast as numbers, and the &lt;code&gt;and&lt;/code&gt; operation is run on the two numbers:&lt;/p&gt;
5483 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
5484 &lt;span class=&quot;nf&quot;&gt;bool_and&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5485 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5486     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyBool_Check&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyBool_Check&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
5487         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyLong_Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tp_as_number&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nb_and&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5488     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyBool_FromLong&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
5489 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5490 &lt;/pre&gt;&lt;/div&gt;
5491 
5492 &lt;p&gt;The &lt;code&gt;long&lt;/code&gt; type is a bit more complex, as the memory requirements are expansive. In the transition from Python 2 to 3, CPython dropped support for the &lt;code&gt;int&lt;/code&gt; type and instead used the &lt;code&gt;long&lt;/code&gt; type as the primary integer type. Python&amp;rsquo;s &lt;code&gt;long&lt;/code&gt; type is quite special in that it can store a variable-length number. The maximum length is set in the compiled binary.&lt;/p&gt;
5493 &lt;p&gt;The data structure of a Python &lt;code&gt;long&lt;/code&gt; consists of the &lt;code&gt;PyObject&lt;/code&gt; header and a list of digits. The list of digits, &lt;code&gt;ob_digit&lt;/code&gt; is initially set to have one digit, but it later expanded to a longer length when initialized:&lt;/p&gt;
5494 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_longobject&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5495     &lt;span class=&quot;n&quot;&gt;PyObject_VAR_HEAD&lt;/span&gt;
5496     &lt;span class=&quot;n&quot;&gt;digit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ob_digit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
5497 &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
5498 &lt;/pre&gt;&lt;/div&gt;
5499 
5500 &lt;p&gt;Memory is allocated to a new &lt;code&gt;long&lt;/code&gt; through &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/longobject.c#L262&quot;&gt;&lt;code&gt;_PyLong_New()&lt;/code&gt;&lt;/a&gt;. This function takes a fixed length and makes sure it is smaller than &lt;code&gt;MAX_LONG_DIGITS&lt;/code&gt;. Then it reallocates the memory for &lt;code&gt;ob_digit&lt;/code&gt; to match the length.&lt;/p&gt;
5501 &lt;p&gt;To convert a C &lt;code&gt;long&lt;/code&gt; type to a Python &lt;code&gt;long&lt;/code&gt; type, the &lt;code&gt;long&lt;/code&gt; is converted to a list of digits, the memory for the Python &lt;code&gt;long&lt;/code&gt; is assigned, and then each of the digits is set.
5502 Because &lt;code&gt;long&lt;/code&gt; is initialized with &lt;code&gt;ob_digit&lt;/code&gt; already being at a length of 1, if the number is less than 10, then the value is set without the memory being allocated:&lt;/p&gt;
5503 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
5504 &lt;span class=&quot;nf&quot;&gt;PyLong_FromLong&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ival&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5505 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5506     &lt;span class=&quot;n&quot;&gt;PyLongObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5507     &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;abs_ival&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5508     &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;cm&quot;&gt;/* unsigned so &amp;gt;&amp;gt; doesn&amp;#39;t propagate sign bit */&lt;/span&gt;
5509     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ndigits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5510     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sign&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5511 
5512     &lt;span class=&quot;n&quot;&gt;CHECK_SMALL_INT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ival&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5513 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5514     &lt;span class=&quot;cm&quot;&gt;/* Fast path for single-digit ints */&lt;/span&gt;
5515     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;abs_ival&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyLong_SHIFT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5516         &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_PyLong_New&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5517         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5518             &lt;span class=&quot;n&quot;&gt;Py_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sign&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5519             &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ob_digit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_SAFE_DOWNCAST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
5520                 &lt;span class=&quot;n&quot;&gt;abs_ival&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;digit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5521         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5522         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5523     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5524 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5525     &lt;span class=&quot;cm&quot;&gt;/* Larger numbers: loop to determine number of digits */&lt;/span&gt;
5526     &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;abs_ival&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5527     &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5528         &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ndigits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5529         &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyLong_SHIFT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5530     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5531     &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_PyLong_New&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ndigits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5532     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5533         &lt;span class=&quot;n&quot;&gt;digit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ob_digit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5534         &lt;span class=&quot;n&quot;&gt;Py_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ndigits&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sign&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5535         &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;abs_ival&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5536         &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5537             &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_SAFE_DOWNCAST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
5538                 &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyLong_MASK&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;digit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5539             &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyLong_SHIFT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5540         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5541     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5542     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5543 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5544 &lt;/pre&gt;&lt;/div&gt;
5545 
5546 &lt;p&gt;To convert a &lt;a href=&quot;https://en.wikipedia.org/wiki/Double-precision_floating-point_format&quot;&gt;double-point floating point&lt;/a&gt; to a Python &lt;code&gt;long&lt;/code&gt;, &lt;code&gt;PyLong_FromDouble()&lt;/code&gt; does the math for you:&lt;/p&gt;
5547 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
5548 &lt;span class=&quot;nf&quot;&gt;PyLong_FromDouble&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5549 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5550     &lt;span class=&quot;n&quot;&gt;PyLongObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5551     &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;frac&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5552     &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ndig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;neg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5553     &lt;span class=&quot;n&quot;&gt;neg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5554     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Py_IS_INFINITY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5555         &lt;span class=&quot;n&quot;&gt;PyErr_SetString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyExc_OverflowError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
5556                         &lt;span class=&quot;s&quot;&gt;&amp;quot;cannot convert float infinity to integer&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5557         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5558     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5559     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Py_IS_NAN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5560         &lt;span class=&quot;n&quot;&gt;PyErr_SetString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyExc_ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
5561                         &lt;span class=&quot;s&quot;&gt;&amp;quot;cannot convert float NaN to integer&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5562         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5563     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5564     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dval&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5565         &lt;span class=&quot;n&quot;&gt;neg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5566         &lt;span class=&quot;n&quot;&gt;dval&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5567     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5568     &lt;span class=&quot;n&quot;&gt;frac&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;frexp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* dval = frac*2**expo; 0.0 &amp;lt;= frac &amp;lt; 1.0 */&lt;/span&gt;
5569     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5570         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyLong_FromLong&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5571     &lt;span class=&quot;n&quot;&gt;ndig&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyLong_SHIFT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* Number of &amp;#39;digits&amp;#39; in result */&lt;/span&gt;
5572     &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_PyLong_New&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ndig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5573     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5574         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5575     &lt;span class=&quot;n&quot;&gt;frac&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ldexp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;frac&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyLong_SHIFT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5576     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ndig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5577         &lt;span class=&quot;n&quot;&gt;digit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;digit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;frac&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5578         &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ob_digit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5579         &lt;span class=&quot;n&quot;&gt;frac&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;frac&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5580         &lt;span class=&quot;n&quot;&gt;frac&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ldexp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;frac&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyLong_SHIFT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5581     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5582     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;neg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5583         &lt;span class=&quot;n&quot;&gt;Py_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Py_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
5584     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5585 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5586 &lt;/pre&gt;&lt;/div&gt;
5587 
5588 &lt;p&gt;The remainder of the implementation functions in &lt;code&gt;longobject.c&lt;/code&gt; have utilities, such as converting a Unicode string into a number with &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/longobject.c#L2672&quot;&gt;&lt;code&gt;PyLong_FromUnicodeObject()&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
5589 &lt;h3 id=&quot;a-review-of-the-generator-type&quot;&gt;A Review of the Generator Type&lt;/h3&gt;
5590 &lt;p&gt;&lt;a href=&quot;https://realpython.com/introduction-to-python-generators/&quot;&gt;Python Generators&lt;/a&gt; are functions which return a &lt;code&gt;yield&lt;/code&gt; statement and can be called continually to generate further values.&lt;/p&gt;
5591 &lt;p&gt;Commonly they are used as a more memory efficient way of looping through values in a large block of data, like a file, a database or over a network. &lt;/p&gt;
5592 &lt;p&gt;Generator objects are returned in place of a value when &lt;code&gt;yield&lt;/code&gt; is used instead of &lt;code&gt;return&lt;/code&gt;. The generator object is created from the &lt;code&gt;yield&lt;/code&gt; statement and returned to the caller.&lt;/p&gt;
5593 &lt;p&gt;Let&amp;rsquo;s create a simple generator with a list of 4 constant values:&lt;/p&gt;
5594 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
5595 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;lst&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
5596 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
5597 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;
5598 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;
5599 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
5600 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;
5601 &lt;span class=&quot;go&quot;&gt;&amp;lt;generator object example at 0x100bcc480&amp;gt;&lt;/span&gt;
5602 &lt;/pre&gt;&lt;/div&gt;
5603 
5604 &lt;p&gt;If you explore the contents of the generator object, you can see some of the fields starting with &lt;code&gt;gi_&lt;/code&gt;:&lt;/p&gt;
5605 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5606 &lt;span class=&quot;go&quot;&gt;[ ...&lt;/span&gt;
5607 &lt;span class=&quot;go&quot;&gt; &amp;#39;close&amp;#39;, &lt;/span&gt;
5608 &lt;span class=&quot;go&quot;&gt; &amp;#39;gi_code&amp;#39;, &lt;/span&gt;
5609 &lt;span class=&quot;go&quot;&gt; &amp;#39;gi_frame&amp;#39;, &lt;/span&gt;
5610 &lt;span class=&quot;go&quot;&gt; &amp;#39;gi_running&amp;#39;, &lt;/span&gt;
5611 &lt;span class=&quot;go&quot;&gt; &amp;#39;gi_yieldfrom&amp;#39;, &lt;/span&gt;
5612 &lt;span class=&quot;go&quot;&gt; &amp;#39;send&amp;#39;, &lt;/span&gt;
5613 &lt;span class=&quot;go&quot;&gt; &amp;#39;throw&amp;#39;]&lt;/span&gt;
5614 &lt;/pre&gt;&lt;/div&gt;
5615 
5616 &lt;p&gt;The &lt;code&gt;PyGenObject&lt;/code&gt; type is defined in &lt;code&gt;Include/genobject.h&lt;/code&gt; and there are 3 flavors:&lt;/p&gt;
5617 &lt;ol&gt;
5618 &lt;li&gt;Generator objects&lt;/li&gt;
5619 &lt;li&gt;Coroutine objects&lt;/li&gt;
5620 &lt;li&gt;Async generator objects&lt;/li&gt;
5621 &lt;/ol&gt;
5622 &lt;p&gt;All 3 share the same subset of fields used in generators, and have similar behaviors:&lt;/p&gt;
5623 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/generators.536b1404195a.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/generators.536b1404195a.png&quot; width=&quot;612&quot; height=&quot;264&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/generators.536b1404195a.png&amp;amp;w=153&amp;amp;sig=665718d32fbb9081b7983085eaeb6de7437ed6b6 153w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/generators.536b1404195a.png&amp;amp;w=306&amp;amp;sig=05fff378d8474cff821fbd585c9a57c5c5c51283 306w, https://files.realpython.com/media/generators.536b1404195a.png 612w&quot; sizes=&quot;75vw&quot; alt=&quot;Structure of generator types&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
5624 &lt;p&gt;Focusing first on generators, you can see the fields:&lt;/p&gt;
5625 &lt;ul&gt;
5626 &lt;li&gt;&lt;code&gt;gi_frame&lt;/code&gt; linking to a &lt;code&gt;PyFrameObject&lt;/code&gt; for the generator, earlier in the execution chapter, we explored the use of locals and globals inside a frame&amp;rsquo;s value stack. This is how generators remember the last value of local variables since the frame is persistent between calls&lt;/li&gt;
5627 &lt;li&gt;&lt;code&gt;gi_running&lt;/code&gt; set to 0 or 1 if the generator is currently running&lt;/li&gt;
5628 &lt;li&gt;&lt;code&gt;gi_code&lt;/code&gt; linking to a &lt;code&gt;PyCodeObject&lt;/code&gt; with the compiled function that yielded the generator so that it can be called again&lt;/li&gt;
5629 &lt;li&gt;&lt;code&gt;gi_weakreflist&lt;/code&gt; linking to a list of weak references to objects inside the generator function&lt;/li&gt;
5630 &lt;li&gt;&lt;code&gt;gi_name&lt;/code&gt; as the name of the generator&lt;/li&gt;
5631 &lt;li&gt;&lt;code&gt;gi_qualname&lt;/code&gt; as the qualified name of the generator&lt;/li&gt;
5632 &lt;li&gt;&lt;code&gt;gi_exc_state&lt;/code&gt; as a tuple of exception data if the generator call raises an exception&lt;/li&gt;
5633 &lt;/ul&gt;
5634 &lt;p&gt;The coroutine and &lt;a href=&quot;https://realpython.com/async-io-python/#other-features-async-for-and-async-generators-comprehensions&quot;&gt;async generators&lt;/a&gt; have the same fields but prepended with &lt;code&gt;cr&lt;/code&gt; and &lt;code&gt;ag&lt;/code&gt; respectively.&lt;/p&gt;
5635 &lt;p&gt;If you call &lt;code&gt;__next__()&lt;/code&gt; on the generator object, the next value is yielded until eventually a &lt;code&gt;StopIteration&lt;/code&gt; is raised:&lt;/p&gt;
5636 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__next__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
5637 &lt;span class=&quot;go&quot;&gt;1&lt;/span&gt;
5638 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__next__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
5639 &lt;span class=&quot;go&quot;&gt;2&lt;/span&gt;
5640 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__next__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
5641 &lt;span class=&quot;go&quot;&gt;3&lt;/span&gt;
5642 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__next__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
5643 &lt;span class=&quot;go&quot;&gt;4&lt;/span&gt;
5644 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__next__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
5645 &lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
5646   File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
5647 &lt;span class=&quot;gr&quot;&gt;StopIteration&lt;/span&gt;
5648 &lt;/pre&gt;&lt;/div&gt;
5649 
5650 &lt;p&gt;Each time &lt;code&gt;__next__()&lt;/code&gt; is called, the code object inside the generators &lt;code&gt;gi_code&lt;/code&gt; field is executed as a new frame and the return value is pushed to the value stack.&lt;/p&gt;
5651 &lt;p&gt;You can also see that &lt;code&gt;gi_code&lt;/code&gt; is the compiled code object for the generator function by importing the &lt;code&gt;dis&lt;/code&gt; module and disassembling the bytecode inside:&lt;/p&gt;
5652 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
5653 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dis&lt;/span&gt;
5654 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;disco&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5655 &lt;span class=&quot;go&quot;&gt;  2           0 LOAD_CONST               1 (1)&lt;/span&gt;
5656 &lt;span class=&quot;go&quot;&gt;              2 LOAD_CONST               2 (2)&lt;/span&gt;
5657 &lt;span class=&quot;go&quot;&gt;              4 LOAD_CONST               3 (3)&lt;/span&gt;
5658 &lt;span class=&quot;go&quot;&gt;              6 LOAD_CONST               4 (4)&lt;/span&gt;
5659 &lt;span class=&quot;go&quot;&gt;              8 BUILD_LIST               4&lt;/span&gt;
5660 &lt;span class=&quot;go&quot;&gt;             10 STORE_FAST               0 (l)&lt;/span&gt;
5661 
5662 &lt;span class=&quot;go&quot;&gt;  3          12 SETUP_LOOP              18 (to 32)&lt;/span&gt;
5663 &lt;span class=&quot;go&quot;&gt;             14 LOAD_FAST                0 (l)&lt;/span&gt;
5664 &lt;span class=&quot;go&quot;&gt;             16 GET_ITER&lt;/span&gt;
5665 &lt;span class=&quot;go&quot;&gt;        &amp;gt;&amp;gt;   18 FOR_ITER                10 (to 30)&lt;/span&gt;
5666 &lt;span class=&quot;go&quot;&gt;             20 STORE_FAST               1 (i)&lt;/span&gt;
5667 
5668 &lt;span class=&quot;go&quot;&gt;  4          22 LOAD_FAST                1 (i)&lt;/span&gt;
5669 &lt;span class=&quot;go&quot;&gt;             24 YIELD_VALUE&lt;/span&gt;
5670 &lt;span class=&quot;go&quot;&gt;             26 POP_TOP&lt;/span&gt;
5671 &lt;span class=&quot;go&quot;&gt;             28 JUMP_ABSOLUTE           18&lt;/span&gt;
5672 &lt;span class=&quot;go&quot;&gt;        &amp;gt;&amp;gt;   30 POP_BLOCK&lt;/span&gt;
5673 &lt;span class=&quot;go&quot;&gt;        &amp;gt;&amp;gt;   32 LOAD_CONST               0 (None)&lt;/span&gt;
5674 &lt;span class=&quot;go&quot;&gt;             34 RETURN_VALUE&lt;/span&gt;
5675 &lt;/pre&gt;&lt;/div&gt;
5676 
5677 &lt;p&gt;Whenever &lt;code&gt;__next__()&lt;/code&gt; is called on a generator object, &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/genobject.c#L541&quot;&gt;&lt;code&gt;gen_iternext()&lt;/code&gt;&lt;/a&gt; is called with the generator instance, which immediately calls &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/genobject.c#L153&quot;&gt;&lt;code&gt;gen_send_ex()&lt;/code&gt;&lt;/a&gt; inside &lt;code&gt;Objects/genobject.c&lt;/code&gt;.&lt;/p&gt;
5678 &lt;p&gt;&lt;code&gt;gen_send_ex()&lt;/code&gt; is the function that converts a generator object into the next yielded result. You&amp;rsquo;ll see many similarities with the way frames are constructed in &lt;code&gt;Python/ceval.c&lt;/code&gt; from a code object as these functions have similar tasks.&lt;/p&gt;
5679 &lt;p&gt;The &lt;code&gt;gen_send_ex()&lt;/code&gt; function is shared with generators, coroutines, and async generators and has the following steps:&lt;/p&gt;
5680 &lt;ol&gt;
5681 &lt;li&gt;
5682 &lt;p&gt;The current thread state is fetched&lt;/p&gt;
5683 &lt;/li&gt;
5684 &lt;li&gt;
5685 &lt;p&gt;The frame object from the generator object is fetched&lt;/p&gt;
5686 &lt;/li&gt;
5687 &lt;li&gt;
5688 &lt;p&gt;If the generator is running when &lt;code&gt;__next__()&lt;/code&gt; was called, raise a &lt;code&gt;ValueError&lt;/code&gt;&lt;/p&gt;
5689 &lt;/li&gt;
5690 &lt;li&gt;
5691 &lt;p&gt;If the frame inside the generator is at the top of the stack:&lt;/p&gt;
5692 &lt;ul&gt;
5693 &lt;li&gt;In the case of a coroutine, if the coroutine is not already marked as closing, a &lt;code&gt;RuntimeError&lt;/code&gt; is raised&lt;/li&gt;
5694 &lt;li&gt;If this is an async generator, raise a &lt;code&gt;StopAsyncIteration&lt;/code&gt;&lt;/li&gt;
5695 &lt;li&gt;For a standard generator, a &lt;code&gt;StopIteration&lt;/code&gt; is raised.&lt;/li&gt;
5696 &lt;/ul&gt;
5697 &lt;/li&gt;
5698 &lt;li&gt;
5699 &lt;p&gt;If the last instruction in the frame (&lt;code&gt;f-&amp;gt;f_lasti&lt;/code&gt;) is still -1 because it has just been started, and this is a coroutine or async generator, then a non-None value can&amp;rsquo;t be passed as an argument, so an exception is raised&lt;/p&gt;
5700 &lt;/li&gt;
5701 &lt;li&gt;
5702 &lt;p&gt;Else, this is the first time it&amp;rsquo;s being called, and arguments are allowed. The value of the argument is pushed to the frame&amp;rsquo;s value stack&lt;/p&gt;
5703 &lt;/li&gt;
5704 &lt;li&gt;
5705 &lt;p&gt;The &lt;code&gt;f_back&lt;/code&gt; field of the frame is the caller to which return values are sent, so this is set to the current frame in the thread. This means that the return value is sent to the caller, not the creator of the generator&lt;/p&gt;
5706 &lt;/li&gt;
5707 &lt;li&gt;
5708 &lt;p&gt;The generator is marked as running&lt;/p&gt;
5709 &lt;/li&gt;
5710 &lt;li&gt;
5711 &lt;p&gt;The last exception in the generator&amp;rsquo;s exception info is copied from the last exception in the thread state&lt;/p&gt;
5712 &lt;/li&gt;
5713 &lt;li&gt;
5714 &lt;p&gt;The thread state exception info is set to the address of the generator&amp;rsquo;s exception info. This means that if the caller enters a breakpoint around the execution of a generator, the stack trace goes through the generator and the offending code is clear&lt;/p&gt;
5715 &lt;/li&gt;
5716 &lt;li&gt;
5717 &lt;p&gt;The frame inside the generator is executed within the &lt;code&gt;Python/ceval.c&lt;/code&gt; main execution loop, and the value returned&lt;/p&gt;
5718 &lt;/li&gt;
5719 &lt;li&gt;
5720 &lt;p&gt;The thread state last exception is reset to the value before the frame was called&lt;/p&gt;
5721 &lt;/li&gt;
5722 &lt;li&gt;
5723 &lt;p&gt;The generator is marked as not running&lt;/p&gt;
5724 &lt;/li&gt;
5725 &lt;li&gt;
5726 &lt;p&gt;The following cases then match the return value and any exceptions thrown by the call to the generator. Remember that generators should raise a &lt;code&gt;StopIteration&lt;/code&gt; when they are exhausted, either manually, or by not yielding a value. Coroutines and async generators should not:&lt;/p&gt;
5727 &lt;ul&gt;
5728 &lt;li&gt;If no result was returned from the frame, raise a &lt;code&gt;StopIteration&lt;/code&gt; for generators and &lt;code&gt;StopAsyncIteration&lt;/code&gt; for async generators&lt;/li&gt;
5729 &lt;li&gt;If a &lt;code&gt;StopIteration&lt;/code&gt; was explicitly raised, but this is a coroutine or an async generator, raise a &lt;code&gt;RuntimeError&lt;/code&gt; as this is not allowed&lt;/li&gt;
5730 &lt;li&gt;If a &lt;code&gt;StopAsyncIteration&lt;/code&gt; was explicitly raised and this is an async generator, raise a &lt;code&gt;RuntimeError&lt;/code&gt;, as this is not allowed&lt;/li&gt;
5731 &lt;/ul&gt;
5732 &lt;/li&gt;
5733 &lt;li&gt;
5734 &lt;p&gt;Lastly, the result is returned back to the caller of &lt;code&gt;__next__()&lt;/code&gt;&lt;/p&gt;
5735 &lt;/li&gt;
5736 &lt;/ol&gt;
5737 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
5738 &lt;span class=&quot;nf&quot;&gt;gen_send_ex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyGenObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;closing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5739 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5740 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;PyThreadState&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tstate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_PyThreadState_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;// 1.&lt;/span&gt;
5741 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;PyFrameObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;                   &lt;span class=&quot;c1&quot;&gt;// 2.&lt;/span&gt;
5742 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5743 
5744 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_running&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;// 3.&lt;/span&gt;
5745 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;generator already executing&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5746         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyCoro_CheckExact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5747             &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;coroutine already executing&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5748         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5749         &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyAsyncGen_CheckExact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5750             &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;async generator already executing&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5751         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5752         &lt;span class=&quot;n&quot;&gt;PyErr_SetString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyExc_ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5753         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5754     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5755 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_stacktop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 4.&lt;/span&gt;
5756 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyCoro_CheckExact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;closing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5757             &lt;span class=&quot;cm&quot;&gt;/* `gen` is an exhausted coroutine: raise an error,&lt;/span&gt;
5758 &lt;span class=&quot;cm&quot;&gt;               except when called from gen_close(), which should&lt;/span&gt;
5759 &lt;span class=&quot;cm&quot;&gt;               always be a silent method. */&lt;/span&gt;
5760 &lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;n&quot;&gt;PyErr_SetString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
5761 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;                &lt;span class=&quot;n&quot;&gt;PyExc_RuntimeError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
5762 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;                &lt;span class=&quot;s&quot;&gt;&amp;quot;cannot reuse already awaited coroutine&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 4a.&lt;/span&gt;
5763 &lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5764         &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5765             &lt;span class=&quot;cm&quot;&gt;/* `gen` is an exhausted generator:&lt;/span&gt;
5766 &lt;span class=&quot;cm&quot;&gt;               only set exception if called from send(). */&lt;/span&gt;
5767             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyAsyncGen_CheckExact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5768 &lt;span class=&quot;hll&quot;&gt;                &lt;span class=&quot;n&quot;&gt;PyErr_SetNone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyExc_StopAsyncIteration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 4b.&lt;/span&gt;
5769 &lt;/span&gt;            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5770             &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5771 &lt;span class=&quot;hll&quot;&gt;                &lt;span class=&quot;n&quot;&gt;PyErr_SetNone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyExc_StopIteration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;// 4c.&lt;/span&gt;
5772 &lt;/span&gt;            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5773         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5774         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5775     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5776 
5777     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_lasti&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5778 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 5.&lt;/span&gt;
5779 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;can&amp;#39;t send non-None value to a &amp;quot;&lt;/span&gt;
5780                               &lt;span class=&quot;s&quot;&gt;&amp;quot;just-started generator&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5781             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyCoro_CheckExact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5782                 &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NON_INIT_CORO_MSG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5783             &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5784             &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyAsyncGen_CheckExact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5785                 &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;can&amp;#39;t send non-None value to a &amp;quot;&lt;/span&gt;
5786                       &lt;span class=&quot;s&quot;&gt;&amp;quot;just-started async generator&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5787             &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5788             &lt;span class=&quot;n&quot;&gt;PyErr_SetString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyExc_TypeError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5789             &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5790         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5791 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 6.&lt;/span&gt;
5792 &lt;/span&gt;        &lt;span class=&quot;cm&quot;&gt;/* Push arg onto the frame&amp;#39;s value stack */&lt;/span&gt;
5793         &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;arg&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5794         &lt;span class=&quot;n&quot;&gt;Py_INCREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5795         &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_stacktop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5796     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5797 
5798     &lt;span class=&quot;cm&quot;&gt;/* Generators always return to their most recent caller, not&lt;/span&gt;
5799 &lt;span class=&quot;cm&quot;&gt;     * necessarily their creator. */&lt;/span&gt;
5800     &lt;span class=&quot;n&quot;&gt;Py_XINCREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tstate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5801     &lt;span class=&quot;n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_back&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5802 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_back&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tstate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;                          &lt;span class=&quot;c1&quot;&gt;// 7.&lt;/span&gt;
5803 &lt;/span&gt;
5804 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;                                &lt;span class=&quot;c1&quot;&gt;// 8.&lt;/span&gt;
5805 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_exc_state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;previous_item&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tstate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exc_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 9.&lt;/span&gt;
5806 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;tstate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exc_info&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_exc_state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;              &lt;span class=&quot;c1&quot;&gt;// 10.&lt;/span&gt;
5807 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyEval_EvalFrameEx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                &lt;span class=&quot;c1&quot;&gt;// 11.&lt;/span&gt;
5808 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;tstate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exc_info&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_exc_state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;previous_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 12.&lt;/span&gt;
5809 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_exc_state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;previous_item&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;             
5810 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;                                &lt;span class=&quot;c1&quot;&gt;// 13.&lt;/span&gt;
5811 &lt;/span&gt;
5812     &lt;span class=&quot;cm&quot;&gt;/* Don&amp;#39;t keep the reference to f_back any longer than necessary.  It&lt;/span&gt;
5813 &lt;span class=&quot;cm&quot;&gt;     * may keep a chain of frames alive or it could create a reference&lt;/span&gt;
5814 &lt;span class=&quot;cm&quot;&gt;     * cycle. */&lt;/span&gt;
5815     &lt;span class=&quot;n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_back&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tstate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5816     &lt;span class=&quot;n&quot;&gt;Py_CLEAR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5817 
5818     &lt;span class=&quot;cm&quot;&gt;/* If the generator just returned (as opposed to yielding), signal&lt;/span&gt;
5819 &lt;span class=&quot;cm&quot;&gt;     * that the generator is exhausted. */&lt;/span&gt;
5820 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_stacktop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// 14a.&lt;/span&gt;
5821 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5822             &lt;span class=&quot;cm&quot;&gt;/* Delay exception instantiation if we can */&lt;/span&gt;
5823             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyAsyncGen_CheckExact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5824                 &lt;span class=&quot;n&quot;&gt;PyErr_SetNone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyExc_StopAsyncIteration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5825             &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5826             &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5827                 &lt;span class=&quot;n&quot;&gt;PyErr_SetNone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyExc_StopIteration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5828             &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5829         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5830         &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5831             &lt;span class=&quot;cm&quot;&gt;/* Async generators cannot return anything but None */&lt;/span&gt;
5832             &lt;span class=&quot;n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyAsyncGen_CheckExact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
5833             &lt;span class=&quot;n&quot;&gt;_PyGen_SetStopIterationValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5834         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5835         &lt;span class=&quot;n&quot;&gt;Py_CLEAR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5836     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5837 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyErr_ExceptionMatches&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyExc_StopIteration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 14b.&lt;/span&gt;
5838 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;generator raised StopIteration&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5839         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyCoro_CheckExact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5840             &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;coroutine raised StopIteration&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5841         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5842         &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyAsyncGen_CheckExact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5843             &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;async generator raised StopIteration&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5844         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5845         &lt;span class=&quot;n&quot;&gt;_PyErr_FormatFromCause&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyExc_RuntimeError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;%s&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5846 
5847     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5848     &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyAsyncGen_CheckExact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;
5849 &lt;span class=&quot;hll&quot;&gt;             &lt;span class=&quot;n&quot;&gt;PyErr_ExceptionMatches&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyExc_StopAsyncIteration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// 14c.&lt;/span&gt;
5850 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5851         &lt;span class=&quot;cm&quot;&gt;/* code in `gen` raised a StopAsyncIteration error:&lt;/span&gt;
5852 &lt;span class=&quot;cm&quot;&gt;           raise a RuntimeError.&lt;/span&gt;
5853 &lt;span class=&quot;cm&quot;&gt;        */&lt;/span&gt;
5854         &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;async generator raised StopAsyncIteration&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5855         &lt;span class=&quot;n&quot;&gt;_PyErr_FormatFromCause&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyExc_RuntimeError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;%s&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5856     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5857 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5858 
5859 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 15.&lt;/span&gt;
5860 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5861 &lt;/pre&gt;&lt;/div&gt;
5862 
5863 &lt;p&gt;Going back to the evaluation of code objects whenever a function or module is called, there was a special case for generators, coroutines, and async generators in &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/ceval.c#L4045&quot;&gt;&lt;code&gt;_PyEval_EvalCodeWithName()&lt;/code&gt;&lt;/a&gt;. This function checks for the &lt;code&gt;CO_GENERATOR&lt;/code&gt;, &lt;code&gt;CO_COROUTINE&lt;/code&gt;, and &lt;code&gt;CO_ASYNC_GENERATOR&lt;/code&gt; flags on the code object.&lt;/p&gt;
5864 &lt;p&gt;When a new coroutine is created using &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/genobject.c#L1152&quot;&gt;&lt;code&gt;PyCoro_New()&lt;/code&gt;&lt;/a&gt;, a new async generator is created with &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/genobject.c#L1428&quot;&gt;&lt;code&gt;PyAsyncGen_New()&lt;/code&gt;&lt;/a&gt; or a generator with &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/genobject.c#L811&quot;&gt;&lt;code&gt;PyGen_NewWithQualName()&lt;/code&gt;&lt;/a&gt;. These objects are returned early instead of returning an evaluated frame, which is why you get a generator object after calling a function with a yield statement:&lt;/p&gt;
5865 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
5866 &lt;span class=&quot;nf&quot;&gt;_PyEval_EvalCodeWithName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;locals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5867 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5868     &lt;span class=&quot;cm&quot;&gt;/* Handle generator/coroutine/asynchronous generator */&lt;/span&gt;
5869     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_flags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CO_GENERATOR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CO_COROUTINE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CO_ASYNC_GENERATOR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5870         &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5871         &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coro_wrapper&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tstate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coroutine_wrapper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5872         &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_coro&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_flags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CO_COROUTINE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5873         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5874         &lt;span class=&quot;cm&quot;&gt;/* Create a new generator that owns the ready to run frame&lt;/span&gt;
5875 &lt;span class=&quot;cm&quot;&gt;         * and return that as the value. */&lt;/span&gt;
5876         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_coro&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5877             &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyCoro_New&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qualname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5878         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_flags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CO_ASYNC_GENERATOR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5879             &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyAsyncGen_New&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qualname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5880         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5881             &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyGen_NewWithQualName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qualname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5882         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5883         &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5884         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5885     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5886 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
5887 &lt;/pre&gt;&lt;/div&gt;
5888 
5889 &lt;p&gt;The flags in the code object were injected by the compiler after traversing the AST and seeing the &lt;code&gt;yield&lt;/code&gt; or &lt;code&gt;yield from&lt;/code&gt; statements or seeing the &lt;code&gt;coroutine&lt;/code&gt; decorator.&lt;/p&gt;
5890 &lt;p&gt;&lt;code&gt;PyGen_NewWithQualName()&lt;/code&gt; will call &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Objects/genobject.c#L778&quot;&gt;&lt;code&gt;gen_new_with_qualname()&lt;/code&gt;&lt;/a&gt; with the generated frame and then create the &lt;code&gt;PyGenObject&lt;/code&gt; with &lt;code&gt;NULL&lt;/code&gt; values and the compiled code object:&lt;/p&gt;
5891 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
5892 &lt;span class=&quot;nf&quot;&gt;gen_new_with_qualname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyTypeObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyFrameObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
5893                       &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;qualname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5894 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5895     &lt;span class=&quot;n&quot;&gt;PyGenObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject_GC_New&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyGenObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5896     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
5897         &lt;span class=&quot;n&quot;&gt;Py_DECREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5898         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5899     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5900     &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_frame&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5901     &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5902     &lt;span class=&quot;n&quot;&gt;Py_INCREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5903     &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_code&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f_code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5904     &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5905     &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_weakreflist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5906     &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_exc_state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exc_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5907     &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_exc_state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exc_value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5908     &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_exc_state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exc_traceback&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5909     &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_exc_state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;previous_item&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5910     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5911         &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5912     &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
5913         &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyCodeObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5914     &lt;span class=&quot;n&quot;&gt;Py_INCREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5915     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;qualname&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5916         &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_qualname&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qualname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5917     &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
5918         &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_qualname&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5919     &lt;span class=&quot;n&quot;&gt;Py_INCREF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gi_qualname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5920     &lt;span class=&quot;n&quot;&gt;_PyObject_GC_TRACK&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
5921     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
5922 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
5923 &lt;/pre&gt;&lt;/div&gt;
5924 
5925 &lt;p&gt;Bringing this all together you can see how the generator expression is a powerful syntax where a single keyword, &lt;code&gt;yield&lt;/code&gt; triggers a whole flow to create a unique object, copy a compiled code object as a property, set a frame, and store a list of variables in the local scope.&lt;/p&gt;
5926 &lt;p&gt;To the user of the generator expression, this all seems like magic, but under the covers it&amp;rsquo;s not &lt;em&gt;that&lt;/em&gt; complex.&lt;/p&gt;
5927 &lt;h3 id=&quot;conclusion_4&quot;&gt;Conclusion&lt;/h3&gt;
5928 &lt;p&gt;Now that you understand how some built-in types, you can explore other types. &lt;/p&gt;
5929 &lt;p&gt;When exploring Python classes, it is important to remember there are built-in types, written in C and classes inheriting from those types, written in Python or C.&lt;/p&gt;
5930 &lt;p&gt;Some libraries have types written in C instead of inheriting from the built-in types. One example is &lt;code&gt;numpy&lt;/code&gt;, a library for numeric arrays. The &lt;a href=&quot;https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html&quot;&gt;&lt;code&gt;nparray&lt;/code&gt;&lt;/a&gt; type is written in C, is highly efficient and performant.&lt;/p&gt;
5931 &lt;p&gt;In the next Part, we will explore the classes and functions defined in the standard library.&lt;/p&gt;
5932 &lt;h2 h1=&quot;h1&quot; id=&quot;part-5-the-cpython-standard-library&quot;&gt;Part 5: The CPython Standard Library&lt;/h2&gt;
5933 &lt;p&gt;Python has always come &amp;ldquo;batteries included.&amp;rdquo; This statement means that with a standard CPython distribution, there are libraries for working with files, threads, networks, web sites, music, keyboards, screens, text, and a whole manner of utilities.&lt;/p&gt;
5934 &lt;p&gt;Some of the batteries that come with CPython are more like AA batteries. They&amp;rsquo;re useful for everything, like the &lt;code&gt;collections&lt;/code&gt; module and the &lt;code&gt;sys&lt;/code&gt; module. Some of them are a bit more obscure, like a small watch battery that you never know when it might come in useful.&lt;/p&gt;
5935 &lt;p&gt;There are 2 types of modules in the CPython standard library:&lt;/p&gt;
5936 &lt;ol&gt;
5937 &lt;li&gt;Those written in pure Python that provides a utility&lt;/li&gt;
5938 &lt;li&gt;Those written in C with Python wrappers&lt;/li&gt;
5939 &lt;/ol&gt;
5940 &lt;p&gt;We will explore both types.&lt;/p&gt;
5941 &lt;h3 id=&quot;python-modules&quot;&gt;Python Modules&lt;/h3&gt;
5942 &lt;p&gt;The modules written in pure Python are all located in the &lt;code&gt;Lib/&lt;/code&gt; directory in the source code. Some of the larger modules have submodules in subfolders, like the &lt;code&gt;email&lt;/code&gt; module.&lt;/p&gt;
5943 &lt;p&gt;An easy module to look at would be the &lt;code&gt;colorsys&lt;/code&gt; module. It&amp;rsquo;s only a few hundred lines of Python code. You may not have come across it before. The &lt;code&gt;colorsys&lt;/code&gt; module has some utility functions for converting color scales.&lt;/p&gt;
5944 &lt;p&gt;When you install a Python distribution from source, standard library modules are copied from the &lt;code&gt;Lib&lt;/code&gt; folder into the distribution folder. This folder is always part of your path when you start Python, so you can &lt;code&gt;import&lt;/code&gt; the modules without having to worry about where they&amp;rsquo;re located. &lt;/p&gt;
5945 &lt;p&gt;For example:&lt;/p&gt;
5946 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;colorsys&lt;/span&gt;
5947 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colorsys&lt;/span&gt;
5948 &lt;span class=&quot;go&quot;&gt;&amp;lt;module &amp;#39;colorsys&amp;#39; from &amp;#39;/usr/shared/lib/python3.7/colorsys.py&amp;#39;&amp;gt;&lt;/span&gt;
5949 
5950 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colorsys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rgb_to_hls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5951 &lt;span class=&quot;go&quot;&gt;(0.0, 127.5, -1.007905138339921) &lt;/span&gt;
5952 &lt;/pre&gt;&lt;/div&gt;
5953 
5954 &lt;p&gt;We can see the source code of &lt;code&gt;rgb_to_hls()&lt;/code&gt; inside &lt;code&gt;Lib/colorsys.py&lt;/code&gt;:&lt;/p&gt;
5955 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# HLS: Hue, Luminance, Saturation&lt;/span&gt;
5956 &lt;span class=&quot;c1&quot;&gt;# H: position in the spectrum&lt;/span&gt;
5957 &lt;span class=&quot;c1&quot;&gt;# L: color lightness&lt;/span&gt;
5958 &lt;span class=&quot;c1&quot;&gt;# S: color saturation&lt;/span&gt;
5959 
5960 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;rgb_to_hls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
5961     &lt;span class=&quot;n&quot;&gt;maxc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5962     &lt;span class=&quot;n&quot;&gt;minc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5963     &lt;span class=&quot;c1&quot;&gt;# XXX Can optimize (maxc+minc) and (maxc-minc)&lt;/span&gt;
5964     &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt;
5965     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;minc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
5966         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;
5967     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
5968         &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5969     &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
5970         &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5971     &lt;span class=&quot;n&quot;&gt;rc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5972     &lt;span class=&quot;n&quot;&gt;gc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5973     &lt;span class=&quot;n&quot;&gt;bc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5974     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
5975         &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gc&lt;/span&gt;
5976     &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
5977         &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bc&lt;/span&gt;
5978     &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
5979         &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rc&lt;/span&gt;
5980     &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;6.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;
5981     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;
5982 &lt;/pre&gt;&lt;/div&gt;
5983 
5984 &lt;p&gt;There&amp;rsquo;s nothing special about this function, it&amp;rsquo;s just standard Python. You&amp;rsquo;ll find similar things with all of the pure Python standard library modules. They&amp;rsquo;re just written in plain Python, well laid out and easy to understand. You may even spot improvements or bugs, so you can make changes to them and contribute it to the Python distribution. We&amp;rsquo;ll cover that toward the end of this article.&lt;/p&gt;
5985 &lt;h3 id=&quot;python-and-c-modules&quot;&gt;Python and C Modules&lt;/h3&gt;
5986 &lt;p&gt;The remainder of modules are written in C, or a combination or Python and C. The source code for these is in &lt;code&gt;Lib/&lt;/code&gt; for the Python component, and &lt;code&gt;Modules/&lt;/code&gt; for the C component. There are two exceptions to this rule, the &lt;code&gt;sys&lt;/code&gt; module, found in &lt;code&gt;Python/sysmodule.c&lt;/code&gt; and the &lt;code&gt;__builtins__&lt;/code&gt; module, found in &lt;code&gt;Python/bltinmodule.c&lt;/code&gt;.&lt;/p&gt;
5987 &lt;p&gt;Python will &lt;code&gt;import * from __builtins__&lt;/code&gt; when an interpreter is instantiated, so all of the functions like &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/bltinmodule.c#L1821&quot;&gt;&lt;code&gt;print()&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/bltinmodule.c#L688&quot;&gt;&lt;code&gt;chr()&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Python/bltinmodule.c#L672&quot;&gt;&lt;code&gt;format()&lt;/code&gt;&lt;/a&gt;, etc. are found within &lt;code&gt;Python/bltinmodule.c&lt;/code&gt;.&lt;/p&gt;
5988 &lt;p&gt;Because the &lt;code&gt;sys&lt;/code&gt; module is so specific to the interpreter and the internals of CPython, that is found inside the &lt;code&gt;Python&lt;/code&gt; directly. It is also marked as an &amp;ldquo;implementation detail&amp;rdquo; of CPython and not found in other distributions.&lt;/p&gt;
5989 &lt;p&gt;The built-in &lt;code&gt;print()&lt;/code&gt; function was probably the first thing you learned to do in Python. So what happens when you type &lt;code&gt;print(&quot;hello world!&quot;)&lt;/code&gt;?&lt;/p&gt;
5990 &lt;ol&gt;
5991 &lt;li&gt;The argument &lt;code&gt;&quot;hello world&quot;&lt;/code&gt; was converted from a string constant to a &lt;code&gt;PyUnicodeObject&lt;/code&gt; by the compiler&lt;/li&gt;
5992 &lt;li&gt;&lt;code&gt;builtin_print()&lt;/code&gt; was executed with 1 argument, and NULL &lt;code&gt;kwnames&lt;/code&gt;&lt;/li&gt;
5993 &lt;li&gt;The &lt;code&gt;file&lt;/code&gt; variable is set to &lt;code&gt;PyId_stdout&lt;/code&gt;, the system&amp;rsquo;s &lt;code&gt;stdout&lt;/code&gt; handle&lt;/li&gt;
5994 &lt;li&gt;Each argument is sent to &lt;code&gt;file&lt;/code&gt;&lt;/li&gt;
5995 &lt;li&gt;A line break, &lt;code&gt;\n&lt;/code&gt; is sent to &lt;code&gt;file&lt;/code&gt;&lt;/li&gt;
5996 &lt;/ol&gt;
5997 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
5998 &lt;span class=&quot;nf&quot;&gt;builtin_print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_ssize_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwnames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
5999 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
6000     &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
6001     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
6002 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_PySys_GetObjectId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyId_stdout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
6003 &lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
6004     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
6005     &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
6006     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
6007         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
6008             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6009 &lt;span class=&quot;hll&quot;&gt;                &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyFile_WriteString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot; &amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
6010 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
6011                 &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyFile_WriteObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
6012                                          &lt;span class=&quot;n&quot;&gt;Py_PRINT_RAW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
6013             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6014                 &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
6015         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
6016         &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyFile_WriteObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_PRINT_RAW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
6017         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6018             &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
6019     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
6020 
6021     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6022 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyFile_WriteString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
6023 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
6024         &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyFile_WriteObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Py_PRINT_RAW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
6025     &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
6026     &lt;span class=&quot;n&quot;&gt;Py_RETURN_NONE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
6027 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
6028 &lt;/pre&gt;&lt;/div&gt;
6029 
6030 &lt;p&gt;The contents of some modules written in C expose operating system functions. Because the CPython source code needs to compile to macOS, Windows, Linux, and other *nix-based operating systems, there are some special cases.&lt;/p&gt;
6031 &lt;p&gt;The &lt;code&gt;time&lt;/code&gt; module is a good example. The way that Windows keeps and stores time in the Operating System is fundamentally different than Linux and macOS. This is one of the reasons why the accuracy of the clock functions differs &lt;a href=&quot;https://docs.python.org/3/library/time.html#time.clock_gettime_ns&quot;&gt;between operating systems&lt;/a&gt;.&lt;/p&gt;
6032 &lt;p&gt;In &lt;code&gt;Modules/timemodule.c&lt;/code&gt;, the operating system time functions for Unix-based systems are imported from &lt;code&gt;&amp;lt;sys/times.h&amp;gt;&lt;/code&gt;:&lt;/p&gt;
6033 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;#ifdef HAVE_SYS_TIMES_H&lt;/span&gt;
6034 &lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;sys/times.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;&lt;/span&gt;
6035 &lt;span class=&quot;cp&quot;&gt;#endif&lt;/span&gt;
6036 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
6037 &lt;span class=&quot;cp&quot;&gt;#ifdef MS_WINDOWS&lt;/span&gt;
6038 &lt;span class=&quot;cp&quot;&gt;#define WIN32_LEAN_AND_MEAN&lt;/span&gt;
6039 &lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;windows.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;&lt;/span&gt;
6040 &lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;quot;pythread.h&amp;quot;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;&lt;/span&gt;
6041 &lt;span class=&quot;cp&quot;&gt;#endif &lt;/span&gt;&lt;span class=&quot;cm&quot;&gt;/* MS_WINDOWS */&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;&lt;/span&gt;
6042 &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
6043 &lt;/pre&gt;&lt;/div&gt;
6044 
6045 &lt;p&gt;Later in the file, &lt;code&gt;time_process_time_ns()&lt;/code&gt; is defined as a wrapper for &lt;code&gt;_PyTime_GetProcessTimeWithInfo()&lt;/code&gt;:&lt;/p&gt;
6046 &lt;div class=&quot;highlight c&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
6047 &lt;span class=&quot;nf&quot;&gt;time_process_time_ns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unused&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6048 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
6049     &lt;span class=&quot;n&quot;&gt;_PyTime_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
6050     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_PyTime_GetProcessTimeWithInfo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
6051         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
6052     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
6053     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_PyTime_AsNanosecondsObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
6054 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
6055 &lt;/pre&gt;&lt;/div&gt;
6056 
6057 &lt;p&gt;&lt;a href=&quot;https://github.com/python/cpython/blob/d93605de7232da5e6a182fd1d5c220639e900159/Modules/timemodule.c#L1120&quot;&gt;&lt;code&gt;_PyTime_GetProcessTimeWithInfo()&lt;/code&gt;&lt;/a&gt; is implemented multiple different ways in the source code, but only certain parts are compiled into the binary for the module, depending on the operating system. Windows systems will call &lt;code&gt;GetProcessTimes()&lt;/code&gt; and Unix systems will call &lt;code&gt;clock_gettime()&lt;/code&gt;.&lt;/p&gt;
6058 &lt;p&gt;Other modules that have multiple implementations for the same API are &lt;a href=&quot;https://realpython.com/intro-to-python-threading/&quot;&gt;the threading module&lt;/a&gt;, the file system module, and the networking modules. Because the Operating Systems behave differently, the CPython source code implements the same behavior as best as it can and exposes it using a consistent, abstracted API.&lt;/p&gt;
6059 &lt;h3 id=&quot;the-cpython-regression-test-suite&quot;&gt;The CPython Regression Test Suite&lt;/h3&gt;
6060 &lt;p&gt;CPython has a robust and extensive test suite covering the core interpreter, the standard library, the tooling and distribution for both Windows and Linux/macOS.&lt;/p&gt;
6061 &lt;p&gt;The test suite is located in &lt;code&gt;Lib/test&lt;/code&gt; and written almost entirely in Python.&lt;/p&gt;
6062 &lt;p&gt;The full test suite is a Python package, so can be run using the Python interpreter that you&amp;rsquo;ve compiled. Change directory to the &lt;code&gt;Lib&lt;/code&gt; directory and run &lt;code&gt;python -m test -j2&lt;/code&gt;, where &lt;code&gt;j2&lt;/code&gt; means to use 2 CPUs.&lt;/p&gt;
6063 &lt;p&gt;On Windows use the &lt;code&gt;rt.bat&lt;/code&gt; script inside the PCBuild folder, ensuring that you have built the &lt;strong&gt;Release&lt;/strong&gt; configuration from Visual Studio in advance:&lt;/p&gt;
6064 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; PCbuild
6065 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; rt.bat -q
6066 
6067 &lt;span class=&quot;go&quot;&gt;C:\repos\cpython\PCbuild&amp;gt;&amp;quot;C:\repos\cpython\PCbuild\win32\python.exe&amp;quot;  -u -Wd -E -bb -m test&lt;/span&gt;
6068 &lt;span class=&quot;go&quot;&gt;== CPython 3.8.0b4&lt;/span&gt;
6069 &lt;span class=&quot;go&quot;&gt;== Windows-10-10.0.17134-SP0 little-endian&lt;/span&gt;
6070 &lt;span class=&quot;go&quot;&gt;== cwd: C:\repos\cpython\build\test_python_2784&lt;/span&gt;
6071 &lt;span class=&quot;go&quot;&gt;== CPU count: 2&lt;/span&gt;
6072 &lt;span class=&quot;go&quot;&gt;== encodings: locale=cp1252, FS=utf-8&lt;/span&gt;
6073 &lt;span class=&quot;go&quot;&gt;Run tests sequentially&lt;/span&gt;
6074 &lt;span class=&quot;go&quot;&gt;0:00:00 [  1/420] test_grammar&lt;/span&gt;
6075 &lt;span class=&quot;go&quot;&gt;0:00:00 [  2/420] test_opcodes&lt;/span&gt;
6076 &lt;span class=&quot;go&quot;&gt;0:00:00 [  3/420] test_dict&lt;/span&gt;
6077 &lt;span class=&quot;go&quot;&gt;0:00:00 [  4/420] test_builtin&lt;/span&gt;
6078 &lt;span class=&quot;go&quot;&gt;...&lt;/span&gt;
6079 &lt;/pre&gt;&lt;/div&gt;
6080 
6081 &lt;p&gt;On Linux:&lt;/p&gt;
6082 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; Lib
6083 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; ../python -m &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt; -j2   
6084 &lt;span class=&quot;go&quot;&gt;== CPython 3.8.0b4&lt;/span&gt;
6085 &lt;span class=&quot;go&quot;&gt;== macOS-10.14.3-x86_64-i386-64bit little-endian&lt;/span&gt;
6086 &lt;span class=&quot;go&quot;&gt;== cwd: /Users/anthonyshaw/cpython/build/test_python_23399&lt;/span&gt;
6087 &lt;span class=&quot;go&quot;&gt;== CPU count: 4&lt;/span&gt;
6088 &lt;span class=&quot;go&quot;&gt;== encodings: locale=UTF-8, FS=utf-8&lt;/span&gt;
6089 &lt;span class=&quot;go&quot;&gt;Run tests in parallel using 2 child processes&lt;/span&gt;
6090 &lt;span class=&quot;go&quot;&gt;0:00:00 load avg: 2.14 [  1/420] test_opcodes passed&lt;/span&gt;
6091 &lt;span class=&quot;go&quot;&gt;0:00:00 load avg: 2.14 [  2/420] test_grammar passed&lt;/span&gt;
6092 &lt;span class=&quot;go&quot;&gt;...&lt;/span&gt;
6093 &lt;/pre&gt;&lt;/div&gt;
6094 
6095 &lt;p&gt;On macOS:&lt;/p&gt;
6096 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; Lib
6097 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; ../python.exe -m &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt; -j2   
6098 &lt;span class=&quot;go&quot;&gt;== CPython 3.8.0b4&lt;/span&gt;
6099 &lt;span class=&quot;go&quot;&gt;== macOS-10.14.3-x86_64-i386-64bit little-endian&lt;/span&gt;
6100 &lt;span class=&quot;go&quot;&gt;== cwd: /Users/anthonyshaw/cpython/build/test_python_23399&lt;/span&gt;
6101 &lt;span class=&quot;go&quot;&gt;== CPU count: 4&lt;/span&gt;
6102 &lt;span class=&quot;go&quot;&gt;== encodings: locale=UTF-8, FS=utf-8&lt;/span&gt;
6103 &lt;span class=&quot;go&quot;&gt;Run tests in parallel using 2 child processes&lt;/span&gt;
6104 &lt;span class=&quot;go&quot;&gt;0:00:00 load avg: 2.14 [  1/420] test_opcodes passed&lt;/span&gt;
6105 &lt;span class=&quot;go&quot;&gt;0:00:00 load avg: 2.14 [  2/420] test_grammar passed&lt;/span&gt;
6106 &lt;span class=&quot;go&quot;&gt;...&lt;/span&gt;
6107 &lt;/pre&gt;&lt;/div&gt;
6108 
6109 &lt;p&gt;Some tests require certain flags; otherwise they are skipped. For example, many of the IDLE tests require a GUI.&lt;/p&gt;
6110 &lt;p&gt;To see a list of test suites in the configuration, use the &lt;code&gt;--list-tests&lt;/code&gt; flag:&lt;/p&gt;
6111 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; ../python.exe -m &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt; --list-tests
6112 
6113 &lt;span class=&quot;go&quot;&gt;test_grammar&lt;/span&gt;
6114 &lt;span class=&quot;go&quot;&gt;test_opcodes&lt;/span&gt;
6115 &lt;span class=&quot;go&quot;&gt;test_dict&lt;/span&gt;
6116 &lt;span class=&quot;go&quot;&gt;test_builtin&lt;/span&gt;
6117 &lt;span class=&quot;go&quot;&gt;test_exceptions&lt;/span&gt;
6118 &lt;span class=&quot;go&quot;&gt;...&lt;/span&gt;
6119 &lt;/pre&gt;&lt;/div&gt;
6120 
6121 &lt;p&gt;You can run specific tests by providing the test suite as the first argument:&lt;/p&gt;
6122 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; ../python.exe -m &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt; test_webbrowser
6123 
6124 &lt;span class=&quot;go&quot;&gt;Run tests sequentially&lt;/span&gt;
6125 &lt;span class=&quot;go&quot;&gt;0:00:00 load avg: 2.74 [1/1] test_webbrowser&lt;/span&gt;
6126 
6127 &lt;span class=&quot;go&quot;&gt;== Tests result: SUCCESS ==&lt;/span&gt;
6128 
6129 &lt;span class=&quot;go&quot;&gt;1 test OK.&lt;/span&gt;
6130 
6131 &lt;span class=&quot;go&quot;&gt;Total duration: 117 ms&lt;/span&gt;
6132 &lt;span class=&quot;go&quot;&gt;Tests result: SUCCESS&lt;/span&gt;
6133 &lt;/pre&gt;&lt;/div&gt;
6134 
6135 &lt;p&gt;You can also see a detailed list of tests that were executed with the result using the &lt;code&gt;-v&lt;/code&gt; argument:&lt;/p&gt;
6136 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; ../python.exe -m &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt; test_webbrowser -v
6137 
6138 &lt;span class=&quot;go&quot;&gt;== CPython 3.8.0b4 &lt;/span&gt;
6139 &lt;span class=&quot;go&quot;&gt;== macOS-10.14.3-x86_64-i386-64bit little-endian&lt;/span&gt;
6140 &lt;span class=&quot;go&quot;&gt;== cwd: /Users/anthonyshaw/cpython/build/test_python_24562&lt;/span&gt;
6141 &lt;span class=&quot;go&quot;&gt;== CPU count: 4&lt;/span&gt;
6142 &lt;span class=&quot;go&quot;&gt;== encodings: locale=UTF-8, FS=utf-8&lt;/span&gt;
6143 &lt;span class=&quot;go&quot;&gt;Run tests sequentially&lt;/span&gt;
6144 &lt;span class=&quot;go&quot;&gt;0:00:00 load avg: 2.36 [1/1] test_webbrowser&lt;/span&gt;
6145 &lt;span class=&quot;go&quot;&gt;test_open (test.test_webbrowser.BackgroundBrowserCommandTest) ... ok&lt;/span&gt;
6146 &lt;span class=&quot;go&quot;&gt;test_register (test.test_webbrowser.BrowserRegistrationTest) ... ok&lt;/span&gt;
6147 &lt;span class=&quot;go&quot;&gt;test_register_default (test.test_webbrowser.BrowserRegistrationTest) ... ok&lt;/span&gt;
6148 &lt;span class=&quot;go&quot;&gt;test_register_preferred (test.test_webbrowser.BrowserRegistrationTest) ... ok&lt;/span&gt;
6149 &lt;span class=&quot;go&quot;&gt;test_open (test.test_webbrowser.ChromeCommandTest) ... ok&lt;/span&gt;
6150 &lt;span class=&quot;go&quot;&gt;test_open_new (test.test_webbrowser.ChromeCommandTest) ... ok&lt;/span&gt;
6151 &lt;span class=&quot;go&quot;&gt;...&lt;/span&gt;
6152 &lt;span class=&quot;go&quot;&gt;test_open_with_autoraise_false (test.test_webbrowser.OperaCommandTest) ... ok&lt;/span&gt;
6153 
6154 &lt;span class=&quot;go&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;
6155 
6156 &lt;span class=&quot;go&quot;&gt;Ran 34 tests in 0.056s&lt;/span&gt;
6157 
6158 &lt;span class=&quot;go&quot;&gt;OK (skipped=2)&lt;/span&gt;
6159 
6160 &lt;span class=&quot;go&quot;&gt;== Tests result: SUCCESS ==&lt;/span&gt;
6161 
6162 &lt;span class=&quot;go&quot;&gt;1 test OK.&lt;/span&gt;
6163 
6164 &lt;span class=&quot;go&quot;&gt;Total duration: 134 ms&lt;/span&gt;
6165 &lt;span class=&quot;go&quot;&gt;Tests result: SUCCESS&lt;/span&gt;
6166 &lt;/pre&gt;&lt;/div&gt;
6167 
6168 &lt;p&gt;Understanding how to use the test suite and checking the state of the version you have compiled is very important if you wish to make changes to CPython. Before you start making changes, you should run the whole test suite and make sure everything is passing.&lt;/p&gt;
6169 &lt;h3 id=&quot;installing-a-custom-version&quot;&gt;Installing a Custom Version&lt;/h3&gt;
6170 &lt;p&gt;From your source repository, if you&amp;rsquo;re happy with your changes and want to use them inside your system, you can install it as a custom version.&lt;/p&gt;
6171 &lt;p&gt;For macOS and Linux, you can use the &lt;code&gt;altinstall&lt;/code&gt; command, which won&amp;rsquo;t create symlinks for &lt;code&gt;python3&lt;/code&gt; and install a standalone version:&lt;/p&gt;
6172 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; make altinstall
6173 &lt;/pre&gt;&lt;/div&gt;
6174 
6175 &lt;p&gt;For Windows, you have to change the build configuration from &lt;code&gt;Debug&lt;/code&gt; to &lt;code&gt;Release&lt;/code&gt;, then copy the packaged binaries to a directory on your computer which is part of the system path.&lt;/p&gt;
6176 &lt;h2 id=&quot;the-cpython-source-code-conclusion&quot;&gt;The CPython Source Code: Conclusion&lt;/h2&gt;
6177 &lt;p&gt;Congratulations, you made it! Did your tea get cold? Make yourself another cup. You&amp;rsquo;ve earned it.&lt;/p&gt;
6178 &lt;p&gt;Now that you&amp;rsquo;ve seen the CPython source code, the modules, the compiler, and the tooling, you may wish to make some changes and contribute them back to the Python ecosystem.&lt;/p&gt;
6179 &lt;p&gt;The &lt;a href=&quot;https://devguide.python.org/&quot;&gt;official dev guide&lt;/a&gt; contains plenty of resources for beginners. You&amp;rsquo;ve already taken the first step, to understand the source, knowing how to change, compile, and test the CPython applications.&lt;/p&gt;
6180 &lt;p&gt;Think back to all the things you&amp;rsquo;ve learned about CPython over this article. All the pieces of magic to which you&amp;rsquo;ve learned the secrets. The journey doesn&amp;rsquo;t stop here. &lt;/p&gt;
6181 &lt;p&gt;This might be a good time to learn more about Python and C. Who knows: you could be contributing more and more to the CPython project!&lt;/p&gt;
6182         &lt;hr /&gt;
6183         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
6184       </content>
6185     </entry>
6186   
6187     <entry>
6188       <title>Python Histogram Plotting: NumPy, Matplotlib, Pandas &amp; Seaborn</title>
6189       <id>https://realpython.com/courses/python-histograms/</id>
6190       <link href="https://realpython.com/courses/python-histograms/"/>
6191       <updated>2019-08-20T14:00:00+00:00</updated>
6192       <summary>In this course, you&#39;ll be equipped to make production-quality, presentation-ready Python histogram plots with a range of choices and features. It&#39;s your one-stop shop for constructing and manipulating histograms with Python&#39;s scientific stack.</summary>
6193       <content type="html">
6194         &lt;p&gt;In this course, you&amp;rsquo;ll be equipped to make production-quality, presentation-ready Python histogram plots with a range of choices and features.&lt;/p&gt;
6195 &lt;p&gt;If you have introductory to intermediate knowledge in Python and statistics, then you can use this article as a one-stop shop for building and plotting histograms in Python using libraries from its scientific stack, including NumPy, Matplotlib, Pandas, and Seaborn.&lt;/p&gt;
6196 &lt;p&gt;A histogram is a great tool for quickly assessing a &lt;a href=&quot;https://en.wikipedia.org/wiki/Probability_distribution&quot;&gt;probability distribution&lt;/a&gt; that is intuitively understood by almost any audience.  Python offers a handful of different options for building and plotting histograms.  Most people know a histogram by its graphical representation, which is similar to a bar graph:&lt;/p&gt;
6197 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/commute_times.621e5b1ce062.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-75&quot; src=&quot;https://files.realpython.com/media/commute_times.621e5b1ce062.png&quot; width=&quot;1152&quot; height=&quot;888&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/commute_times.621e5b1ce062.png&amp;amp;w=288&amp;amp;sig=408f56b07d4fb71d47171405f51e3cb58a7e6cc2 288w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/commute_times.621e5b1ce062.png&amp;amp;w=576&amp;amp;sig=a2b0324c81a7cd89fe7ceaacdcd4ae797ad1a587 576w, https://files.realpython.com/media/commute_times.621e5b1ce062.png 1152w&quot; sizes=&quot;75vw&quot; alt=&quot;Histogram of commute times for 1000 commuters&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6198 &lt;p&gt;This course will guide you through creating plots like the one above as well as more complex ones.  Here&amp;rsquo;s what you&amp;rsquo;ll cover:&lt;/p&gt;
6199 &lt;ul&gt;
6200 &lt;li&gt;Building histograms in pure Python, without use of third party libraries&lt;/li&gt;
6201 &lt;li&gt;Constructing histograms with NumPy to summarize the underlying data&lt;/li&gt;
6202 &lt;li&gt;Plotting the resulting histogram with Matplotlib, Pandas, and Seaborn&lt;/li&gt;
6203 &lt;/ul&gt;
6204 &lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Free Bonus:&lt;/strong&gt; Short on time? &lt;a href=&quot;https://realpython.com/optins/view/histograms-cheatsheet/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-histograms-cheatsheet&quot; data-focus=&quot;false&quot;&gt;Click here to get access to a free two-page Python histograms cheat sheet&lt;/a&gt; that summarizes the techniques explained in this tutorial.&lt;/p&gt;&lt;/div&gt;
6205         &lt;hr /&gt;
6206         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
6207       </content>
6208     </entry>
6209   
6210     <entry>
6211       <title>How to Make a Discord Bot in Python</title>
6212       <id>https://realpython.com/how-to-make-a-discord-bot-python/</id>
6213       <link href="https://realpython.com/how-to-make-a-discord-bot-python/"/>
6214       <updated>2019-08-19T14:00:00+00:00</updated>
6215       <summary>In this step-by-step tutorial, you&#39;ll learn how to make a Discord bot in Python and interact with several APIs. You&#39;ll learn how to handle events, accept commands, validate and verify input, and all the basics that can help you create useful and exciting automations!</summary>
6216       <content type="html">
6217         &lt;p&gt;In a world where video games are so important to so many people, communication and community around games are vital. Discord offers both of those and more in one well-designed package. In this tutorial, you&amp;rsquo;ll learn how to make a Discord bot in Python so that you can make the most of this fantastic platform.&lt;/p&gt;
6218 &lt;p&gt;&lt;strong&gt;By the end of this article you&amp;rsquo;ll learn:&lt;/strong&gt;&lt;/p&gt;
6219 &lt;ul&gt;
6220 &lt;li&gt;What Discord is and why it&amp;rsquo;s so valuable&lt;/li&gt;
6221 &lt;li&gt;How to make a Discord bot through the Developer Portal&lt;/li&gt;
6222 &lt;li&gt;How to create Discord connections&lt;/li&gt;
6223 &lt;li&gt;How to handle events&lt;/li&gt;
6224 &lt;li&gt;How to accept commands and validate assumptions&lt;/li&gt;
6225 &lt;li&gt;How to interact with various Discord APIs&lt;/li&gt;
6226 &lt;/ul&gt;
6227 &lt;p&gt;You&amp;rsquo;ll begin by learning what Discord is and why it&amp;rsquo;s valuable.&lt;/p&gt;
6228 &lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Free Bonus:&lt;/strong&gt; &lt;a href=&quot;&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-tricks-sample&quot; data-focus=&quot;false&quot;&gt;Click here to get access to a chapter from Python Tricks: The Book&lt;/a&gt; that shows you Python&#39;s best practices with simple examples you can apply instantly to write more beautiful + Pythonic code.&lt;/p&gt;&lt;/div&gt;
6229 
6230 &lt;h2 id=&quot;what-is-discord&quot;&gt;What Is Discord?&lt;/h2&gt;
6231 &lt;p&gt;&lt;a href=&quot;https://discordapp.com/&quot;&gt;Discord&lt;/a&gt; is a voice and text communication platform for gamers.&lt;/p&gt;
6232 &lt;p&gt;Players, streamers, and developers use Discord to discuss games, answer questions, chat while they play, and much more. It even has a game store, complete with critical reviews and a subscription service. It is nearly a one-stop shop for gaming communities.&lt;/p&gt;
6233 &lt;p&gt;While there are many things you can build using Discord&amp;rsquo;s &lt;a href=&quot;https://discordapp.com/developers/docs/intro&quot;&gt;APIs&lt;/a&gt;, this tutorial will focus on a particular learning outcome: how to make a Discord bot in Python.&lt;/p&gt;
6234 &lt;h2 id=&quot;what-is-a-bot&quot;&gt;What Is a Bot?&lt;/h2&gt;
6235 &lt;p&gt;Discord is growing in popularity. As such, automated processes, such as banning inappropriate users and reacting to user requests are vital for a community to thrive and grow.&lt;/p&gt;
6236 &lt;p&gt;Automated programs that look and act like users and automatically respond to events and commands on Discord are called &lt;strong&gt;bot users&lt;/strong&gt;. Discord bot users (or just &lt;strong&gt;bots&lt;/strong&gt;) have nearly &lt;a href=&quot;https://discordbots.org&quot;&gt;unlimited applications&lt;/a&gt;.&lt;/p&gt;
6237 &lt;p&gt;For example, let&amp;rsquo;s say you&amp;rsquo;re managing a new Discord guild and a user joins for the very first time. Excited, you may personally reach out to that user and welcome them to your community. You might also tell them about your channels or ask them to introduce themselves.&lt;/p&gt;
6238 &lt;p&gt;The user feels welcomed and enjoys the discussions that happen in your guild and they, in turn, invite friends.&lt;/p&gt;
6239 &lt;p&gt;Over time, your community grows so big that it&amp;rsquo;s no longer feasible to personally reach out to each new member, but you still want to send them something to recognize them as a new member of the guild.&lt;/p&gt;
6240 &lt;p&gt;With a bot, it&amp;rsquo;s possible to automatically react to the new member joining your guild. You can even customize its behavior based on context and control how it interacts with each new user.&lt;/p&gt;
6241 &lt;p&gt;This is great, but it&amp;rsquo;s only one small example of how a bot can be useful. There are so many opportunities for you to be creative with bots, once you know how to make them.&lt;/p&gt;
6242 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
6243 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Although Discord allows you to create bots that deal with voice communication, this article will stick to the text side of the service.&lt;/p&gt;
6244 &lt;/div&gt;
6245 &lt;p&gt;There are two key steps when you&amp;rsquo;re creating a bot:&lt;/p&gt;
6246 &lt;ol&gt;
6247 &lt;li&gt;Create the bot user on Discord and register it with a guild.&lt;/li&gt;
6248 &lt;li&gt;Write code that uses Discord&amp;rsquo;s APIs and implements your bot&amp;rsquo;s behaviors.&lt;/li&gt;
6249 &lt;/ol&gt;
6250 &lt;p&gt;In the next section, you&amp;rsquo;ll learn how to make a Discord bot in Discord&amp;rsquo;s &lt;a href=&quot;https://discordapp.com/developers/applications&quot;&gt;Developer Portal&lt;/a&gt;.&lt;/p&gt;
6251 &lt;h2 id=&quot;how-to-make-a-discord-bot-in-the-developer-portal&quot;&gt;How to Make a Discord Bot in the Developer Portal&lt;/h2&gt;
6252 &lt;p&gt;Before you can dive into any Python code to handle events and create exciting automations, you need to first create a few Discord components:&lt;/p&gt;
6253 &lt;ol&gt;
6254 &lt;li&gt;An account&lt;/li&gt;
6255 &lt;li&gt;An application&lt;/li&gt;
6256 &lt;li&gt;A bot&lt;/li&gt;
6257 &lt;li&gt;A guild&lt;/li&gt;
6258 &lt;/ol&gt;
6259 &lt;p&gt;You&amp;rsquo;ll learn more about each piece in the following sections.&lt;/p&gt;
6260 &lt;p&gt;Once you&amp;rsquo;ve created all of these components, you&amp;rsquo;ll tie them together by registering your bot with your guild.&lt;/p&gt;
6261 &lt;p&gt;You can get started by heading to Discord&amp;rsquo;s &lt;a href=&quot;http://discordapp.com/developers/applications&quot;&gt;Developer Portal&lt;/a&gt;.&lt;/p&gt;
6262 &lt;h3 id=&quot;creating-a-discord-account&quot;&gt;Creating a Discord Account&lt;/h3&gt;
6263 &lt;p&gt;The first thing you&amp;rsquo;ll see is a landing page where you&amp;rsquo;ll need to either login, if you have an existing account, or create a new account:&lt;/p&gt;
6264 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-register-user.41a9c2bc4db9.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-register-user.41a9c2bc4db9.png&quot; width=&quot;3024&quot; height=&quot;1762&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-register-user.41a9c2bc4db9.png&amp;amp;w=756&amp;amp;sig=6d39dcc4f6fc11eb5de6e2b457fc387d04dcb138 756w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-register-user.41a9c2bc4db9.png&amp;amp;w=1512&amp;amp;sig=454c3dfb7830bfefbd4fc267e92af61669936082 1512w, https://files.realpython.com/media/discord-bot-register-user.41a9c2bc4db9.png 3024w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Account Login Screen&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6265 &lt;p&gt;If you need to create a new account, then click on the &lt;em&gt;Register&lt;/em&gt; button below &lt;em&gt;Login&lt;/em&gt; and enter your account information.&lt;/p&gt;
6266 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
6267 &lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; You&amp;rsquo;ll need to verify your email before you&amp;rsquo;re able to move on.&lt;/p&gt;
6268 &lt;/div&gt;
6269 &lt;p&gt;Once you&amp;rsquo;re finished, you&amp;rsquo;ll be redirected to the Developer Portal home page, where you&amp;rsquo;ll create your application.&lt;/p&gt;
6270 &lt;h3 id=&quot;creating-an-application&quot;&gt;Creating an Application&lt;/h3&gt;
6271 &lt;p&gt;An &lt;strong&gt;application&lt;/strong&gt; allows you to interact with Discord&amp;rsquo;s APIs by  providing authentication tokens, designating permissions, and so on.&lt;/p&gt;
6272 &lt;p&gt;To create a new application, select &lt;em&gt;New Application&lt;/em&gt;:&lt;/p&gt;
6273 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-new-app.40b4a51bb57d.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-new-app.40b4a51bb57d.png&quot; width=&quot;3024&quot; height=&quot;1765&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-new-app.40b4a51bb57d.png&amp;amp;w=756&amp;amp;sig=094b6a170204052aa3e03749c858002b90ccb1bd 756w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-new-app.40b4a51bb57d.png&amp;amp;w=1512&amp;amp;sig=fb20c2f10526eafc64138f33c8e8f08e5c895d57 1512w, https://files.realpython.com/media/discord-bot-new-app.40b4a51bb57d.png 3024w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: My Applications Screen&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6274 &lt;p&gt;Next, you&amp;rsquo;ll be prompted to name your application. Select a name and click &lt;em&gt;Create&lt;/em&gt;:&lt;/p&gt;
6275 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-name-application.8ccfc8a69cb5.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-name-application.8ccfc8a69cb5.png&quot; width=&quot;3024&quot; height=&quot;1771&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-name-application.8ccfc8a69cb5.png&amp;amp;w=756&amp;amp;sig=663b4e94bcf90cb6c34d416805e4a488dfea3a36 756w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-name-application.8ccfc8a69cb5.png&amp;amp;w=1512&amp;amp;sig=46630dceba46441199dfd02f62565bc7be1ca896 1512w, https://files.realpython.com/media/discord-bot-name-application.8ccfc8a69cb5.png 3024w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Naming an Application&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6276 &lt;p&gt;Congratulations! You made a Discord application. On the resulting screen, you can see information about your application:&lt;/p&gt;
6277 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-app-info.146a24d590a6.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-app-info.146a24d590a6.png&quot; width=&quot;3015&quot; height=&quot;1767&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-app-info.146a24d590a6.png&amp;amp;w=753&amp;amp;sig=08d7b87d0cdda73c1ce9f040123d5fbe6b3ecc76 753w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-app-info.146a24d590a6.png&amp;amp;w=1507&amp;amp;sig=8d9fdeb253e6d562be88406244262b8797e50525 1507w, https://files.realpython.com/media/discord-bot-app-info.146a24d590a6.png 3015w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Application General Information&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6278 &lt;p&gt;Keep in mind that any program that interacts with Discord APIs requires a Discord application, not just bots. Bot-related APIs are only a subset of Discord&amp;rsquo;s total interface.&lt;/p&gt;
6279 &lt;p&gt;However, since this tutorial is about how to make a Discord bot, navigate to the &lt;em&gt;Bot&lt;/em&gt; tab on the left-hand navigation list.&lt;/p&gt;
6280 &lt;h3 id=&quot;creating-a-bot&quot;&gt;Creating a Bot&lt;/h3&gt;
6281 &lt;p&gt;As you learned in the previous sections, a bot user is one that listens to and automatically reacts to certain events and commands on Discord.&lt;/p&gt;
6282 &lt;p&gt;For your code to actually be manifested on Discord, you&amp;rsquo;ll need to create a bot user. To do so, select &lt;em&gt;Add Bot&lt;/em&gt;:&lt;/p&gt;
6283 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-add-bot.4735c88ff16b.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-add-bot.4735c88ff16b.png&quot; width=&quot;3021&quot; height=&quot;1761&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-add-bot.4735c88ff16b.png&amp;amp;w=755&amp;amp;sig=1bd131ee29f9c52aed1698d09c26f4267abb5f02 755w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-add-bot.4735c88ff16b.png&amp;amp;w=1510&amp;amp;sig=3b9afe030a68a934e1f3730ebab7c9bd7e8981cf 1510w, https://files.realpython.com/media/discord-bot-add-bot.4735c88ff16b.png 3021w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Add Bot&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6284 &lt;p&gt;Once you confirm that you want to add the bot to your application, you&amp;rsquo;ll see the new bot user in the portal:&lt;/p&gt;
6285 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-created.fbdf4a021810.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-created.fbdf4a021810.png&quot; width=&quot;3009&quot; height=&quot;1760&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-created.fbdf4a021810.png&amp;amp;w=752&amp;amp;sig=8d9dc28ff753dff981137aef1b7e5add00a429a1 752w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-created.fbdf4a021810.png&amp;amp;w=1504&amp;amp;sig=2ec8e17f1a229eb212a96d7f85c9b680391776f1 1504w, https://files.realpython.com/media/discord-bot-created.fbdf4a021810.png 3009w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Bot Created Successfully&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6286 &lt;p&gt;Notice that, by default, your bot user will inherit the name of your application. Instead, update the username to something more bot-like, such as &lt;code&gt;RealPythonTutorialBot&lt;/code&gt;, and &lt;em&gt;Save Changes&lt;/em&gt;:&lt;/p&gt;
6287 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-rename-bot.008fd6ed6354.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-rename-bot.008fd6ed6354.png&quot; width=&quot;3023&quot; height=&quot;1770&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-rename-bot.008fd6ed6354.png&amp;amp;w=755&amp;amp;sig=39e7af71df9ef2095bce4d7e964973af010903c0 755w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-rename-bot.008fd6ed6354.png&amp;amp;w=1511&amp;amp;sig=252ca9f5542ba888771dd8aa32cf36b65cbc6317 1511w, https://files.realpython.com/media/discord-bot-rename-bot.008fd6ed6354.png 3023w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Rename Bot&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6288 &lt;p&gt;Now, the bot&amp;rsquo;s all set and ready to go, but to where?&lt;/p&gt;
6289 &lt;p&gt;A bot user is not useful if it&amp;rsquo;s not interacting with other users. Next, you&amp;rsquo;ll create a guild so that your bot can interact with other users.&lt;/p&gt;
6290 &lt;h3 id=&quot;creating-a-guild&quot;&gt;Creating a Guild&lt;/h3&gt;
6291 &lt;p&gt;A &lt;strong&gt;guild&lt;/strong&gt; (or a &lt;strong&gt;server&lt;/strong&gt;, as it is often called in Discord&amp;rsquo;s user interface) is a specific group of channels where users congregate to chat.&lt;/p&gt;
6292 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
6293 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; While &lt;strong&gt;guild&lt;/strong&gt; and &lt;strong&gt;server&lt;/strong&gt; are interchangeable, this article will use the term &lt;strong&gt;guild&lt;/strong&gt; primarily because the APIs stick to the same term. The term &lt;strong&gt;server&lt;/strong&gt; will only be used when referring to a guild in the graphical UI.&lt;/p&gt;
6294 &lt;/div&gt;
6295 &lt;p&gt;For example, say you want to create a space where users can come together and talk about your latest game. You&amp;rsquo;d start by creating a guild. Then, in your guild, you could have multiple channels, such as:&lt;/p&gt;
6296 &lt;ul&gt;
6297 &lt;li&gt;&lt;strong&gt;General Discussion:&lt;/strong&gt; A channel for users to talk about whatever they want&lt;/li&gt;
6298 &lt;li&gt;&lt;strong&gt;Spoilers, Beware:&lt;/strong&gt; A channel for users who have finished your game to talk about all the end game reveals&lt;/li&gt;
6299 &lt;li&gt;&lt;strong&gt;Announcements:&lt;/strong&gt; A channel for you to announce game updates and for users to discuss them&lt;/li&gt;
6300 &lt;/ul&gt;
6301 &lt;p&gt;Once you&amp;rsquo;ve created your guild, you&amp;rsquo;d invite other users to populate it.&lt;/p&gt;
6302 &lt;p&gt;So, to create a guild, head to your Discord &lt;a href=&quot;https://discordapp.com/channels/@me&quot;&gt;home&lt;/a&gt; page:&lt;/p&gt;
6303 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-homepage.f533b989cedd.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-homepage.f533b989cedd.png&quot; width=&quot;3028&quot; height=&quot;1717&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-homepage.f533b989cedd.png&amp;amp;w=757&amp;amp;sig=34788068aad51ef7668a3e5ff0583199519ec3c9 757w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-homepage.f533b989cedd.png&amp;amp;w=1514&amp;amp;sig=e36d4b45395b91c00d546eee352c80cee2f8165a 1514w, https://files.realpython.com/media/discord-bot-homepage.f533b989cedd.png 3028w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: User Account Home Page&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6304 &lt;p&gt;From this home page, you can view and add friends, direct messages, and guilds. From here, select the &lt;em&gt;+&lt;/em&gt; icon on the left-hand side of the web page to &lt;em&gt;Add a Server&lt;/em&gt;:&lt;/p&gt;
6305 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-add-server.bd5a5a58c50c.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-add-server.bd5a5a58c50c.png&quot; width=&quot;3027&quot; height=&quot;1721&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-add-server.bd5a5a58c50c.png&amp;amp;w=756&amp;amp;sig=1113434567a2bedb5b26726cce843adc7e5791d6 756w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-add-server.bd5a5a58c50c.png&amp;amp;w=1513&amp;amp;sig=5cb289f097eac68358ee2400d26e45ee9e377fd6 1513w, https://files.realpython.com/media/discord-bot-add-server.bd5a5a58c50c.png 3027w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Add Server&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6306 &lt;p&gt;This will present two options, &lt;em&gt;Create a server&lt;/em&gt; and &lt;em&gt;Join a Server&lt;/em&gt;. In this case, select &lt;em&gt;Create a server&lt;/em&gt; and enter a name for your guild:&lt;/p&gt;
6307 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-create-server.922dba753792.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-create-server.922dba753792.png&quot; width=&quot;3023&quot; height=&quot;1716&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-create-server.922dba753792.png&amp;amp;w=755&amp;amp;sig=e5083d753497fc156dfec6c3b351abd790b8c0d8 755w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-create-server.922dba753792.png&amp;amp;w=1511&amp;amp;sig=4709fdcaa3feede390c444431adbb70f998f3f04 1511w, https://files.realpython.com/media/discord-bot-create-server.922dba753792.png 3023w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Naming a Server&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6308 &lt;p&gt;Once you&amp;rsquo;ve finished creating your guild, you&amp;rsquo;ll be able to see the users on the right-hand side and the channels on the left:&lt;/p&gt;
6309 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-server.cba61f3781cf.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-server.cba61f3781cf.png&quot; width=&quot;3026&quot; height=&quot;1721&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-server.cba61f3781cf.png&amp;amp;w=756&amp;amp;sig=b8d05142625b9e2ce814747d1cf64921eba8e048 756w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-server.cba61f3781cf.png&amp;amp;w=1513&amp;amp;sig=6b6139eeecc4f1f64523d0a4d96f69e1a623c558 1513w, https://files.realpython.com/media/discord-bot-server.cba61f3781cf.png 3026w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Newly Created Server&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6310 &lt;p&gt;The final step on Discord is to register your bot with your new guild.&lt;/p&gt;
6311 &lt;h3 id=&quot;adding-a-bot-to-a-guild&quot;&gt;Adding a Bot to a Guild&lt;/h3&gt;
6312 &lt;p&gt;A bot can&amp;rsquo;t accept invites like a normal user can. Instead, you&amp;rsquo;ll add your bot using the OAuth2 protocol.&lt;/p&gt;
6313 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
6314 &lt;p&gt;&lt;strong&gt;Technical Detail:&lt;/strong&gt; &lt;a href=&quot;https://oauth.net/2/&quot;&gt;OAuth2&lt;/a&gt; is a protocol for dealing with authorization, where a service can grant a client application limited access based on the application&amp;rsquo;s credentials and allowed scopes.&lt;/p&gt;
6315 &lt;/div&gt;
6316 &lt;p&gt;To do so, head back to the &lt;a href=&quot;http://discordapp.com/developers/applications&quot;&gt;Developer Portal&lt;/a&gt; and select the OAuth2 page from the left-hand navigation:&lt;/p&gt;
6317 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-oauth2.7c000bfe571b.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-oauth2.7c000bfe571b.png&quot; width=&quot;3008&quot; height=&quot;1770&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-oauth2.7c000bfe571b.png&amp;amp;w=752&amp;amp;sig=a7d51e04cbb851359c7fe643d80acb8e1674d8a2 752w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-oauth2.7c000bfe571b.png&amp;amp;w=1504&amp;amp;sig=5ba25b1a754b3d08bb200328f56ea71e8b71db81 1504w, https://files.realpython.com/media/discord-bot-oauth2.7c000bfe571b.png 3008w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Application OAuth2&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6318 &lt;p&gt;From this window, you&amp;rsquo;ll see the OAuth2 URL Generator.&lt;/p&gt;
6319 &lt;p&gt;This tool generates an authorization URL that hits Discord&amp;rsquo;s OAuth2 API and authorizes API access using your application&amp;rsquo;s credentials.&lt;/p&gt;
6320 &lt;p&gt;In this case, you&amp;rsquo;ll want to grant your application&amp;rsquo;s bot user access to Discord APIs using your application&amp;rsquo;s OAuth2 credentials.&lt;/p&gt;
6321 &lt;p&gt;To do this, scroll down and select &lt;em&gt;bot&lt;/em&gt; from the &lt;em&gt;SCOPES&lt;/em&gt; options and &lt;em&gt;Administrator&lt;/em&gt; from &lt;em&gt;BOT PERMISSIONS&lt;/em&gt;:&lt;/p&gt;
6322 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-scopes.ee333b7a5987.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-scopes.ee333b7a5987.png&quot; width=&quot;3012&quot; height=&quot;1766&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-scopes.ee333b7a5987.png&amp;amp;w=753&amp;amp;sig=3df33d0353cab7ddbf213779e83c711e5d629e21 753w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-scopes.ee333b7a5987.png&amp;amp;w=1506&amp;amp;sig=259c0a008e687ca6bcb131a63d825e4a3f5133c5 1506w, https://files.realpython.com/media/discord-bot-scopes.ee333b7a5987.png 3012w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Application Scopes and Bot Permissions&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6323 &lt;p&gt;Now, Discord has generated your application&amp;rsquo;s authorization URL with the selected scope and permissions.&lt;/p&gt;
6324 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
6325 &lt;p&gt;&lt;strong&gt;Disclaimer:&lt;/strong&gt; While we&amp;rsquo;re using &lt;em&gt;Administrator&lt;/em&gt; for the purposes of this tutorial, you should be as granular as possible when granting permissions in a real-world application.&lt;/p&gt;
6326 &lt;/div&gt;
6327 &lt;p&gt;Select &lt;em&gt;Copy&lt;/em&gt; beside the URL that was generated for you, paste it into your browser, and select your guild from the dropdown options:&lt;/p&gt;
6328 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-select-server.3cd1af626256.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-select-server.3cd1af626256.png&quot; width=&quot;3023&quot; height=&quot;1715&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-select-server.3cd1af626256.png&amp;amp;w=755&amp;amp;sig=bfa0dba079ebee97013871c01220f66505eed020 755w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-select-server.3cd1af626256.png&amp;amp;w=1511&amp;amp;sig=f2e91e59e77705a053b9930c27d1bf198ff6c7dc 1511w, https://files.realpython.com/media/discord-bot-select-server.3cd1af626256.png 3023w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Add Bot to a Server&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6329 &lt;p&gt;Click &lt;em&gt;Authorize&lt;/em&gt;, and you&amp;rsquo;re done!&lt;/p&gt;
6330 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
6331 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You might get a &lt;a href=&quot;https://en.wikipedia.org/wiki/ReCAPTCHA&quot;&gt;reCAPTCHA&lt;/a&gt; before moving on. If so, you&amp;rsquo;ll need to prove you&amp;rsquo;re a human.&lt;/p&gt;
6332 &lt;/div&gt;
6333 &lt;p&gt;If you go back to your guild, then you&amp;rsquo;ll see that the bot has been added:&lt;/p&gt;
6334 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-added-to-guild.4a6b4477bc1e.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-added-to-guild.4a6b4477bc1e.png&quot; width=&quot;3024&quot; height=&quot;1719&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-added-to-guild.4a6b4477bc1e.png&amp;amp;w=756&amp;amp;sig=39634e4118660e59511dee28f86e7bb290192f3d 756w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-added-to-guild.4a6b4477bc1e.png&amp;amp;w=1512&amp;amp;sig=add9851baae9338210082db661a6277cad86ef5f 1512w, https://files.realpython.com/media/discord-bot-added-to-guild.4a6b4477bc1e.png 3024w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Bot Added to Guild&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6335 &lt;p&gt;In summary, you&amp;rsquo;ve created:&lt;/p&gt;
6336 &lt;ul&gt;
6337 &lt;li&gt;An &lt;strong&gt;application&lt;/strong&gt; that your bot will use to authenticate with Discord&amp;rsquo;s APIs&lt;/li&gt;
6338 &lt;li&gt;A &lt;strong&gt;bot&lt;/strong&gt; user that you&amp;rsquo;ll use to interact with other users and events in your guild&lt;/li&gt;
6339 &lt;li&gt;A &lt;strong&gt;guild&lt;/strong&gt; in which your user account and your bot user will be active&lt;/li&gt;
6340 &lt;li&gt;A &lt;strong&gt;Discord&lt;/strong&gt; account with which you created everything else and that you&amp;rsquo;ll use to interact with your bot&lt;/li&gt;
6341 &lt;/ul&gt;
6342 &lt;p&gt;Now, you know how to make a Discord bot using the Developer Portal. Next comes the fun stuff: implementing your bot in Python!&lt;/p&gt;
6343 &lt;h2 id=&quot;how-to-make-a-discord-bot-in-python&quot;&gt;How to Make a Discord Bot in Python&lt;/h2&gt;
6344 &lt;p&gt;Since you&amp;rsquo;re learning how to make a Discord bot with Python, you&amp;rsquo;ll be using &lt;code&gt;discord.py&lt;/code&gt;.&lt;/p&gt;
6345 &lt;p&gt;&lt;a href=&quot;https://discordpy.readthedocs.io/en/latest/index.html&quot;&gt;&lt;code&gt;discord.py&lt;/code&gt;&lt;/a&gt; is a Python library that exhaustively implements Discord&amp;rsquo;s APIs in an efficient and Pythonic way. This includes utilizing Python&amp;rsquo;s implementation of &lt;a href=&quot;https://realpython.com/async-io-python/&quot;&gt;Async IO&lt;/a&gt;.&lt;/p&gt;
6346 &lt;p&gt;Begin by installing &lt;code&gt;discord.py&lt;/code&gt; with &lt;a href=&quot;https://realpython.com/what-is-pip/&quot;&gt;&lt;code&gt;pip&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
6347 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pip install -U discord.py
6348 &lt;/pre&gt;&lt;/div&gt;
6349 
6350 &lt;p&gt;Now that you&amp;rsquo;ve installed &lt;code&gt;discord.py&lt;/code&gt;, you&amp;rsquo;ll use it to create your first connection to Discord!&lt;/p&gt;
6351 &lt;h2 id=&quot;creating-a-discord-connection&quot;&gt;Creating a Discord Connection&lt;/h2&gt;
6352 &lt;p&gt;The first step in implementing your bot user is to create a connection to Discord. With &lt;code&gt;discord.py&lt;/code&gt;, you do this by creating an instance of &lt;code&gt;Client&lt;/code&gt;:&lt;/p&gt;
6353 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# bot.py&lt;/span&gt;
6354 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
6355 
6356 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;discord&lt;/span&gt;
6357 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dotenv&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;
6358 
6359 &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6360 &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_TOKEN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6361 
6362 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;discord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6363 
6364 &lt;span class=&quot;nd&quot;&gt;@client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
6365 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_ready&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
6366     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{client.user}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; has connected to Discord!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6367 
6368 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6369 &lt;/pre&gt;&lt;/div&gt;
6370 
6371 &lt;p&gt;A &lt;code&gt;Client&lt;/code&gt; is an object that represents a connection to Discord. A &lt;code&gt;Client&lt;/code&gt; handles events, tracks state, and generally interacts with Discord APIs.&lt;/p&gt;
6372 &lt;p&gt;Here, you&amp;rsquo;ve created a &lt;code&gt;Client&lt;/code&gt; and implemented its &lt;code&gt;on_ready()&lt;/code&gt; event handler, which handles the event when the &lt;code&gt;Client&lt;/code&gt; has established a connection to Discord and it has finished preparing the data that Discord has sent, such as login state, guild and channel data, and more.&lt;/p&gt;
6373 &lt;p&gt;In other words, &lt;code&gt;on_ready()&lt;/code&gt; will be called (and your message will be printed) once &lt;code&gt;client&lt;/code&gt; is ready for further action. You&amp;rsquo;ll learn more about event handlers later in this article.&lt;/p&gt;
6374 &lt;p&gt;When you&amp;rsquo;re working with secrets such as your Discord token, it&amp;rsquo;s good practice to read it into your program from an environment variable. Using environment variables helps you:&lt;/p&gt;
6375 &lt;ul&gt;
6376 &lt;li&gt;Avoid putting the secrets into source control&lt;/li&gt;
6377 &lt;li&gt;Use different variables for development and production environments without changing your code&lt;/li&gt;
6378 &lt;/ul&gt;
6379 &lt;p&gt;While you could &lt;code&gt;export DISCORD_TOKEN={your-bot-token}&lt;/code&gt;, an easier solution is to save a &lt;code&gt;.env&lt;/code&gt; file on all machines that will be running this code. This is not only easier, since you won&amp;rsquo;t have to &lt;code&gt;export&lt;/code&gt; your token every time you clear your shell, but it also protects you from storing your secrets in your shell&amp;rsquo;s history.&lt;/p&gt;
6380 &lt;p&gt;Create a file named &lt;code&gt;.env&lt;/code&gt; in the same directory as &lt;code&gt;bot.py&lt;/code&gt;:&lt;/p&gt;
6381 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;# .env
6382 DISCORD_TOKEN={your-bot-token}
6383 &lt;/pre&gt;&lt;/div&gt;
6384 
6385 &lt;p&gt;You&amp;rsquo;ll need to replace &lt;code&gt;{your-bot-token}&lt;/code&gt; with your bot&amp;rsquo;s token, which you can get by going back to the &lt;em&gt;Bot&lt;/em&gt; page on the &lt;a href=&quot;http://discordapp.com/developers/applications&quot;&gt;Developer Portal&lt;/a&gt; and clicking &lt;em&gt;Copy&lt;/em&gt; under the &lt;em&gt;TOKEN&lt;/em&gt; section:&lt;/p&gt;
6386 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-copy-token.1228e6cb6cba.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-copy-token.1228e6cb6cba.png&quot; width=&quot;3024&quot; height=&quot;1767&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-copy-token.1228e6cb6cba.png&amp;amp;w=756&amp;amp;sig=57425662c2056deac50ebeb9a8d2891d4a8a6b53 756w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-copy-token.1228e6cb6cba.png&amp;amp;w=1512&amp;amp;sig=c8c437a86a1fb23a94e35050f5c969f5f20c4814 1512w, https://files.realpython.com/media/discord-bot-copy-token.1228e6cb6cba.png 3024w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Copy Bot Token&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6387 &lt;p&gt;Looking back at the &lt;code&gt;bot.py&lt;/code&gt; code, you&amp;rsquo;ll notice a library called &lt;a href=&quot;https://github.com/theskumar/python-dotenv&quot;&gt;&lt;code&gt;dotenv&lt;/code&gt;&lt;/a&gt;. This library is handy for working with &lt;code&gt;.env&lt;/code&gt; files. &lt;code&gt;load_dotenv()&lt;/code&gt; loads environment variables from a &lt;code&gt;.env&lt;/code&gt; file into your shell&amp;rsquo;s environment variables so that you can use them in your code.&lt;/p&gt;
6388 &lt;p&gt;Install &lt;code&gt;dotenv&lt;/code&gt; with &lt;code&gt;pip&lt;/code&gt;:&lt;/p&gt;
6389 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pip install -U python-dotenv
6390 &lt;/pre&gt;&lt;/div&gt;
6391 
6392 &lt;p&gt;Finally, &lt;code&gt;client.run()&lt;/code&gt; runs your &lt;code&gt;Client&lt;/code&gt; using your bot&amp;rsquo;s token.&lt;/p&gt;
6393 &lt;p&gt;Now that you&amp;rsquo;ve set up both &lt;code&gt;bot.py&lt;/code&gt; and &lt;code&gt;.env&lt;/code&gt;, you can run your code:&lt;/p&gt;
6394 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python bot.py
6395 &lt;span class=&quot;go&quot;&gt;RealPythonTutorialBot#9643 has connected to Discord!&lt;/span&gt;
6396 &lt;/pre&gt;&lt;/div&gt;
6397 
6398 &lt;p&gt;Great! Your &lt;code&gt;Client&lt;/code&gt; has connected to Discord using your bot&amp;rsquo;s token. In the next section, you&amp;rsquo;ll build on this &lt;code&gt;Client&lt;/code&gt; by interacting with more Discord APIs.&lt;/p&gt;
6399 &lt;h2 id=&quot;interacting-with-discord-apis&quot;&gt;Interacting With Discord APIs&lt;/h2&gt;
6400 &lt;p&gt;Using a &lt;code&gt;Client&lt;/code&gt;, you have access to a wide range of Discord APIs.&lt;/p&gt;
6401 &lt;p&gt;For example, let&amp;rsquo;s say you wanted to write the name and identifier of the guild that you registered your bot user with to the console.&lt;/p&gt;
6402 &lt;p&gt;First, you&amp;rsquo;ll need to add a new environment variable:&lt;/p&gt;
6403 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;# .env
6404 DISCORD_TOKEN={your-bot-token}
6405 &lt;span class=&quot;hll&quot;&gt;DISCORD_GUILD={your-guild-name}
6406 &lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
6407 
6408 &lt;p&gt;Don&amp;rsquo;t forget that you&amp;rsquo;ll need to replace the two placeholders with actual values:&lt;/p&gt;
6409 &lt;ol&gt;
6410 &lt;li&gt;&lt;code&gt;{your-bot-token}&lt;/code&gt;&lt;/li&gt;
6411 &lt;li&gt;&lt;code&gt;{your-guild-name}&lt;/code&gt;&lt;/li&gt;
6412 &lt;/ol&gt;
6413 &lt;p&gt;Remember that Discord calls &lt;code&gt;on_ready()&lt;/code&gt;, which you used before, once the &lt;code&gt;Client&lt;/code&gt; has made the connection and prepared the data. So, you can rely on the guild data being available inside &lt;code&gt;on_ready()&lt;/code&gt;:&lt;/p&gt;
6414 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# bot.py&lt;/span&gt;
6415 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
6416 
6417 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;discord&lt;/span&gt;
6418 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dotenv&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;
6419 
6420 &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6421 &lt;span class=&quot;n&quot;&gt;TOKEN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_TOKEN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6422 &lt;span class=&quot;n&quot;&gt;GUILD&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_GUILD&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6423 
6424 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;discord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6425 
6426 &lt;span class=&quot;nd&quot;&gt;@client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
6427 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_ready&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
6428     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;guild&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;guilds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
6429         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;guild&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GUILD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
6430             &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
6431 
6432     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
6433         &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{client.user}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; is connected to the following guild:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;
6434         &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{guild.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;(id: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{guild.id}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;)&amp;#39;&lt;/span&gt;
6435     &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6436 
6437 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TOKEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6438 &lt;/pre&gt;&lt;/div&gt;
6439 
6440 &lt;p&gt;Here, you looped through the guild data that Discord has sent &lt;code&gt;client&lt;/code&gt;, namely &lt;code&gt;client.guilds&lt;/code&gt;. Then, you found the guild with the matching name and printed a &lt;a href=&quot;https://realpython.com/python-f-strings/&quot;&gt;formatted string&lt;/a&gt; to &lt;code&gt;stdout&lt;/code&gt;.&lt;/p&gt;
6441 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
6442 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Even though you can be pretty confident at this point in the tutorial that your bot is only connected to a single guild (so &lt;code&gt;client.guilds[0]&lt;/code&gt; would be simpler), it&amp;rsquo;s important to realize that a bot user can be connected to many guilds.&lt;/p&gt;
6443 &lt;p&gt;Therefore, a more robust solution is to loop through &lt;code&gt;client.guilds&lt;/code&gt; to find the one you&amp;rsquo;re looking for.&lt;/p&gt;
6444 &lt;/div&gt;
6445 &lt;p&gt;Run the program to see the results:&lt;/p&gt;
6446 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python bot.py
6447 &lt;span class=&quot;go&quot;&gt;RealPythonTutorialBot#9643 is connected to the following guild:&lt;/span&gt;
6448 &lt;span class=&quot;go&quot;&gt;RealPythonTutorialServer(id: 571759877328732195)&lt;/span&gt;
6449 &lt;/pre&gt;&lt;/div&gt;
6450 
6451 &lt;p&gt;Great! You can see the name of your bot, the name of your server, and the server&amp;rsquo;s identification number.&lt;/p&gt;
6452 &lt;p&gt;Another interesting bit of data you can pull from a guild is the list of users who are members of the guild:&lt;/p&gt;
6453 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# bot.py&lt;/span&gt;
6454 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
6455 
6456 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;discord&lt;/span&gt;
6457 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dotenv&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;
6458 
6459 &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6460 &lt;span class=&quot;n&quot;&gt;TOKEN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_TOKEN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6461 &lt;span class=&quot;n&quot;&gt;GUILD&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_GUILD&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6462 
6463 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;discord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6464 
6465 &lt;span class=&quot;nd&quot;&gt;@client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
6466 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_ready&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
6467     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;guild&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;guilds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
6468         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;guild&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GUILD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
6469             &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
6470 
6471     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
6472         &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{client.user}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; is connected to the following guild:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;
6473         &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{guild.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;(id: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{guild.id}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;
6474     &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6475 
6476     &lt;span class=&quot;n&quot;&gt;members&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; - &amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;member&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;member&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;guild&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;members&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
6477     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Guild Members:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{members}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6478 
6479 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TOKEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6480 &lt;/pre&gt;&lt;/div&gt;
6481 
6482 &lt;p&gt;By looping through &lt;code&gt;guild.members&lt;/code&gt;, you pulled the names of all of the members of the guild and printed them with a formatted string.&lt;/p&gt;
6483 &lt;p&gt;When you run the program, you should see at least the name of the account you created the guild with and the name of the bot user itself:&lt;/p&gt;
6484 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python bot.py
6485 &lt;span class=&quot;go&quot;&gt;RealPythonTutorialBot#9643 is connected to the following guild:&lt;/span&gt;
6486 &lt;span class=&quot;go&quot;&gt;RealPythonTutorialServer(id: 571759877328732195)&lt;/span&gt;
6487 
6488 &lt;span class=&quot;go&quot;&gt;Guild Members:&lt;/span&gt;
6489 &lt;span class=&quot;go&quot;&gt; - aronq2&lt;/span&gt;
6490 &lt;span class=&quot;go&quot;&gt; - RealPythonTutorialBot&lt;/span&gt;
6491 &lt;/pre&gt;&lt;/div&gt;
6492 
6493 &lt;p&gt;These examples barely scratch the surface of the APIs available on Discord, be sure to check out their &lt;a href=&quot;https://discordpy.readthedocs.io/en/latest/api.html#&quot;&gt;documentation&lt;/a&gt; to see all that they have to offer.&lt;/p&gt;
6494 &lt;p&gt;Next, you&amp;rsquo;ll learn about some utility functions and how they can simplify these examples.&lt;/p&gt;
6495 &lt;h2 id=&quot;using-utility-functions&quot;&gt;Using Utility Functions&lt;/h2&gt;
6496 &lt;p&gt;Let&amp;rsquo;s take another look at the example from the last section where you printed the name and identifier of the bot&amp;rsquo;s guild:&lt;/p&gt;
6497 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# bot.py&lt;/span&gt;
6498 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
6499 
6500 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;discord&lt;/span&gt;
6501 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dotenv&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;
6502 
6503 &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6504 &lt;span class=&quot;n&quot;&gt;TOKEN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_TOKEN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6505 &lt;span class=&quot;n&quot;&gt;GUILD&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_GUILD&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6506 
6507 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;discord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6508 
6509 &lt;span class=&quot;nd&quot;&gt;@client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
6510 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_ready&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
6511     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;guild&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;guilds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
6512         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;guild&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GUILD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
6513             &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
6514 
6515     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
6516         &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{client.user}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; is connected to the following guild:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;
6517         &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{guild.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;(id: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{guild.id}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;)&amp;#39;&lt;/span&gt;
6518     &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6519 
6520 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TOKEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6521 &lt;/pre&gt;&lt;/div&gt;
6522 
6523 &lt;p&gt;You could clean up this code by using some of the utility functions available in &lt;code&gt;discord.py&lt;/code&gt;.&lt;/p&gt;
6524 &lt;p&gt;&lt;a href=&quot;https://discordpy.readthedocs.io/en/latest/api.html#discord.utils.find&quot;&gt;&lt;code&gt;discord.utils.find()&lt;/code&gt;&lt;/a&gt; is one utility that can improve the simplicity and readability of this code by replacing the &lt;code&gt;for&lt;/code&gt; loop with an intuitive, abstracted function:&lt;/p&gt;
6525 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# bot.py&lt;/span&gt;
6526 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
6527 
6528 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;discord&lt;/span&gt;
6529 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dotenv&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;
6530 
6531 &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6532 &lt;span class=&quot;n&quot;&gt;TOKEN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_TOKEN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6533 &lt;span class=&quot;n&quot;&gt;GUILD&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_GUILD&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6534 
6535 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;discord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6536 
6537 &lt;span class=&quot;nd&quot;&gt;@client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
6538 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_ready&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
6539     &lt;span class=&quot;n&quot;&gt;guild&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;discord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;utils&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GUILD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;guilds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6540     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
6541         &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{client.user}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; is connected to the following guild:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;
6542         &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{guild.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;(id: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{guild.id}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;)&amp;#39;&lt;/span&gt;
6543     &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6544 
6545 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TOKEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6546 &lt;/pre&gt;&lt;/div&gt;
6547 
6548 &lt;p&gt;&lt;code&gt;find()&lt;/code&gt; takes a function, called a &lt;strong&gt;predicate&lt;/strong&gt;, which identifies some characteristic of the element in the iterable that you&amp;rsquo;re looking for. Here, you used a particular type of anonymous function, called a &lt;a href=&quot;https://realpython.com/python-lambda/&quot;&gt;lambda&lt;/a&gt;, as the predicate.&lt;/p&gt;
6549 &lt;p&gt;In this case, you&amp;rsquo;re trying to find the guild with the same name as the one you stored in the &lt;code&gt;DISCORD_GUILD&lt;/code&gt; environment variable. Once &lt;code&gt;find()&lt;/code&gt; locates an element in the iterable that satisfies the predicate, it will return the element. This is essentially equivalent to the &lt;code&gt;break&lt;/code&gt; statement in the previous example, but cleaner.&lt;/p&gt;
6550 &lt;p&gt;&lt;code&gt;discord.py&lt;/code&gt; has even abstracted this concept one step further with the &lt;a href=&quot;https://discordpy.readthedocs.io/en/latest/api.html#discord.utils.get&quot;&gt;&lt;code&gt;get()&lt;/code&gt; utility&lt;/a&gt;:&lt;/p&gt;
6551 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# bot.py&lt;/span&gt;
6552 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
6553 
6554 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;discord&lt;/span&gt;
6555 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dotenv&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;
6556 
6557 &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6558 &lt;span class=&quot;n&quot;&gt;TOKEN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_TOKEN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6559 &lt;span class=&quot;n&quot;&gt;GUILD&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_GUILD&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6560 
6561 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;discord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6562 
6563 &lt;span class=&quot;nd&quot;&gt;@client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
6564 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_ready&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
6565     &lt;span class=&quot;n&quot;&gt;guild&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;discord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;utils&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;guilds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GUILD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6566     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
6567         &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{client.user}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; is connected to the following guild:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;
6568         &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{guild.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;(id: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{guild.id}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;)&amp;#39;&lt;/span&gt;
6569     &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6570 
6571 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TOKEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6572 &lt;/pre&gt;&lt;/div&gt;
6573 
6574 &lt;p&gt;&lt;code&gt;get()&lt;/code&gt; takes the iterable and some keyword arguments. The keyword arguments represent attributes of the elements in the iterable that must all be satisfied for &lt;code&gt;get()&lt;/code&gt; to return the element.&lt;/p&gt;
6575 &lt;p&gt;In this example, you&amp;rsquo;ve identified &lt;code&gt;name=GUILD&lt;/code&gt; as the attribute that must be satisfied.&lt;/p&gt;
6576 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
6577 &lt;p&gt;&lt;strong&gt;Technical Detail:&lt;/strong&gt; Under the hood, &lt;code&gt;get()&lt;/code&gt; actually uses the &lt;code&gt;attrs&lt;/code&gt; keyword arguments to build a predicate, which it then uses to call &lt;code&gt;find()&lt;/code&gt;.&lt;/p&gt;
6578 &lt;/div&gt;
6579 &lt;p&gt;Now that you&amp;rsquo;ve learned the basics of interacting with APIs, you&amp;rsquo;ll dive a little deeper into the function that you&amp;rsquo;ve been using to access them: &lt;code&gt;on_ready()&lt;/code&gt;.&lt;/p&gt;
6580 &lt;h2 id=&quot;responding-to-events&quot;&gt;Responding to Events&lt;/h2&gt;
6581 &lt;p&gt;You already learned that &lt;code&gt;on_ready()&lt;/code&gt; is an event. In fact, you might have noticed that it is identified as such in the code by the &lt;code&gt;client.event&lt;/code&gt; &lt;a href=&quot;https://realpython.com/primer-on-python-decorators/&quot;&gt;decorator&lt;/a&gt;.&lt;/p&gt;
6582 &lt;p&gt;But what is an event?&lt;/p&gt;
6583 &lt;p&gt;An &lt;strong&gt;event&lt;/strong&gt; is something that happens on Discord that you can use to trigger a reaction in your code. Your code will listen for and then respond to events.&lt;/p&gt;
6584 &lt;p&gt;Using the example you&amp;rsquo;ve seen already, the &lt;code&gt;on_ready()&lt;/code&gt; event handler handles the event that the &lt;code&gt;Client&lt;/code&gt; has made a connection to Discord and prepared its response data.&lt;/p&gt;
6585 &lt;p&gt;So, when Discord fires an event, &lt;code&gt;discord.py&lt;/code&gt; will route the event data to the corresponding event handler on your connected &lt;code&gt;Client&lt;/code&gt;.&lt;/p&gt;
6586 &lt;p&gt;There are two ways in &lt;code&gt;discord.py&lt;/code&gt; to implement an event handler:&lt;/p&gt;
6587 &lt;ol&gt;
6588 &lt;li&gt;Using the &lt;code&gt;client.event&lt;/code&gt; decorator&lt;/li&gt;
6589 &lt;li&gt;Creating a subclass of &lt;code&gt;Client&lt;/code&gt; and overriding its handler methods&lt;/li&gt;
6590 &lt;/ol&gt;
6591 &lt;p&gt;You already saw the implementation using the decorator. Next, take a look at how to subclass &lt;code&gt;Client&lt;/code&gt;:&lt;/p&gt;
6592 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# bot.py&lt;/span&gt;
6593 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
6594 
6595 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;discord&lt;/span&gt;
6596 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dotenv&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;
6597 
6598 &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6599 &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_TOKEN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6600 
6601 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CustomClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;discord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
6602     &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_ready&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
6603         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.user}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; has connected to Discord!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6604 
6605 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CustomClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6606 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6607 &lt;/pre&gt;&lt;/div&gt;
6608 
6609 &lt;p&gt;Here, just like before, you&amp;rsquo;ve created a &lt;code&gt;client&lt;/code&gt; variable and called &lt;code&gt;.run()&lt;/code&gt; with your Discord token. The actual &lt;code&gt;Client&lt;/code&gt; is different, however. Instead of using the normal base class, &lt;code&gt;client&lt;/code&gt; is an instance of &lt;code&gt;CustomClient&lt;/code&gt;, which has an overridden &lt;code&gt;on_ready()&lt;/code&gt; function.&lt;/p&gt;
6610 &lt;p&gt;There is no difference between the two implementation styles of events, but this tutorial will primarily use the decorator version because it looks similar to how you implement &lt;code&gt;Bot&lt;/code&gt; commands, which is a topic you&amp;rsquo;ll cover in a bit.&lt;/p&gt;
6611 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
6612 &lt;p&gt;&lt;strong&gt;Technical Detail:&lt;/strong&gt; Regardless of how you implement your event handler, one thing must be consistent: all event handlers in &lt;code&gt;discord.py&lt;/code&gt; must be &lt;a href=&quot;https://realpython.com/async-io-python/#the-asyncawait-syntax-and-native-coroutines&quot;&gt;coroutines&lt;/a&gt;.&lt;/p&gt;
6613 &lt;/div&gt;
6614 &lt;p&gt;Now that you&amp;rsquo;ve learned how to create an event handler, let&amp;rsquo;s walk through some different examples of handlers you can create.&lt;/p&gt;
6615 &lt;h3 id=&quot;welcoming-new-members&quot;&gt;Welcoming New Members&lt;/h3&gt;
6616 &lt;p&gt;Previously, you saw the example of responding to the event where a member joins a guild. In that example, your bot user could send them a message, welcoming them to your Discord community.&lt;/p&gt;
6617 &lt;p&gt;Now, you&amp;rsquo;ll implement that behavior in your &lt;code&gt;Client&lt;/code&gt;, using event handlers, and verify its behavior in Discord:&lt;/p&gt;
6618 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# bot.py&lt;/span&gt;
6619 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
6620 
6621 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;discord&lt;/span&gt;
6622 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dotenv&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;
6623 
6624 &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6625 &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_TOKEN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6626 
6627 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;discord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6628 
6629 &lt;span class=&quot;nd&quot;&gt;@client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
6630 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_ready&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
6631     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{client.user.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; has connected to Discord!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6632 
6633 &lt;span class=&quot;nd&quot;&gt;@client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
6634 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_member_join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;member&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
6635     &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;member&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_dm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6636     &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;member&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dm_channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
6637         &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Hi &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{member.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;, welcome to my Discord server!&amp;#39;&lt;/span&gt;
6638     &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6639 
6640 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6641 &lt;/pre&gt;&lt;/div&gt;
6642 
6643 &lt;p&gt;Like before, you handled the &lt;code&gt;on_ready()&lt;/code&gt; event by printing the bot user&amp;rsquo;s name in a formatted string. New, however, is the implementation of the &lt;code&gt;on_member_join()&lt;/code&gt; event handler.&lt;/p&gt;
6644 &lt;p&gt;&lt;code&gt;on_member_join()&lt;/code&gt;, as its name suggests, handles the event of a new member joining a guild.&lt;/p&gt;
6645 &lt;p&gt;In this example, you used &lt;code&gt;member.create_dm()&lt;/code&gt; to create a direct message channel. Then, you used that channel to &lt;code&gt;.send()&lt;/code&gt; a direct message to that new member.&lt;/p&gt;
6646 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
6647 &lt;p&gt;&lt;strong&gt;Technical Detail:&lt;/strong&gt; Notice the &lt;code&gt;await&lt;/code&gt; keyword before &lt;code&gt;member.create_dm()&lt;/code&gt; and &lt;code&gt;member.dm_channel.send()&lt;/code&gt;.&lt;/p&gt;
6648 &lt;p&gt;&lt;code&gt;await&lt;/code&gt; suspends the execution of the surrounding coroutine until the execution of each coroutine has finished.&lt;/p&gt;
6649 &lt;/div&gt;
6650 &lt;p&gt;Now, let&amp;rsquo;s test out your bot&amp;rsquo;s new behavior.&lt;/p&gt;
6651 &lt;p&gt;First, run your new version of &lt;code&gt;bot.py&lt;/code&gt; and wait for the &lt;code&gt;on_ready()&lt;/code&gt; event to fire, logging your message to &lt;code&gt;stdout&lt;/code&gt;:&lt;/p&gt;
6652 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python bot.py
6653 &lt;span class=&quot;go&quot;&gt;RealPythonTutorialBot has connected to Discord!&lt;/span&gt;
6654 &lt;/pre&gt;&lt;/div&gt;
6655 
6656 &lt;p&gt;Now, head over to &lt;a href=&quot;https://discordapp.com/&quot;&gt;Discord&lt;/a&gt;, log in, and navigate to your guild by selecting it from the left-hand side of the screen:&lt;/p&gt;
6657 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-navigate-to-server.dfef0364630f.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-navigate-to-server.dfef0364630f.png&quot; width=&quot;3024&quot; height=&quot;1767&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-navigate-to-server.dfef0364630f.png&amp;amp;w=756&amp;amp;sig=e385f92a8203695f50bbd2d55922fc8ae3b4599e 756w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-navigate-to-server.dfef0364630f.png&amp;amp;w=1512&amp;amp;sig=7b1fd07e2bfbee2e23b36aa922a614d938b29698 1512w, https://files.realpython.com/media/discord-bot-navigate-to-server.dfef0364630f.png 3024w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Navigate to Server&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6658 &lt;p&gt;Select &lt;em&gt;Invite People&lt;/em&gt; just beside the guild list where you selected your guild. Check the box that says &lt;em&gt;Set this link to never expire&lt;/em&gt; and copy the link:&lt;/p&gt;
6659 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-copy-invite.0dd6b229c819.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-copy-invite.0dd6b229c819.png&quot; width=&quot;3024&quot; height=&quot;1766&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-copy-invite.0dd6b229c819.png&amp;amp;w=756&amp;amp;sig=81a88bffad8cbfa324a0eef38a1b6ff7ccfc67a5 756w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-copy-invite.0dd6b229c819.png&amp;amp;w=1512&amp;amp;sig=3c9f1911a3ebeb646c31e570e6a9ca8cff23f8cd 1512w, https://files.realpython.com/media/discord-bot-copy-invite.0dd6b229c819.png 3024w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Copy Invite Link&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6660 &lt;p&gt;Now, with the invite link copied, create a new account and join the guild using your invite link:&lt;/p&gt;
6661 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-accept-invite.4b33a1ba7062.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-accept-invite.4b33a1ba7062.png&quot; width=&quot;3021&quot; height=&quot;1767&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-accept-invite.4b33a1ba7062.png&amp;amp;w=755&amp;amp;sig=3b1a1db4101d859112e0a1d224d2aa8cc7606715 755w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-accept-invite.4b33a1ba7062.png&amp;amp;w=1510&amp;amp;sig=c5efa82e1d5940ff89422813371ddc83054cdbce 1510w, https://files.realpython.com/media/discord-bot-accept-invite.4b33a1ba7062.png 3021w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Accept Invite&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6662 &lt;p&gt;First, you&amp;rsquo;ll see that Discord introduced you to the guild by default with an automated message. More importantly though, notice the badge on the left-hand side of the screen that notifies you of a new message:&lt;/p&gt;
6663 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-direct-message-notification.95e423f72678.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-direct-message-notification.95e423f72678.png&quot; width=&quot;3022&quot; height=&quot;1768&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-direct-message-notification.95e423f72678.png&amp;amp;w=755&amp;amp;sig=84f08a7484e333f40081aa4071dafc4d331ca7f2 755w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-direct-message-notification.95e423f72678.png&amp;amp;w=1511&amp;amp;sig=51ed613c1cbe18a474ca12e6b648cb18d9a0505a 1511w, https://files.realpython.com/media/discord-bot-direct-message-notification.95e423f72678.png 3022w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Direct Message Notification&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6664 &lt;p&gt;When you select it, you&amp;rsquo;ll see a private message from your bot user:&lt;/p&gt;
6665 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-direct-message.7f49832b7bb7.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-direct-message.7f49832b7bb7.png&quot; width=&quot;3024&quot; height=&quot;1769&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-direct-message.7f49832b7bb7.png&amp;amp;w=756&amp;amp;sig=70663b008b61bdb83ce3f2f3b6cc4d4abda8a914 756w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-direct-message.7f49832b7bb7.png&amp;amp;w=1512&amp;amp;sig=5874eab3fb67f6bd0ba0b3a4ed5c6d401e23f8c8 1512w, https://files.realpython.com/media/discord-bot-direct-message.7f49832b7bb7.png 3024w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Direct Message&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6666 &lt;p&gt;Perfect! Your bot user is now interacting with other users with minimal code.&lt;/p&gt;
6667 &lt;p&gt;Next, you&amp;rsquo;ll learn how to respond to specific user messages in the chat.&lt;/p&gt;
6668 &lt;h3 id=&quot;responding-to-messages&quot;&gt;Responding to Messages&lt;/h3&gt;
6669 &lt;p&gt;Let&amp;rsquo;s add on to the previous functionality of your bot by handling the &lt;code&gt;on_message()&lt;/code&gt; event.&lt;/p&gt;
6670 &lt;p&gt;&lt;code&gt;on_message()&lt;/code&gt; occurs when a message is posted in a channel that your bot has access to. In this example, you&amp;rsquo;ll respond to the message &lt;code&gt;&#39;99!&#39;&lt;/code&gt; with a one-liner from the television show &lt;a href=&quot;https://www.nbc.com/brooklyn-nine-nine&quot;&gt;Brooklyn Nine-Nine&lt;/a&gt;:&lt;/p&gt;
6671 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
6672 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
6673     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;author&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
6674         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;
6675 
6676     &lt;span class=&quot;n&quot;&gt;brooklyn_99_quotes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
6677         &lt;span class=&quot;s1&quot;&gt;&amp;#39;I&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&amp;#39;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;m the human form of the ๐Ÿ’ฏ emoji.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
6678         &lt;span class=&quot;s1&quot;&gt;&amp;#39;Bingpot!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
6679         &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
6680             &lt;span class=&quot;s1&quot;&gt;&amp;#39;Cool. Cool cool cool cool cool cool cool, &amp;#39;&lt;/span&gt;
6681             &lt;span class=&quot;s1&quot;&gt;&amp;#39;no doubt no doubt no doubt no doubt.&amp;#39;&lt;/span&gt;
6682         &lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
6683     &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
6684 
6685     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;99!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
6686         &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;brooklyn_99_quotes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6687         &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6688 &lt;/pre&gt;&lt;/div&gt;
6689 
6690 &lt;p&gt;The bulk of this event handler looks at the &lt;code&gt;message.content&lt;/code&gt;, checks to see if it&amp;rsquo;s equal to &lt;code&gt;&#39;99!&#39;&lt;/code&gt;, and responds by sending a random quote to the message&amp;rsquo;s channel if it is.&lt;/p&gt;
6691 &lt;p&gt;The other piece is an important one:&lt;/p&gt;
6692 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;author&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
6693     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;
6694 &lt;/pre&gt;&lt;/div&gt;
6695 
6696 &lt;p&gt;Because a &lt;code&gt;Client&lt;/code&gt; can&amp;rsquo;t tell the difference between a bot user and a normal user account, your &lt;code&gt;on_message()&lt;/code&gt; handler should protect against a potentially recursive case where the bot sends a message that it might, itself, handle.&lt;/p&gt;
6697 &lt;p&gt;To illustrate, let&amp;rsquo;s say you want your bot to listen for users telling each other &lt;code&gt;&#39;Happy Birthday&#39;&lt;/code&gt;. You could implement your &lt;code&gt;on_message()&lt;/code&gt; handler like this:&lt;/p&gt;
6698 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
6699 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
6700     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;happy birthday&amp;#39;&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lower&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
6701         &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Happy Birthday! ๐ŸŽˆ๐ŸŽ‰&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6702 &lt;/pre&gt;&lt;/div&gt;
6703 
6704 &lt;p&gt;Aside from the potentially spammy nature of this event handler, it also has a devastating side effect. The message that the bot responds with contains the same message it&amp;rsquo;s going to handle!&lt;/p&gt;
6705 &lt;p&gt;So, if one person in the channel tells another &amp;ldquo;Happy Birthday,&amp;rdquo; then the bot will also chime in&amp;hellip; again&amp;hellip; and again&amp;hellip; and again:&lt;/p&gt;
6706 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-happy-birthday-repetition.864acfe23979.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-happy-birthday-repetition.864acfe23979.png&quot; width=&quot;3028&quot; height=&quot;1769&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-happy-birthday-repetition.864acfe23979.png&amp;amp;w=757&amp;amp;sig=1e56e02abfd0366024a7df56f2111fbb023f42be 757w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-happy-birthday-repetition.864acfe23979.png&amp;amp;w=1514&amp;amp;sig=2f67cb61941a012ff7d260910406a5c427c8f4c4 1514w, https://files.realpython.com/media/discord-bot-happy-birthday-repetition.864acfe23979.png 3028w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Happy Birthday Message Repetition&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6707 &lt;p&gt;That&amp;rsquo;s why it&amp;rsquo;s important to compare the &lt;code&gt;message.author&lt;/code&gt; to the &lt;code&gt;client.user&lt;/code&gt; (your bot user), and ignore any of its own messages.&lt;/p&gt;
6708 &lt;p&gt;So, let&amp;rsquo;s fix &lt;code&gt;bot.py&lt;/code&gt;:&lt;/p&gt;
6709 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# bot.py&lt;/span&gt;
6710 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
6711 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt;
6712 
6713 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;discord&lt;/span&gt;
6714 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dotenv&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;
6715 
6716 &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6717 &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_TOKEN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6718 
6719 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;discord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6720 
6721 &lt;span class=&quot;nd&quot;&gt;@client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
6722 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_ready&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
6723     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{client.user.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; has connected to Discord!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6724 
6725 &lt;span class=&quot;nd&quot;&gt;@client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
6726 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_member_join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;member&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
6727     &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;member&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_dm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6728     &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;member&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dm_channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
6729         &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Hi &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{member.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;, welcome to my Discord server!&amp;#39;&lt;/span&gt;
6730     &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6731 
6732 &lt;span class=&quot;nd&quot;&gt;@client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
6733 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
6734     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;author&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
6735         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;
6736 
6737     &lt;span class=&quot;n&quot;&gt;brooklyn_99_quotes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
6738         &lt;span class=&quot;s1&quot;&gt;&amp;#39;I&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&amp;#39;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;m the human form of the ๐Ÿ’ฏ emoji.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
6739         &lt;span class=&quot;s1&quot;&gt;&amp;#39;Bingpot!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
6740         &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
6741             &lt;span class=&quot;s1&quot;&gt;&amp;#39;Cool. Cool cool cool cool cool cool cool, &amp;#39;&lt;/span&gt;
6742             &lt;span class=&quot;s1&quot;&gt;&amp;#39;no doubt no doubt no doubt no doubt.&amp;#39;&lt;/span&gt;
6743         &lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
6744     &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
6745 
6746     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;99!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
6747         &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;brooklyn_99_quotes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6748         &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6749 
6750 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6751 &lt;/pre&gt;&lt;/div&gt;
6752 
6753 &lt;p&gt;Don&amp;rsquo;t forget to &lt;code&gt;import random&lt;/code&gt; at the top of the module, since the &lt;code&gt;on_message()&lt;/code&gt; handler utilizes &lt;code&gt;random.choice()&lt;/code&gt;.&lt;/p&gt;
6754 &lt;p&gt;Run the program:&lt;/p&gt;
6755 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python bot.py
6756 &lt;span class=&quot;go&quot;&gt;RealPythonTutorialBot has connected to Discord!&lt;/span&gt;
6757 &lt;/pre&gt;&lt;/div&gt;
6758 
6759 &lt;p&gt;Finally, head over to Discord to test it out:&lt;/p&gt;
6760 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-brooklyn-99-quotes.e934592e025e.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-brooklyn-99-quotes.e934592e025e.png&quot; width=&quot;3027&quot; height=&quot;1767&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-brooklyn-99-quotes.e934592e025e.png&amp;amp;w=756&amp;amp;sig=4f9a47703207d0920393a69f3411bd7d5914521b 756w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-brooklyn-99-quotes.e934592e025e.png&amp;amp;w=1513&amp;amp;sig=d78013102c8901874e9632b66e4a8d1b26f7b7fc 1513w, https://files.realpython.com/media/discord-bot-brooklyn-99-quotes.e934592e025e.png 3027w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Quotes From Brooklyn Nine-Nine&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6761 &lt;p&gt;Great! Now that you&amp;rsquo;ve seen a few different ways to handle some common Discord events, you&amp;rsquo;ll learn how to deal with errors that event handlers may raise.&lt;/p&gt;
6762 &lt;h3 id=&quot;handling-exceptions&quot;&gt;Handling Exceptions&lt;/h3&gt;
6763 &lt;p&gt;As you&amp;rsquo;ve seen already, &lt;code&gt;discord.py&lt;/code&gt; is an event-driven system. This focus on events extends even to exceptions. When one event handler &lt;a href=&quot;https://realpython.com/python-exceptions/&quot;&gt;raises an &lt;code&gt;Exception&lt;/code&gt;&lt;/a&gt;, Discord calls &lt;code&gt;on_error()&lt;/code&gt;.&lt;/p&gt;
6764 &lt;p&gt;The default behavior of &lt;code&gt;on_error()&lt;/code&gt; is to write the error message and stack trace to &lt;code&gt;stderr&lt;/code&gt;. To test this, add a special message handler to &lt;code&gt;on_message()&lt;/code&gt;:&lt;/p&gt;
6765 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# bot.py&lt;/span&gt;
6766 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
6767 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt;
6768 
6769 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;discord&lt;/span&gt;
6770 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dotenv&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;
6771 
6772 &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6773 &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_TOKEN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6774 
6775 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;discord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6776 
6777 &lt;span class=&quot;nd&quot;&gt;@client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
6778 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_ready&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
6779     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{client.user.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; has connected to Discord!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6780 
6781 &lt;span class=&quot;nd&quot;&gt;@client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
6782 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_member_join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;member&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
6783     &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;member&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_dm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6784     &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;member&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dm_channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
6785         &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Hi &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{member.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;, welcome to my Discord server!&amp;#39;&lt;/span&gt;
6786     &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6787 
6788 &lt;span class=&quot;nd&quot;&gt;@client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
6789 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
6790     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;author&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
6791         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;
6792 
6793     &lt;span class=&quot;n&quot;&gt;brooklyn_99_quotes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
6794         &lt;span class=&quot;s1&quot;&gt;&amp;#39;I&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&amp;#39;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;m the human form of the ๐Ÿ’ฏ emoji.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
6795         &lt;span class=&quot;s1&quot;&gt;&amp;#39;Bingpot!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
6796         &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
6797             &lt;span class=&quot;s1&quot;&gt;&amp;#39;Cool. Cool cool cool cool cool cool cool, &amp;#39;&lt;/span&gt;
6798             &lt;span class=&quot;s1&quot;&gt;&amp;#39;no doubt no doubt no doubt no doubt.&amp;#39;&lt;/span&gt;
6799         &lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
6800     &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
6801 
6802     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;99!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
6803         &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;brooklyn_99_quotes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6804         &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6805 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;raise-exception&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
6806 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;discord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DiscordException&lt;/span&gt;
6807 &lt;/span&gt;
6808 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6809 &lt;/pre&gt;&lt;/div&gt;
6810 
6811 &lt;p&gt;The new &lt;code&gt;raise-exception&lt;/code&gt; message handler allows you to raise a &lt;code&gt;DiscordException&lt;/code&gt; on command.&lt;/p&gt;
6812 &lt;p&gt;Run the program and type &lt;code&gt;raise-exception&lt;/code&gt; into the Discord channel:&lt;/p&gt;
6813 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-raise-exception.7fcae85fb06e.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-raise-exception.7fcae85fb06e.png&quot; width=&quot;3026&quot; height=&quot;1765&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-raise-exception.7fcae85fb06e.png&amp;amp;w=756&amp;amp;sig=5d0026bbd91a3b8bb1bd561f0779dde3150d588a 756w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-raise-exception.7fcae85fb06e.png&amp;amp;w=1513&amp;amp;sig=7b226812216c03ef139c4c33823dd6318dc34ec5 1513w, https://files.realpython.com/media/discord-bot-raise-exception.7fcae85fb06e.png 3026w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Raise Exception Message&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6814 &lt;p&gt;You should now see the &lt;code&gt;Exception&lt;/code&gt; that was raised by your &lt;code&gt;on_message()&lt;/code&gt; handler in the console:&lt;/p&gt;
6815 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python bot.py
6816 &lt;span class=&quot;go&quot;&gt;RealPythonTutorialBot has connected to Discord!&lt;/span&gt;
6817 &lt;span class=&quot;go&quot;&gt;Ignoring exception in on_message&lt;/span&gt;
6818 &lt;span class=&quot;go&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
6819 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;/Users/alex.ronquillo/.pyenv/versions/discord-venv/lib/python3.7/site-packages/discord/client.py&amp;quot;, line 255, in _run_event&lt;/span&gt;
6820 &lt;span class=&quot;go&quot;&gt;    await coro(*args, **kwargs)&lt;/span&gt;
6821 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;bot.py&amp;quot;, line 42, in on_message&lt;/span&gt;
6822 &lt;span class=&quot;go&quot;&gt;    raise discord.DiscordException&lt;/span&gt;
6823 &lt;span class=&quot;go&quot;&gt;discord.errors.DiscordException&lt;/span&gt;
6824 &lt;/pre&gt;&lt;/div&gt;
6825 
6826 &lt;p&gt;The exception was caught by the default error handler, so the output contains the message &lt;code&gt;Ignoring exception in on_message&lt;/code&gt;. Let&amp;rsquo;s fix that by handling that particular error. To do so, you&amp;rsquo;ll catch the &lt;code&gt;DiscordException&lt;/code&gt; and &lt;a href=&quot;https://realpython.com/working-with-files-in-python/&quot;&gt;write it to a file&lt;/a&gt; instead.&lt;/p&gt;
6827 &lt;p&gt;The &lt;code&gt;on_error()&lt;/code&gt; event handler takes the &lt;code&gt;event&lt;/code&gt; as the first argument. In this case, we expect the &lt;code&gt;event&lt;/code&gt; to be &lt;code&gt;&#39;on_message&#39;&lt;/code&gt;. It also accepts &lt;code&gt;*args&lt;/code&gt; and &lt;code&gt;**kwargs&lt;/code&gt; as flexible, positional and keyword arguments passed to the original event handler.&lt;/p&gt;
6828 &lt;p&gt;So, since &lt;code&gt;on_message()&lt;/code&gt; takes a single argument, &lt;code&gt;message&lt;/code&gt;, we expect &lt;code&gt;args[0]&lt;/code&gt; to be the &lt;code&gt;message&lt;/code&gt; that the user sent in the Discord channel:&lt;/p&gt;
6829 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
6830 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
6831     &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;err.log&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;a&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
6832         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;on_message&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
6833             &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Unhandled message: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{args[0]}&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6834         &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
6835             &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt;
6836 &lt;/pre&gt;&lt;/div&gt;
6837 
6838 &lt;p&gt;If the &lt;code&gt;Exception&lt;/code&gt; originated in the &lt;code&gt;on_message()&lt;/code&gt; event handler, you &lt;code&gt;.write()&lt;/code&gt; a formatted string to the file &lt;code&gt;err.log&lt;/code&gt;. If another event raises an &lt;code&gt;Exception&lt;/code&gt;, then we simply want our handler to re-raise the exception to invoke the default behavior.&lt;/p&gt;
6839 &lt;p&gt;Run &lt;code&gt;bot.py&lt;/code&gt; and send the &lt;code&gt;raise-exception&lt;/code&gt; message again to view the output in &lt;code&gt;err.log&lt;/code&gt;:&lt;/p&gt;
6840 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; cat err.log
6841 &lt;span class=&quot;go&quot;&gt;Unhandled message: &amp;lt;Message id=573845548923224084 pinned=False author=&amp;lt;Member id=543612676807327754 name=&amp;#39;alexronquillo&amp;#39; discriminator=&amp;#39;0933&amp;#39; bot=False nick=None guild=&amp;lt;Guild id=571759877328732195 name=&amp;#39;RealPythonTutorialServer&amp;#39; chunked=True&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
6842 &lt;/pre&gt;&lt;/div&gt;
6843 
6844 &lt;p&gt;Instead of only a stack trace, you have a more informative error, showing the &lt;code&gt;message&lt;/code&gt; that caused &lt;code&gt;on_message()&lt;/code&gt; to raise the &lt;code&gt;DiscordException&lt;/code&gt;, saved to a file for longer persistence.&lt;/p&gt;
6845 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
6846 &lt;p&gt;&lt;strong&gt;Technical Detail:&lt;/strong&gt; If you want to take the actual &lt;code&gt;Exception&lt;/code&gt; into account when you&amp;rsquo;re writing your error messages to &lt;code&gt;err.log&lt;/code&gt;, then you can use functions from &lt;code&gt;sys&lt;/code&gt;, such as &lt;a href=&quot;https://docs.python.org/library/sys.html#sys.exc_info&quot;&gt;&lt;code&gt;exc_info()&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
6847 &lt;/div&gt;
6848 &lt;p&gt;Now that you have some experience handling different events and interacting with Discord APIs, you&amp;rsquo;ll learn about a subclass of &lt;code&gt;Client&lt;/code&gt; called &lt;code&gt;Bot&lt;/code&gt;, which implements some handy, bot-specific functionality.&lt;/p&gt;
6849 &lt;h2 id=&quot;connecting-a-bot&quot;&gt;Connecting a Bot&lt;/h2&gt;
6850 &lt;p&gt;A &lt;code&gt;Bot&lt;/code&gt; is a subclass of &lt;code&gt;Client&lt;/code&gt; that adds a little bit of extra functionality that is useful when you&amp;rsquo;re creating bot users. For example, a &lt;code&gt;Bot&lt;/code&gt; can handle events and commands, invoke validation checks, and more.&lt;/p&gt;
6851 &lt;p&gt;Before you get into the features specific to &lt;code&gt;Bot&lt;/code&gt;, convert &lt;code&gt;bot.py&lt;/code&gt; to use a &lt;code&gt;Bot&lt;/code&gt; instead of a &lt;code&gt;Client&lt;/code&gt;:&lt;/p&gt;
6852 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# bot.py&lt;/span&gt;
6853 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
6854 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt;
6855 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dotenv&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;
6856 
6857 &lt;span class=&quot;c1&quot;&gt;# 1&lt;/span&gt;
6858 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;discord.ext&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commands&lt;/span&gt;
6859 
6860 &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6861 &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_TOKEN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6862 
6863 &lt;span class=&quot;c1&quot;&gt;# 2&lt;/span&gt;
6864 &lt;span class=&quot;n&quot;&gt;bot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commands&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Bot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;command_prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6865 
6866 &lt;span class=&quot;nd&quot;&gt;@bot&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
6867 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_ready&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
6868     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{bot.user.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; has connected to Discord!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6869 
6870 &lt;span class=&quot;n&quot;&gt;bot&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6871 &lt;/pre&gt;&lt;/div&gt;
6872 
6873 &lt;p&gt;As you can see, &lt;code&gt;Bot&lt;/code&gt; can handle events the same way that &lt;code&gt;Client&lt;/code&gt; does. However, notice the differences between &lt;code&gt;Client&lt;/code&gt; and &lt;code&gt;Bot&lt;/code&gt;:&lt;/p&gt;
6874 &lt;ol&gt;
6875 &lt;li&gt;&lt;code&gt;Bot&lt;/code&gt; is imported from the &lt;code&gt;discord.ext.commands&lt;/code&gt; module.&lt;/li&gt;
6876 &lt;li&gt;The &lt;code&gt;Bot&lt;/code&gt; initializer requires a &lt;code&gt;command_prefix&lt;/code&gt;, which you&amp;rsquo;ll learn more about in the next section.&lt;/li&gt;
6877 &lt;/ol&gt;
6878 &lt;p&gt;The extensions library, &lt;code&gt;ext&lt;/code&gt;, offers several interesting components to help you create a Discord &lt;code&gt;Bot&lt;/code&gt;. One such component is the &lt;a href=&quot;https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html&quot;&gt;&lt;code&gt;Command&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
6879 &lt;h3 id=&quot;using-bot-commands&quot;&gt;Using &lt;code&gt;Bot&lt;/code&gt; Commands&lt;/h3&gt;
6880 &lt;p&gt;In general terms, a &lt;strong&gt;command&lt;/strong&gt; is an order that a user gives to a bot so that it will do something. Commands are different from events because they are:&lt;/p&gt;
6881 &lt;ul&gt;
6882 &lt;li&gt;Arbitrarily defined&lt;/li&gt;
6883 &lt;li&gt;Directly called by the user&lt;/li&gt;
6884 &lt;li&gt;Flexible, in terms of their interface&lt;/li&gt;
6885 &lt;/ul&gt;
6886 &lt;p&gt;In technical terms, a &lt;strong&gt;&lt;code&gt;Command&lt;/code&gt;&lt;/strong&gt; is an object that wraps a function that is invoked by a text command in Discord. The text command must start with the &lt;code&gt;command_prefix&lt;/code&gt;, defined by the &lt;code&gt;Bot&lt;/code&gt; object.&lt;/p&gt;
6887 &lt;p&gt;Let&amp;rsquo;s take a look at an old event to better understand what this looks like:&lt;/p&gt;
6888 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# bot.py&lt;/span&gt;
6889 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
6890 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt;
6891 
6892 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;discord&lt;/span&gt;
6893 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dotenv&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;
6894 
6895 &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6896 &lt;span class=&quot;n&quot;&gt;TOKEN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_TOKEN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6897 
6898 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;discord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6899 
6900 &lt;span class=&quot;nd&quot;&gt;@client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
6901 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
6902     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;author&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
6903         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;
6904 
6905     &lt;span class=&quot;n&quot;&gt;brooklyn_99_quotes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
6906         &lt;span class=&quot;s1&quot;&gt;&amp;#39;I&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&amp;#39;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;m the human form of the ๐Ÿ’ฏ emoji.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
6907         &lt;span class=&quot;s1&quot;&gt;&amp;#39;Bingpot!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
6908         &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
6909             &lt;span class=&quot;s1&quot;&gt;&amp;#39;Cool. Cool cool cool cool cool cool cool, &amp;#39;&lt;/span&gt;
6910             &lt;span class=&quot;s1&quot;&gt;&amp;#39;no doubt no doubt no doubt no doubt.&amp;#39;&lt;/span&gt;
6911         &lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
6912     &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
6913 
6914     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;99!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
6915         &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;brooklyn_99_quotes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6916         &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6917 
6918 &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TOKEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6919 &lt;/pre&gt;&lt;/div&gt;
6920 
6921 &lt;p&gt;Here, you created an &lt;code&gt;on_message()&lt;/code&gt; event handler, which receives the &lt;code&gt;message&lt;/code&gt; string and compares it to a pre-defined option: &lt;code&gt;&#39;99!&#39;&lt;/code&gt;.&lt;/p&gt;
6922 &lt;p&gt;Using a &lt;code&gt;Command&lt;/code&gt;, you can convert this example to be more specific:&lt;/p&gt;
6923 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# bot.py&lt;/span&gt;
6924 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
6925 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt;
6926 
6927 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;discord.ext&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commands&lt;/span&gt;
6928 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dotenv&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;
6929 
6930 &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6931 &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_TOKEN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6932 
6933 &lt;span class=&quot;n&quot;&gt;bot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commands&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Bot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;command_prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6934 
6935 &lt;span class=&quot;nd&quot;&gt;@bot&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;99&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6936 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;nine_nine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
6937     &lt;span class=&quot;n&quot;&gt;brooklyn_99_quotes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
6938         &lt;span class=&quot;s1&quot;&gt;&amp;#39;I&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&amp;#39;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;m the human form of the ๐Ÿ’ฏ emoji.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
6939         &lt;span class=&quot;s1&quot;&gt;&amp;#39;Bingpot!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
6940         &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
6941             &lt;span class=&quot;s1&quot;&gt;&amp;#39;Cool. Cool cool cool cool cool cool cool, &amp;#39;&lt;/span&gt;
6942             &lt;span class=&quot;s1&quot;&gt;&amp;#39;no doubt no doubt no doubt no doubt.&amp;#39;&lt;/span&gt;
6943         &lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
6944     &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
6945 
6946     &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;brooklyn_99_quotes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6947     &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6948 
6949 &lt;span class=&quot;n&quot;&gt;bot&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6950 &lt;/pre&gt;&lt;/div&gt;
6951 
6952 &lt;p&gt;There are several important characteristics to understand about using &lt;code&gt;Command&lt;/code&gt;:&lt;/p&gt;
6953 &lt;ol&gt;
6954 &lt;li&gt;
6955 &lt;p&gt;Instead of using &lt;code&gt;bot.event&lt;/code&gt; like before, you use &lt;code&gt;bot.command()&lt;/code&gt;, passing the invocation command (&lt;code&gt;name&lt;/code&gt;) as its argument.&lt;/p&gt;
6956 &lt;/li&gt;
6957 &lt;li&gt;
6958 &lt;p&gt;The function will now only be called when &lt;code&gt;!99&lt;/code&gt; is mentioned in chat. This is different than the &lt;code&gt;on_message()&lt;/code&gt; event, which was executed any time a user sent a message, regardless of the content.&lt;/p&gt;
6959 &lt;/li&gt;
6960 &lt;li&gt;
6961 &lt;p&gt;The command must be prefixed with the exclamation point (&lt;code&gt;!&lt;/code&gt; ) because that&amp;rsquo;s the &lt;code&gt;command_prefix&lt;/code&gt; that you defined in the initializer for your &lt;code&gt;Bot&lt;/code&gt;.&lt;/p&gt;
6962 &lt;/li&gt;
6963 &lt;li&gt;
6964 &lt;p&gt;Any &lt;code&gt;Command&lt;/code&gt; function (technically called a &lt;code&gt;callback&lt;/code&gt;) must accept at least one parameter, called &lt;code&gt;ctx&lt;/code&gt;, which is the &lt;a href=&quot;https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html#invocation-context&quot;&gt;&lt;code&gt;Context&lt;/code&gt;&lt;/a&gt; surrounding the invoked &lt;code&gt;Command&lt;/code&gt;.&lt;/p&gt;
6965 &lt;/li&gt;
6966 &lt;/ol&gt;
6967 &lt;p&gt;A &lt;code&gt;Context&lt;/code&gt; holds data such as the channel and guild that the user called the &lt;code&gt;Command&lt;/code&gt; from.&lt;/p&gt;
6968 &lt;p&gt;Run the program:&lt;/p&gt;
6969 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python bot.py
6970 &lt;/pre&gt;&lt;/div&gt;
6971 
6972 &lt;p&gt;With your bot running, you can now head to Discord to try out your new command:&lt;/p&gt;
6973 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-brooklyn-99-command.f01b21540756.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-brooklyn-99-command.f01b21540756.png&quot; width=&quot;3031&quot; height=&quot;1769&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-brooklyn-99-command.f01b21540756.png&amp;amp;w=757&amp;amp;sig=dff1081b5b379d39c1cdd41115bdef90fa4a833c 757w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-brooklyn-99-command.f01b21540756.png&amp;amp;w=1515&amp;amp;sig=a05851348e0e7b2217a0900c531a16a42bf4f32e 1515w, https://files.realpython.com/media/discord-bot-brooklyn-99-command.f01b21540756.png 3031w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Brooklyn Nine-Nine Command&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6974 &lt;p&gt;From the user&amp;rsquo;s point of view, the practical difference is that the prefix helps formalize the command, rather than simply reacting to a particular &lt;code&gt;on_message()&lt;/code&gt; event.&lt;/p&gt;
6975 &lt;p&gt;This comes with other great benefits as well. For example, you can invoke the &lt;code&gt;help&lt;/code&gt; command to see all the commands that your &lt;code&gt;Bot&lt;/code&gt; handles:&lt;/p&gt;
6976 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-help-command.a2ec772cc910.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-help-command.a2ec772cc910.png&quot; width=&quot;3027&quot; height=&quot;1767&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-help-command.a2ec772cc910.png&amp;amp;w=756&amp;amp;sig=122cba0d422a63e39e790208f553a16ebbceb674 756w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-help-command.a2ec772cc910.png&amp;amp;w=1513&amp;amp;sig=e8ef5a87dcca2de43f9ff198bb191746c5757465 1513w, https://files.realpython.com/media/discord-bot-help-command.a2ec772cc910.png 3027w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Help Command&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
6977 &lt;p&gt;If you want to add a description to your command so that the &lt;code&gt;help&lt;/code&gt; message is more informative, simply pass a &lt;code&gt;help&lt;/code&gt; description to the &lt;code&gt;.command()&lt;/code&gt; decorator:&lt;/p&gt;
6978 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# bot.py&lt;/span&gt;
6979 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
6980 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt;
6981 
6982 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;discord.ext&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commands&lt;/span&gt;
6983 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dotenv&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;
6984 
6985 &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
6986 &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_TOKEN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6987 
6988 &lt;span class=&quot;n&quot;&gt;bot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commands&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Bot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;command_prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6989 
6990 &lt;span class=&quot;nd&quot;&gt;@bot&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;99&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;help&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Responds with a random quote from Brooklyn 99&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
6991 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;nine_nine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
6992     &lt;span class=&quot;n&quot;&gt;brooklyn_99_quotes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
6993         &lt;span class=&quot;s1&quot;&gt;&amp;#39;I&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&amp;#39;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;m the human form of the ๐Ÿ’ฏ emoji.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
6994         &lt;span class=&quot;s1&quot;&gt;&amp;#39;Bingpot!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
6995         &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
6996             &lt;span class=&quot;s1&quot;&gt;&amp;#39;Cool. Cool cool cool cool cool cool cool, &amp;#39;&lt;/span&gt;
6997             &lt;span class=&quot;s1&quot;&gt;&amp;#39;no doubt no doubt no doubt no doubt.&amp;#39;&lt;/span&gt;
6998         &lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
6999     &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
7000 
7001     &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;brooklyn_99_quotes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7002     &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7003 
7004 &lt;span class=&quot;n&quot;&gt;bot&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7005 &lt;/pre&gt;&lt;/div&gt;
7006 
7007 &lt;p&gt;Now, when the user invokes the &lt;code&gt;help&lt;/code&gt; command, your bot will present a description of your command:&lt;/p&gt;
7008 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-help-description.7f710c984c66.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-help-description.7f710c984c66.png&quot; width=&quot;3030&quot; height=&quot;1771&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-help-description.7f710c984c66.png&amp;amp;w=757&amp;amp;sig=183786aba9f5ba8b48f494d4403adfc38436116d 757w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-help-description.7f710c984c66.png&amp;amp;w=1515&amp;amp;sig=9fde033f6aae07bf16a859ca882fd6b5ffbcf574 1515w, https://files.realpython.com/media/discord-bot-help-description.7f710c984c66.png 3030w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Informative Help Description&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
7009 &lt;p&gt;Keep in mind that all of this functionality exists only for the &lt;code&gt;Bot&lt;/code&gt; subclass, not the &lt;code&gt;Client&lt;/code&gt; superclass.&lt;/p&gt;
7010 &lt;p&gt;&lt;code&gt;Command&lt;/code&gt; has another useful functionality: the ability to use a &lt;code&gt;Converter&lt;/code&gt; to change the types of its arguments.&lt;/p&gt;
7011 &lt;h3 id=&quot;converting-parameters-automatically&quot;&gt;Converting Parameters Automatically&lt;/h3&gt;
7012 &lt;p&gt;Another benefit of using commands is the ability to &lt;strong&gt;convert&lt;/strong&gt; parameters.&lt;/p&gt;
7013 &lt;p&gt;Sometimes, you require a parameter to be a certain type, but arguments to a &lt;code&gt;Command&lt;/code&gt; function are, by default, strings. A &lt;a href=&quot;https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html#converters&quot;&gt;&lt;code&gt;Converter&lt;/code&gt;&lt;/a&gt; lets you convert those parameters to the type that you expect.&lt;/p&gt;
7014 &lt;p&gt;For example, if you want to build a &lt;code&gt;Command&lt;/code&gt; for your bot user to simulate rolling some dice (knowing what you&amp;rsquo;ve learned so far), you might define it like this:&lt;/p&gt;
7015 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@bot&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;roll_dice&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;help&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Simulates rolling dice.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7016 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;roll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number_of_dice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number_of_sides&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
7017     &lt;span class=&quot;n&quot;&gt;dice&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
7018         &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number_of_sides&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
7019         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;number_of_dice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7020     &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
7021     &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;, &amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
7022 &lt;/pre&gt;&lt;/div&gt;
7023 
7024 &lt;p&gt;You defined &lt;code&gt;roll&lt;/code&gt; to take two parameters:&lt;/p&gt;
7025 &lt;ol&gt;
7026 &lt;li&gt;The number of dice to roll&lt;/li&gt;
7027 &lt;li&gt;The number of sides per die&lt;/li&gt;
7028 &lt;/ol&gt;
7029 &lt;p&gt;Then, you decorated it with &lt;code&gt;.command()&lt;/code&gt; so that you can invoke it with the &lt;code&gt;!roll_dice&lt;/code&gt; command. Finally, you &lt;code&gt;.send()&lt;/code&gt; the results in a message back to the &lt;code&gt;channel&lt;/code&gt;.&lt;/p&gt;
7030 &lt;p&gt;While this looks correct, it isn&amp;rsquo;t. Unfortunately, if you run &lt;code&gt;bot.py&lt;/code&gt;, and invoke the &lt;code&gt;!roll_dice&lt;/code&gt; command in your Discord channel, you&amp;rsquo;ll see the following error:&lt;/p&gt;
7031 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python bot.py
7032 &lt;span class=&quot;go&quot;&gt;Ignoring exception in command roll_dice:&lt;/span&gt;
7033 &lt;span class=&quot;go&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
7034 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;/Users/alex.ronquillo/.pyenv/versions/discord-venv/lib/python3.7/site-packages/discord/ext/commands/core.py&amp;quot;, line 63, in wrapped&lt;/span&gt;
7035 &lt;span class=&quot;go&quot;&gt;    ret = await coro(*args, **kwargs)&lt;/span&gt;
7036 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;bot.py&amp;quot;, line 40, in roll&lt;/span&gt;
7037 &lt;span class=&quot;go&quot;&gt;    for _ in range(number_of_dice)&lt;/span&gt;
7038 &lt;span class=&quot;go&quot;&gt;TypeError: &amp;#39;str&amp;#39; object cannot be interpreted as an integer&lt;/span&gt;
7039 
7040 &lt;span class=&quot;go&quot;&gt;The above exception was the direct cause of the following exception:&lt;/span&gt;
7041 
7042 &lt;span class=&quot;go&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
7043 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;/Users/alex.ronquillo/.pyenv/versions/discord-venv/lib/python3.7/site-packages/discord/ext/commands/bot.py&amp;quot;, line 860, in invoke&lt;/span&gt;
7044 &lt;span class=&quot;go&quot;&gt;    await ctx.command.invoke(ctx)&lt;/span&gt;
7045 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;/Users/alex.ronquillo/.pyenv/versions/discord-venv/lib/python3.7/site-packages/discord/ext/commands/core.py&amp;quot;, line 698, in invoke&lt;/span&gt;
7046 &lt;span class=&quot;go&quot;&gt;    await injected(*ctx.args, **ctx.kwargs)&lt;/span&gt;
7047 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;/Users/alex.ronquillo/.pyenv/versions/discord-venv/lib/python3.7/site-packages/discord/ext/commands/core.py&amp;quot;, line 72, in wrapped&lt;/span&gt;
7048 &lt;span class=&quot;go&quot;&gt;    raise CommandInvokeError(exc) from exc&lt;/span&gt;
7049 &lt;span class=&quot;go&quot;&gt;discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: &amp;#39;str&amp;#39; object cannot be interpreted as an integer&lt;/span&gt;
7050 &lt;/pre&gt;&lt;/div&gt;
7051 
7052 &lt;p&gt;In other words, &lt;code&gt;range()&lt;/code&gt; can&amp;rsquo;t accept a &lt;code&gt;str&lt;/code&gt; as an argument. Instead, it must be an &lt;code&gt;int&lt;/code&gt;. While you could cast each value to an &lt;code&gt;int&lt;/code&gt;, there is a better way: you can use a &lt;code&gt;Converter&lt;/code&gt; .&lt;/p&gt;
7053 &lt;p&gt;In &lt;code&gt;discord.py&lt;/code&gt;, a &lt;code&gt;Converter&lt;/code&gt; is defined using Python 3&amp;rsquo;s &lt;a href=&quot;https://realpython.com/python-type-checking/#annotations&quot;&gt;function annotations&lt;/a&gt;:&lt;/p&gt;
7054 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@bot&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;roll_dice&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;help&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Simulates rolling dice.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7055 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;roll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number_of_dice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number_of_sides&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
7056     &lt;span class=&quot;n&quot;&gt;dice&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
7057         &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number_of_sides&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
7058         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;number_of_dice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7059     &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
7060     &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;, &amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
7061 &lt;/pre&gt;&lt;/div&gt;
7062 
7063 &lt;p&gt;You added &lt;code&gt;: int&lt;/code&gt; annotations to the two parameters that you expect to be of type &lt;code&gt;int&lt;/code&gt;. Try the command again:&lt;/p&gt;
7064 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-roll-dice.0255e76f078e.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-roll-dice.0255e76f078e.png&quot; width=&quot;3025&quot; height=&quot;1769&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-roll-dice.0255e76f078e.png&amp;amp;w=756&amp;amp;sig=ab57569c628b33d801c556a37291da4c2bf38898 756w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-roll-dice.0255e76f078e.png&amp;amp;w=1512&amp;amp;sig=561a833205a5df4d50976436b4640f10942e6be3 1512w, https://files.realpython.com/media/discord-bot-roll-dice.0255e76f078e.png 3025w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Bot Dice-Rolling Command&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
7065 &lt;p&gt;With that little change, your command works! The difference is that you&amp;rsquo;re now converting the command arguments to &lt;code&gt;int&lt;/code&gt;, which makes them compatible with your function&amp;rsquo;s logic.&lt;/p&gt;
7066 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
7067 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; A &lt;code&gt;Converter&lt;/code&gt; can be any callable, not merely data types. The argument will be passed to the callable, and the return value will be passed into the &lt;code&gt;Command&lt;/code&gt;.&lt;/p&gt;
7068 &lt;/div&gt;
7069 &lt;p&gt;Next, you&amp;rsquo;ll learn about the &lt;code&gt;Check&lt;/code&gt; object and how it can improve your commands.&lt;/p&gt;
7070 &lt;h3 id=&quot;checking-command-predicates&quot;&gt;Checking Command Predicates&lt;/h3&gt;
7071 &lt;p&gt;A &lt;code&gt;Check&lt;/code&gt; is a predicate that is evaluated before a &lt;code&gt;Command&lt;/code&gt; is executed to ensure that the &lt;code&gt;Context&lt;/code&gt; surrounding the &lt;code&gt;Command&lt;/code&gt; invocation is valid.&lt;/p&gt;
7072 &lt;p&gt;In an earlier example, you did something similar to verify that the user who sent a message that the bot handles was not the bot user, itself:&lt;/p&gt;
7073 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;author&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
7074     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;
7075 &lt;/pre&gt;&lt;/div&gt;
7076 
7077 &lt;p&gt;The &lt;code&gt;commands&lt;/code&gt; extension provides a cleaner and more usable mechanism for performing this kind of check, namely using &lt;code&gt;Check&lt;/code&gt; objects.&lt;/p&gt;
7078 &lt;p&gt;To demonstrate how this works, assume you want to support a command &lt;code&gt;!create_channel &amp;lt;channel_name&amp;gt;&lt;/code&gt; that creates a new channel. However, you only want to allow administrators the ability to create new channels with this command.&lt;/p&gt;
7079 &lt;p&gt;First, you&amp;rsquo;ll need to create a new member role in the admin. Go into the Discord guild and select the &lt;em&gt;{Server Name} โ†’ Server Settings&lt;/em&gt; menu:&lt;/p&gt;
7080 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-server-settings.1eb7e71e881b.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-server-settings.1eb7e71e881b.png&quot; width=&quot;3023&quot; height=&quot;1768&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-server-settings.1eb7e71e881b.png&amp;amp;w=755&amp;amp;sig=65edbf90c63743f93f195bb32661ebd7dc8c453f 755w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-server-settings.1eb7e71e881b.png&amp;amp;w=1511&amp;amp;sig=9576ffae79458bead937645d0d0fc693e91c9972 1511w, https://files.realpython.com/media/discord-bot-server-settings.1eb7e71e881b.png 3023w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Server Settings Screen&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
7081 &lt;p&gt;Then, select &lt;em&gt;Roles&lt;/em&gt; from the left-hand navigation list:&lt;/p&gt;
7082 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-roles.bdc21374afa9.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-roles.bdc21374afa9.png&quot; width=&quot;3002&quot; height=&quot;1769&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-roles.bdc21374afa9.png&amp;amp;w=750&amp;amp;sig=c9353734130e1c77c2e9022990b89f79db8806b7 750w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-roles.bdc21374afa9.png&amp;amp;w=1501&amp;amp;sig=c990b12b692cfdee8eccd91f8dec3e70e1977a67 1501w, https://files.realpython.com/media/discord-bot-roles.bdc21374afa9.png 3002w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Navigate to Roles&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
7083 &lt;p&gt;Finally select the &lt;em&gt;+&lt;/em&gt; sign next to &lt;em&gt;ROLES&lt;/em&gt; and enter the name &lt;code&gt;admin&lt;/code&gt; and select &lt;em&gt;Save Changes&lt;/em&gt;:&lt;/p&gt;
7084 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-new-role.7e8d95291d0d.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-new-role.7e8d95291d0d.png&quot; width=&quot;3027&quot; height=&quot;1767&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-new-role.7e8d95291d0d.png&amp;amp;w=756&amp;amp;sig=f69a116bd35511e1c5b1b70d26b96c483d0f723e 756w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-new-role.7e8d95291d0d.png&amp;amp;w=1513&amp;amp;sig=3d883a2bb6f60a880175c06d523790ca07cc208c 1513w, https://files.realpython.com/media/discord-bot-new-role.7e8d95291d0d.png 3027w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Create New Admin Role&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
7085 &lt;p&gt;Now, you&amp;rsquo;ve created an &lt;code&gt;admin&lt;/code&gt; role that you can assign to particular users. Next, you&amp;rsquo;ll update &lt;code&gt;bot.py&lt;/code&gt; to &lt;code&gt;Check&lt;/code&gt; the user&amp;rsquo;s role before allowing them to initiate the command:&lt;/p&gt;
7086 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# bot.py&lt;/span&gt;
7087 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
7088 
7089 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;discord&lt;/span&gt;
7090 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;discord.ext&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commands&lt;/span&gt;
7091 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dotenv&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;
7092 
7093 &lt;span class=&quot;n&quot;&gt;load_dotenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
7094 &lt;span class=&quot;n&quot;&gt;TOKEN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;DISCORD_TOKEN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7095 
7096 &lt;span class=&quot;n&quot;&gt;bot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commands&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Bot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;command_prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7097 
7098 &lt;span class=&quot;nd&quot;&gt;@bot&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;create-channel&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7099 &lt;span class=&quot;nd&quot;&gt;@commands&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;has_role&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;admin&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7100 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_channel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;channel_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;real-python&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
7101     &lt;span class=&quot;n&quot;&gt;guild&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;guild&lt;/span&gt;
7102     &lt;span class=&quot;n&quot;&gt;existing_channel&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;discord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;utils&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;guild&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channel_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7103     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;existing_channel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
7104         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Creating a new channel: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{channel_name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7105         &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;guild&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_text_channel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channel_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7106 
7107 &lt;span class=&quot;n&quot;&gt;bot&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TOKEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7108 &lt;/pre&gt;&lt;/div&gt;
7109 
7110 &lt;p&gt;In &lt;code&gt;bot.py&lt;/code&gt;, you have a new &lt;code&gt;Command&lt;/code&gt; function, called &lt;code&gt;create_channel()&lt;/code&gt; which takes an optional &lt;code&gt;channel_name&lt;/code&gt; and creates that channel. &lt;code&gt;create_channel()&lt;/code&gt; is also decorated with a &lt;code&gt;Check&lt;/code&gt; called &lt;code&gt;has_role()&lt;/code&gt;.&lt;/p&gt;
7111 &lt;p&gt;You also use &lt;code&gt;discord.utils.get()&lt;/code&gt; to ensure that you don&amp;rsquo;t create a channel with the same name as an existing channel.&lt;/p&gt;
7112 &lt;p&gt;If you run this program as it is and type &lt;code&gt;!create-channel&lt;/code&gt; into your Discord channel, then you&amp;rsquo;ll see the following error message:&lt;/p&gt;
7113 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python bot.py
7114 &lt;span class=&quot;go&quot;&gt;Ignoring exception in command create-channel:&lt;/span&gt;
7115 &lt;span class=&quot;go&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
7116 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;/Users/alex.ronquillo/.pyenv/versions/discord-venv/lib/python3.7/site-packages/discord/ext/commands/bot.py&amp;quot;, line 860, in invoke&lt;/span&gt;
7117 &lt;span class=&quot;go&quot;&gt;    await ctx.command.invoke(ctx)&lt;/span&gt;
7118 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;/Users/alex.ronquillo/.pyenv/versions/discord-venv/lib/python3.7/site-packages/discord/ext/commands/core.py&amp;quot;, line 691, in invoke&lt;/span&gt;
7119 &lt;span class=&quot;go&quot;&gt;    await self.prepare(ctx)&lt;/span&gt;
7120 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;/Users/alex.ronquillo/.pyenv/versions/discord-venv/lib/python3.7/site-packages/discord/ext/commands/core.py&amp;quot;, line 648, in prepare&lt;/span&gt;
7121 &lt;span class=&quot;go&quot;&gt;    await self._verify_checks(ctx)&lt;/span&gt;
7122 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;/Users/alex.ronquillo/.pyenv/versions/discord-venv/lib/python3.7/site-packages/discord/ext/commands/core.py&amp;quot;, line 598, in _verify_checks&lt;/span&gt;
7123 &lt;span class=&quot;go&quot;&gt;    raise CheckFailure(&amp;#39;The check functions for command {0.qualified_name} failed.&amp;#39;.format(self))&lt;/span&gt;
7124 &lt;span class=&quot;go&quot;&gt;discord.ext.commands.errors.CheckFailure: The check functions for command create-channel failed.&lt;/span&gt;
7125 &lt;/pre&gt;&lt;/div&gt;
7126 
7127 &lt;p&gt;This &lt;code&gt;CheckFailure&lt;/code&gt; says that &lt;code&gt;has_role(&#39;admin&#39;)&lt;/code&gt; failed. Unfortunately, this error only prints to &lt;code&gt;stdout&lt;/code&gt;. It would be better to report this to the user in the channel. To do so, add the following event:&lt;/p&gt;
7128 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@bot&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;
7129 &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;on_command_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
7130     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commands&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;errors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CheckFailure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
7131         &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;You do not have the correct role for this command.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7132 &lt;/pre&gt;&lt;/div&gt;
7133 
7134 &lt;p&gt;This event handles an error event from the command and sends an informative error message back to the original &lt;code&gt;Context&lt;/code&gt; of the invoked &lt;code&gt;Command&lt;/code&gt;.&lt;/p&gt;
7135 &lt;p&gt;Try it all again, and you should see an error in the Discord channel:&lt;/p&gt;
7136 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-role-error-message.adfe85fe76a9.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-role-error-message.adfe85fe76a9.png&quot; width=&quot;3028&quot; height=&quot;1768&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-role-error-message.adfe85fe76a9.png&amp;amp;w=757&amp;amp;sig=f4c4c8c318e4079b634fd089dfbdbc14524de8d8 757w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-role-error-message.adfe85fe76a9.png&amp;amp;w=1514&amp;amp;sig=0ac21290b0674b8954a40c6a92c0e14c04a8ca78 1514w, https://files.realpython.com/media/discord-bot-role-error-message.adfe85fe76a9.png 3028w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Role Check Error&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
7137 &lt;p&gt;Great! Now, to resolve the issue, you&amp;rsquo;ll need to give yourself the &lt;em&gt;admin&lt;/em&gt; role:&lt;/p&gt;
7138 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-role-granted.081c0c317834.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-role-granted.081c0c317834.png&quot; width=&quot;3026&quot; height=&quot;1767&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-role-granted.081c0c317834.png&amp;amp;w=756&amp;amp;sig=d2cb8f8765ffd8fc574808a810170c267e06069f 756w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-role-granted.081c0c317834.png&amp;amp;w=1513&amp;amp;sig=33fb00357b97007f92ee8fa88ecadfdcecdb09f9 1513w, https://files.realpython.com/media/discord-bot-role-granted.081c0c317834.png 3026w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Grant Admin Role&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
7139 &lt;p&gt;With the &lt;em&gt;admin&lt;/em&gt; role, your user will pass the &lt;code&gt;Check&lt;/code&gt; and will be able to create channels using the command.&lt;/p&gt;
7140 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
7141 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Keep in mind that in order to assign a role, your user will have to have the correct permissions. The easiest way to ensure this is to sign in with the user that you created the guild with.&lt;/p&gt;
7142 &lt;/div&gt;
7143 &lt;p&gt;When you type &lt;code&gt;!create-channel&lt;/code&gt; again, you&amp;rsquo;ll successfully create the channel &lt;em&gt;real-python&lt;/em&gt;:&lt;/p&gt;
7144 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/discord-bot-new-channel.43cd2889446c.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/discord-bot-new-channel.43cd2889446c.png&quot; width=&quot;3026&quot; height=&quot;1768&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-new-channel.43cd2889446c.png&amp;amp;w=756&amp;amp;sig=768abb1b230e675f2d0b864311f357eff67414b2 756w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/discord-bot-new-channel.43cd2889446c.png&amp;amp;w=1513&amp;amp;sig=ee91b1a9895a80fa1d326de58e6e5b8ac4fd1719 1513w, https://files.realpython.com/media/discord-bot-new-channel.43cd2889446c.png 3026w&quot; sizes=&quot;75vw&quot; alt=&quot;Discord: Navigate to New Channel&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
7145 &lt;p&gt;Also, note that you can pass the optional &lt;code&gt;channel_name&lt;/code&gt; argument to name the channel to whatever you want!&lt;/p&gt;
7146 &lt;p&gt;With this last example, you combined a &lt;code&gt;Command&lt;/code&gt;, an event, a &lt;code&gt;Check&lt;/code&gt;, and even the &lt;code&gt;get()&lt;/code&gt; utility to create a useful Discord bot!&lt;/p&gt;
7147 &lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
7148 &lt;p&gt;Congratulations! Now, you&amp;rsquo;ve learned how to make a Discord bot in Python. You&amp;rsquo;re able to build bots for interacting with users in guilds that you create or even bots that other users can invite to interact with their communities. Your bots will be able to respond to messages and commands and numerous other events.&lt;/p&gt;
7149 &lt;p&gt;In this tutorial, you learned the basics of creating your own Discord bot. You now know:&lt;/p&gt;
7150 &lt;ul&gt;
7151 &lt;li&gt;What Discord is&lt;/li&gt;
7152 &lt;li&gt;Why &lt;code&gt;discord.py&lt;/code&gt; is so valuable&lt;/li&gt;
7153 &lt;li&gt;How to make a Discord bot in the Developer Portal&lt;/li&gt;
7154 &lt;li&gt;How to create a Discord connection in Python&lt;/li&gt;
7155 &lt;li&gt;How to handle events&lt;/li&gt;
7156 &lt;li&gt;How to create a &lt;code&gt;Bot&lt;/code&gt; connection&lt;/li&gt;
7157 &lt;li&gt;How to use bot commands, checks, and converters&lt;/li&gt;
7158 &lt;/ul&gt;
7159 &lt;p&gt;To read more about the powerful &lt;code&gt;discord.py&lt;/code&gt; library and take your bots to the next level, read through their extensive &lt;a href=&quot;https://discordapp.com/developers/docs/intro&quot;&gt;documentation&lt;/a&gt;. Also, now that you&amp;rsquo;re familiar with Discord APIs in general, you have a better foundation for building other types of Discord applications.&lt;/p&gt;
7160         &lt;hr /&gt;
7161         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
7162       </content>
7163     </entry>
7164   
7165     <entry>
7166       <title>An Effective Python Environment: Making Yourself at Home</title>
7167       <id>https://realpython.com/effective-python-environment/</id>
7168       <link href="https://realpython.com/effective-python-environment/"/>
7169       <updated>2019-08-14T14:00:00+00:00</updated>
7170       <summary>This guide will walk you through the decisions you need to make when customizing your development environment for working with Python.</summary>
7171       <content type="html">
7172         &lt;p&gt;When you&amp;rsquo;re first learning a new programming language, a lot of your time and effort go into understanding the syntax, code style, and built-in tooling. This is just as true for Python as it is for any other language. Once you gain enough familiarity to be comfortable with the ins and outs of Python, you can start to invest time into building a Python environment that will foster your productivity.&lt;/p&gt;
7173 &lt;p&gt;Your shell is more than a prebuilt program provided to you as-is. It&amp;rsquo;s a framework on which you can build an ecosystem. This ecosystem will come to fit your needs so that you can spend less time fiddling and more time thinking about the next big project you&amp;rsquo;re working on.&lt;/p&gt;
7174 &lt;p&gt;Although no two developers have the same setup, there are a number of choices everyone faces when cultivating their Python environment. It&amp;rsquo;s important to understand each of these decisions and the options available to you!&lt;/p&gt;
7175 &lt;p&gt;&lt;strong&gt;By the end of this article, you&amp;rsquo;ll be able to answer questions like:&lt;/strong&gt;&lt;/p&gt;
7176 &lt;ul&gt;
7177 &lt;li&gt;What shell should I use? What terminal should I use?&lt;/li&gt;
7178 &lt;li&gt;What version(s) of Python can I use?&lt;/li&gt;
7179 &lt;li&gt;How do I manage dependencies for different projects?&lt;/li&gt;
7180 &lt;li&gt;How can I make my tools do some of the work for me?&lt;/li&gt;
7181 &lt;/ul&gt;
7182 &lt;p&gt;Once you&amp;rsquo;ve answered these questions for yourself, you can embark on the journey of creating a Python environment to call your very own. Let&amp;rsquo;s get started!&lt;/p&gt;
7183 &lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Free Bonus:&lt;/strong&gt; &lt;a href=&quot;&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-dependency-pitfalls-email-course&quot; data-focus=&quot;false&quot;&gt;Click here to get access to a free 5-day class&lt;/a&gt; that shows you how to avoid common dependency management issues with tools like Pip, PyPI, Virtualenv, and requirements files.&lt;/p&gt;&lt;/div&gt;
7184 
7185 &lt;h2 id=&quot;shells&quot;&gt;Shells&lt;/h2&gt;
7186 &lt;p&gt;When you use a &lt;a href=&quot;https://realpython.com/command-line-interfaces-python-argparse/#what-is-a-command-line-interface&quot;&gt;command-line interface&lt;/a&gt; (CLI), you execute commands and see their output. A &lt;strong&gt;shell&lt;/strong&gt; is a program that provides this (usually text-based) interface to you. Shells often provide their own programming language that you can use to manipulate files, install software, and so on.&lt;/p&gt;
7187 &lt;p&gt;There are more unique shells than could be reasonably listed here, so you&amp;rsquo;ll see a few prominent ones. Others differ in syntax or enhanced features, but they generally provide the same core functionality.&lt;/p&gt;
7188 &lt;h3 id=&quot;unix-shells&quot;&gt;Unix Shells&lt;/h3&gt;
7189 &lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Unix&quot;&gt;Unix&lt;/a&gt; is a family of operating systems first developed in the early days of computing. Unix&amp;rsquo;s popularity has lasted through today, heavily inspiring Linux and macOS. The first shells were developed for use with Unix and Unix-like operating systems.&lt;/p&gt;
7190 &lt;h4 id=&quot;bourne-shell-sh&quot;&gt;Bourne Shell (&lt;code&gt;sh&lt;/code&gt;)&lt;/h4&gt;
7191 &lt;p&gt;The Bourne shell&amp;mdash;developed by Stephen Bourne for Bell Labs in 1979&amp;mdash;was one of the first to incorporate the idea of environment variables, conditionals, and loops. It has  provided a strong basis for many other shells in use today and is still available on most systems at &lt;code&gt;/bin/sh&lt;/code&gt;.&lt;/p&gt;
7192 &lt;h4 id=&quot;bourne-again-shell-bash&quot;&gt;Bourne-Again Shell (&lt;code&gt;bash&lt;/code&gt;)&lt;/h4&gt;
7193 &lt;p&gt;Built on the success of the original Bourne shell, &lt;code&gt;bash&lt;/code&gt; introduced improved user-interaction features. With &lt;code&gt;bash&lt;/code&gt;, you get &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-tab&quot;&gt;Tab&lt;/kbd&gt;&lt;/span&gt; completion, history, and wildcard searching for commands and paths. The &lt;code&gt;bash&lt;/code&gt; programming language provides more data types, like arrays.&lt;/p&gt;
7194 &lt;h4 id=&quot;z-shell-zsh&quot;&gt;Z Shell (&lt;code&gt;zsh&lt;/code&gt;)&lt;/h4&gt;
7195 &lt;p&gt;&lt;code&gt;zsh&lt;/code&gt; combines many of the best features from other shells along with a few of its own tricks into one experience. &lt;code&gt;zsh&lt;/code&gt; offers autocorrection of misspelled commands, shorthand for manipulating multiple files, and advanced options for customizing your command prompt.&lt;/p&gt;
7196 &lt;p&gt;&lt;code&gt;zsh&lt;/code&gt; also provides a framework for deep customization. The &lt;a href=&quot;https://ohmyz.sh&quot;&gt;Oh My Zsh&lt;/a&gt; project supplies a rich set of themes and plugins, and is often used hand in hand with &lt;code&gt;zsh&lt;/code&gt;.&lt;/p&gt;
7197 &lt;p&gt;&lt;a href=&quot;https://support.apple.com/en-us/HT208050&quot;&gt;macOS will ship with &lt;code&gt;zsh&lt;/code&gt; as its default shell starting with Catalina&lt;/a&gt;, speaking to the shell&amp;rsquo;s popularity. Consider acquainting yourself with &lt;code&gt;zsh&lt;/code&gt; now so that you&amp;rsquo;ll be comfortable with it going forward.&lt;/p&gt;
7198 &lt;h4 id=&quot;xonsh&quot;&gt;Xonsh&lt;/h4&gt;
7199 &lt;p&gt;If you&amp;rsquo;re feeling particularly adventurous, you can give &lt;a href=&quot;https://xon.sh&quot;&gt;Xonsh&lt;/a&gt; a try. Xonsh is a shell that combines some features of other Unix-like shells with the power of Python syntax. You can use the language you already know to accomplish tasks on your filesystem and so on.&lt;/p&gt;
7200 &lt;p&gt;Although Xonsh is powerful, it lacks the compatibility other shells tend to share. You might not be able to run many existing shell scripts in Xonsh as a result. If you find that you like Xonsh, but compatibility is a concern, then you can use Xonsh as a supplement to your activities in a more widely used shell.&lt;/p&gt;
7201 &lt;h3 id=&quot;windows-shells&quot;&gt;Windows Shells&lt;/h3&gt;
7202 &lt;p&gt;Similarly to Unix-like operating systems, Windows also offers a number of options when it comes to shells. The shells offered in Windows vary in features and syntax, so you may need to try several to find one you like best.&lt;/p&gt;
7203 &lt;h4 id=&quot;cmd-cmdexe&quot;&gt;CMD (&lt;code&gt;cmd.exe&lt;/code&gt;)&lt;/h4&gt;
7204 &lt;p&gt;CMD (short for &amp;ldquo;command&amp;rdquo;) is the default CLI shell for Windows. It&amp;rsquo;s the successor to COMMAND.COM, the shell built for DOS (disk operating system).&lt;/p&gt;
7205 &lt;p&gt;Because DOS and Unix evolved independently, the commands and syntax in CMD are markedly different from shells built for Unix-like systems. However, CMD still provides the same core functionality for browsing and manipulating files, running commands, and viewing output.&lt;/p&gt;
7206 &lt;h4 id=&quot;powershell&quot;&gt;PowerShell&lt;/h4&gt;
7207 &lt;p&gt;PowerShell was released in 2006 and also ships with Windows. It provides Unix-like aliases for most commands, so if you&amp;rsquo;re coming to Windows from macOS or Linux or have to use both, then PowerShell might be great for you.&lt;/p&gt;
7208 &lt;p&gt;PowerShell is vastly more powerful than CMD. With PowerShell you can:&lt;/p&gt;
7209 &lt;ul&gt;
7210 &lt;li&gt;Pipe the output of one command to the input of another&lt;/li&gt;
7211 &lt;li&gt;Automate tasks through the exposed Windows management features&lt;/li&gt;
7212 &lt;li&gt;Use a scripting language to accomplish complex tasks&lt;/li&gt;
7213 &lt;/ul&gt;
7214 &lt;h4 id=&quot;windows-subsystem-for-linux&quot;&gt;Windows Subsystem for Linux&lt;/h4&gt;
7215 &lt;p&gt;Microsoft has released a &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/wsl/install-win10&quot;&gt;Windows subsystem for Linux&lt;/a&gt; (WSL) for running Linux directly on Windows. If you install WSL, then you can use &lt;code&gt;zsh&lt;/code&gt;, &lt;code&gt;bash&lt;/code&gt;, or any other Unix-like shell. If you want strong compatibility across your Windows and macOS or Linux environments, then be sure to give WSL a try. You may also consider &lt;a href=&quot;https://opensource.com/article/18/5/dual-boot-linux&quot;&gt;dual-booting Linux and Windows&lt;/a&gt; as an alternative.&lt;/p&gt;
7216 &lt;p&gt;See this &lt;a href=&quot;https://en.wikipedia.org/wiki/Comparison_of_command_shells&quot;&gt;comparison of command shells&lt;/a&gt; for exhaustive coverage.&lt;/p&gt;
7217 &lt;h2 id=&quot;terminal-emulators&quot;&gt;Terminal Emulators&lt;/h2&gt;
7218 &lt;p&gt;Early developers used &lt;strong&gt;terminals&lt;/strong&gt; to interact with a central mainframe computer. These were devices with a keyboard and a screen or printer that would display computed output.&lt;/p&gt;
7219 &lt;p&gt;Today, computers are portable and don&amp;rsquo;t require separate devices to interact with them, but the terminology still remains. Whereas a shell provides the prompt and interpreter you use to interface with text-based CLI tools, a terminal &lt;strong&gt;emulator&lt;/strong&gt; (often shortened to &lt;strong&gt;terminal&lt;/strong&gt;) is the graphical application you run to access the shell.&lt;/p&gt;
7220 &lt;p&gt;Almost any terminal you encounter should support the same basic features:&lt;/p&gt;
7221 &lt;ul&gt;
7222 &lt;li&gt;&lt;strong&gt;Text colors&lt;/strong&gt; for syntax highlighting in your code or distinguishing meaningful text in command output&lt;/li&gt;
7223 &lt;li&gt;&lt;strong&gt;Scrolling&lt;/strong&gt; for viewing an earlier command or its output&lt;/li&gt;
7224 &lt;li&gt;&lt;strong&gt;Copy/paste&lt;/strong&gt; for transferring text in or out of the shell from other programs&lt;/li&gt;
7225 &lt;li&gt;&lt;strong&gt;Tabs&lt;/strong&gt; for running multiple programs at once or separating your work into different sessions&lt;/li&gt;
7226 &lt;/ul&gt;
7227 &lt;h3 id=&quot;macos-terminals&quot;&gt;macOS Terminals&lt;/h3&gt;
7228 &lt;p&gt;The terminal options available for macOS are all full-featured, differing mostly in aesthetics and specific integrations with other tools.&lt;/p&gt;
7229 &lt;h4 id=&quot;terminal&quot;&gt;Terminal&lt;/h4&gt;
7230 &lt;p&gt;If you&amp;rsquo;re using a Mac, then you may have used the built-in &lt;a href=&quot;https://support.apple.com/guide/terminal/welcome/mac&quot;&gt;Terminal&lt;/a&gt; app before. Terminal supports all the usual functionality, and you can also customize the color scheme and a few hotkeys. It&amp;rsquo;s a nice enough tool if you don&amp;rsquo;t need many bells and whistles. You can find the Terminal app in &lt;em&gt;Applications &amp;rarr; Utilities &amp;rarr; Terminal&lt;/em&gt; on macOS.&lt;/p&gt;
7231 &lt;h4 id=&quot;iterm2&quot;&gt;iTerm2&lt;/h4&gt;
7232 &lt;p&gt;I&amp;rsquo;ve been a long-time user of &lt;a href=&quot;https://iterm2.com&quot;&gt;iTerm2&lt;/a&gt;. It takes the developer experience on Mac a step further, offering a much wider palette of customization and productivity options that enable you to:&lt;/p&gt;
7233 &lt;ul&gt;
7234 &lt;li&gt;Integrate with the shell to jump quickly to previously entered commands&lt;/li&gt;
7235 &lt;li&gt;Create custom search term highlighting in the output from commands&lt;/li&gt;
7236 &lt;li&gt;Open URLs and files displayed in the terminal with &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd&gt;click&lt;/kbd&gt;&lt;/span&gt;&lt;/li&gt;
7237 &lt;/ul&gt;
7238 &lt;p&gt;A Python API ships with the latest versions of iTerm2, so you can even improve your Python chops by developing more intricate customizations!&lt;/p&gt;
7239 &lt;p&gt;iTerm2 is popular enough to enjoy first-class integration with several other tools, and has a healthy community building plugins and so on. It&amp;rsquo;s a good choice because of its more frequent release cycle compared to Terminal, which only updates as often as macOS does.&lt;/p&gt;
7240 &lt;h4 id=&quot;hyper&quot;&gt;Hyper&lt;/h4&gt;
7241 &lt;p&gt;A relative newcomer, &lt;a href=&quot;https://hyper.is/&quot;&gt;Hyper&lt;/a&gt; is a terminal built on &lt;a href=&quot;https://electronjs.org/&quot;&gt;Electron&lt;/a&gt;, a framework for building desktop applications using web technologies. Electron apps are heavily customizable because they&amp;rsquo;re &amp;ldquo;just JavaScript&amp;rdquo; under the hood. You can create any functionality that you can write the JavaScript for.&lt;/p&gt;
7242 &lt;p&gt;On the other hand, JavaScript is a high-level programming language and won&amp;rsquo;t always perform as well as low-level languages like Objective-C or Swift. Be mindful of the plugins you install or create!&lt;/p&gt;
7243 &lt;h3 id=&quot;windows-terminals&quot;&gt;Windows Terminals&lt;/h3&gt;
7244 &lt;p&gt;As with the shell options, Windows terminal options vary widely in utility. Some are tightly bound to a particular shell as well.&lt;/p&gt;
7245 &lt;h4 id=&quot;command-prompt&quot;&gt;Command Prompt&lt;/h4&gt;
7246 &lt;p&gt;Command Prompt is the graphical application you can use to work with CMD in Windows. Like CMD, it&amp;rsquo;s a bare-bones tool for getting a few small things done. Although Command Prompt and CMD provide fewer features than other alternatives, you can be confident that they&amp;rsquo;ll be available on nearly every Windows installation and in a consistent place.&lt;/p&gt;
7247 &lt;h4 id=&quot;cygwin&quot;&gt;Cygwin&lt;/h4&gt;
7248 &lt;p&gt;Cygwin is a third-party suite of tools for Windows that provides a Unix-like wrapper. This was my preferred setup when I was in Windows, but you may consider adopting the Windows Subsystem for Linux as it receives more traction and polish.&lt;/p&gt;
7249 &lt;h4 id=&quot;windows-terminal&quot;&gt;Windows Terminal&lt;/h4&gt;
7250 &lt;p&gt;Microsoft recently released an open source terminal for Windows 10 called &lt;a href=&quot;https://github.com/Microsoft/Terminal&quot;&gt;Windows Terminal&lt;/a&gt;. It lets you work in CMD, PowerShell, and even the Windows Subsystem for Linux. If you need to do a fair amount of shell work in Windows, then Windows Terminal is probably your best bet! Windows Terminal is still in late beta, so it doesn&amp;rsquo;t ship with Windows yet. Check the documentation for instructions on getting access.&lt;/p&gt;
7251 &lt;h2 id=&quot;python-version-management&quot;&gt;Python Version Management&lt;/h2&gt;
7252 &lt;p&gt;With your choice of terminal and shell made, you can focus your attention on your Python environment specifically.&lt;/p&gt;
7253 &lt;p&gt;Something you&amp;rsquo;ll eventually run into is the need to run multiple &lt;strong&gt;versions&lt;/strong&gt; of Python. Projects you use may only run on certain versions, or you may be interested in creating a project that supports multiple Python versions. You can configure your Python environment to accommodate these needs.&lt;/p&gt;
7254 &lt;p&gt;macOS and most Unix operating systems come with a version of Python installed by default. This is often called the &lt;strong&gt;system Python&lt;/strong&gt;. The system Python works just fine, but it&amp;rsquo;s usually out of date. As of this writing, macOS High Sierra still ships with Python 2.7.10 as the system Python.&lt;/p&gt;
7255 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
7256 &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: You&amp;rsquo;ll almost certainly want to install the latest version of Python at a minimum, so you&amp;rsquo;ll have at least two versions of Python already. &lt;/p&gt;
7257 &lt;p&gt;&lt;strong&gt;It&amp;rsquo;s important that you leave the system Python as the default&lt;/strong&gt;, because many parts of the system rely on the default Python being a specific version. This is one of many great reasons to customize your Python environment!&lt;/p&gt;
7258 &lt;/div&gt;
7259 &lt;p&gt;How do you navigate this? Tooling is here to help.&lt;/p&gt;
7260 &lt;h3 id=&quot;pyenv&quot;&gt;&lt;code&gt;pyenv&lt;/code&gt;&lt;/h3&gt;
7261 &lt;p&gt;&lt;a href=&quot;https://github.com/pyenv/pyenv&quot;&gt;&lt;code&gt;pyenv&lt;/code&gt;&lt;/a&gt; is a mature tool for installing and managing multiple Python versions on macOS. I recommend &lt;a href=&quot;https://github.com/pyenv/pyenv#homebrew-on-macos&quot;&gt;installing it with Homebrew&lt;/a&gt;. If you&amp;rsquo;re using Windows, you can use &lt;a href=&quot;https://github.com/pyenv-win/pyenv-win#installation&quot;&gt;&lt;code&gt;pyenv-win&lt;/code&gt;&lt;/a&gt;. After you&amp;rsquo;ve got &lt;code&gt;pyenv&lt;/code&gt; installed, you can install multiple versions of Python into your Python environment with a few short commands:&lt;/p&gt;
7262 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pyenv versions
7263 &lt;span class=&quot;go&quot;&gt;* system&lt;/span&gt;
7264 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python --version
7265 &lt;span class=&quot;go&quot;&gt;Python 2.7.10&lt;/span&gt;
7266 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pyenv install &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;.7.3  &lt;span class=&quot;c1&quot;&gt;# This may take some time&lt;/span&gt;
7267 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pyenv versions
7268 &lt;span class=&quot;go&quot;&gt;* system&lt;/span&gt;
7269 &lt;span class=&quot;go&quot;&gt;  3.7.3&lt;/span&gt;
7270 &lt;/pre&gt;&lt;/div&gt;
7271 
7272 &lt;p&gt;You can manage which Python you&amp;rsquo;d like to use in your current session, globally, or on a per-project basis as well. &lt;code&gt;pyenv&lt;/code&gt; will make the &lt;code&gt;python&lt;/code&gt; command point to whichever Python you specify. Note that none of these overrides the default system Python for other applications, so you&amp;rsquo;re safe to use them however they work best for you within your Python environment:&lt;/p&gt;
7273 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pyenv global &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;.7.3
7274 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pyenv versions
7275 &lt;span class=&quot;go&quot;&gt;  system&lt;/span&gt;
7276 &lt;span class=&quot;go&quot;&gt;* 3.7.3 (set by /Users/dhillard/.pyenv/version)&lt;/span&gt;
7277 
7278 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pyenv &lt;span class=&quot;nb&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;.7.3
7279 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pyenv versions
7280 &lt;span class=&quot;go&quot;&gt;  system&lt;/span&gt;
7281 &lt;span class=&quot;go&quot;&gt;* 3.7.3 (set by /Users/dhillard/myproj/.python-version)&lt;/span&gt;
7282 
7283 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pyenv shell &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;.7.3
7284 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pyenv versions
7285 &lt;span class=&quot;go&quot;&gt;  system&lt;/span&gt;
7286 &lt;span class=&quot;go&quot;&gt;* 3.7.3 (set by PYENV_VERSION environment variable)&lt;/span&gt;
7287 
7288 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python --version
7289 &lt;span class=&quot;go&quot;&gt;Python 3.7.3&lt;/span&gt;
7290 &lt;/pre&gt;&lt;/div&gt;
7291 
7292 &lt;p&gt;Because I use a specific version of Python for work, the latest version of Python for personal projects, and multiple versions for testing open source projects, &lt;code&gt;pyenv&lt;/code&gt; has proven to be a fairly smooth way for me to manage all these different versions within my own Python environment. See &lt;a href=&quot;https://realpython.com/intro-to-pyenv/&quot;&gt;Managing Multiple Python Versions with &lt;code&gt;pyenv&lt;/code&gt;&lt;/a&gt; for a detailed overview of the tool.&lt;/p&gt;
7293 &lt;h3 id=&quot;conda&quot;&gt;&lt;code&gt;conda&lt;/code&gt;&lt;/h3&gt;
7294 &lt;p&gt;If you&amp;rsquo;re in the data science community, you might already be using &lt;a href=&quot;https://www.anaconda.com/distribution/&quot;&gt;Anaconda&lt;/a&gt; (or &lt;a href=&quot;https://docs.conda.io/en/latest/miniconda.html&quot;&gt;Miniconda&lt;/a&gt;). Anaconda is a sort of one-stop shop for data science software that supports more than just Python.&lt;/p&gt;
7295 &lt;p&gt;If you don&amp;rsquo;t need the data science packages or all the things that come pre-packaged with Anaconda, &lt;code&gt;pyenv&lt;/code&gt; might be a better lightweight solution for you. Managing Python versions is pretty similar in each, though. You can install Python versions similarly to &lt;code&gt;pyenv&lt;/code&gt;, using the &lt;code&gt;conda&lt;/code&gt; command:&lt;/p&gt;
7296 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; conda install &lt;span class=&quot;nv&quot;&gt;python&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;.7.3
7297 &lt;/pre&gt;&lt;/div&gt;
7298 
7299 &lt;p&gt;You&amp;rsquo;ll see a verbose list of all the dependent software &lt;code&gt;conda&lt;/code&gt; will install, and it will ask you to confirm.&lt;/p&gt;
7300 &lt;p&gt;&lt;code&gt;conda&lt;/code&gt; doesn&amp;rsquo;t have a way to set the &amp;ldquo;default&amp;rdquo; Python version or even a good way to see which versions of Python you&amp;rsquo;ve installed. Rather, it hinges on the concept of &amp;ldquo;environments,&amp;rdquo; which you can read more about in the following sections.&lt;/p&gt;
7301 &lt;h2 id=&quot;virtual-environments&quot;&gt;Virtual Environments&lt;/h2&gt;
7302 &lt;p&gt;Now you know how to manage multiple Python versions. Often, you&amp;rsquo;ll be working on multiple projects that need the &lt;em&gt;same&lt;/em&gt; Python version.&lt;/p&gt;
7303 &lt;p&gt;Because each project has its own set of dependencies, it&amp;rsquo;s a good practice to avoid mixing them. If all the dependencies are installed together in a single Python environment, then it will be difficult to discern where each one came from. In the worst cases, two different projects may depend on two different versions of a package, but with Python you can only have one version of a package installed at one time. What a mess!&lt;/p&gt;
7304 &lt;p&gt;Enter &lt;strong&gt;virtual environments&lt;/strong&gt;. You can think of a virtual environment as a carbon copy of a base version of Python. If you&amp;rsquo;ve installed Python 3.7.3, for example, then you can create many virtual environments based off of it. When you install a package in a virtual environment, you do it in isolation from other Python environments you may have. Each virtual environment has its own copy of the &lt;code&gt;python&lt;/code&gt; executable.&lt;/p&gt;
7305 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
7306 &lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;: Most virtual environment tooling provides a way to update your shell&amp;rsquo;s command prompt to show the current active virtual environment. Make sure to do this if you frequently switch between projects so you&amp;rsquo;re sure you&amp;rsquo;re working inside the correct virtual environment.&lt;/p&gt;
7307 &lt;/div&gt;
7308 &lt;h3 id=&quot;venv&quot;&gt;&lt;code&gt;venv&lt;/code&gt;&lt;/h3&gt;
7309 &lt;p&gt;&lt;a href=&quot;https://docs.python.org/3/library/venv.html&quot;&gt;&lt;code&gt;venv&lt;/code&gt;&lt;/a&gt; ships with Python versions 3.3+. You can create virtual environments just by passing it a path at which to store the environment&amp;rsquo;s &lt;code&gt;python&lt;/code&gt;, installed packages, and so on:&lt;/p&gt;
7310 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python -m venv ~/.virtualenvs/my-env
7311 &lt;/pre&gt;&lt;/div&gt;
7312 
7313 &lt;p&gt;You activate a virtual environment by sourcing its &lt;code&gt;activate&lt;/code&gt; script:&lt;/p&gt;
7314 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; ~/.virtualenvs/my-env/bin/activate
7315 &lt;/pre&gt;&lt;/div&gt;
7316 
7317 &lt;p&gt;You exit the virtual environment using the &lt;code&gt;deactivate&lt;/code&gt; command, which is made available when you activate the virtual environment:&lt;/p&gt;
7318 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;(my-env)$&lt;/span&gt; deactivate
7319 &lt;/pre&gt;&lt;/div&gt;
7320 
7321 &lt;p&gt;&lt;code&gt;venv&lt;/code&gt; is built on the wonderful work and successes of the independent &lt;a href=&quot;https://virtualenv.pypa.io/en/stable/&quot;&gt;&lt;code&gt;virtualenv&lt;/code&gt;&lt;/a&gt; project. &lt;code&gt;virtualenv&lt;/code&gt; still provides a few interesting features of its own, but &lt;code&gt;venv&lt;/code&gt; is nice because it provides the utility of virtual environments without requiring you to install additional software. You can probably get pretty far with it if you&amp;rsquo;re working mostly in a single Python version in your Python environment.&lt;/p&gt;
7322 &lt;p&gt;If you&amp;rsquo;re already managing multiple Python versions (or plan to), then it could make sense to integrate with that tooling to simplify the process of making new virtual environments with specific versions of Python. The &lt;code&gt;pyenv&lt;/code&gt; and &lt;code&gt;conda&lt;/code&gt; ecosystems both provide ways to specify the Python version to use when you create new virtual environments, covered in the following sections.&lt;/p&gt;
7323 &lt;h3 id=&quot;pyenv-virtualenv&quot;&gt;&lt;code&gt;pyenv-virtualenv&lt;/code&gt;&lt;/h3&gt;
7324 &lt;p&gt;If you&amp;rsquo;re using &lt;code&gt;pyenv&lt;/code&gt;, then &lt;a href=&quot;https://github.com/pyenv/pyenv-virtualenv&quot;&gt;&lt;code&gt;pyenv-virtualenv&lt;/code&gt;&lt;/a&gt; enhances &lt;code&gt;pyenv&lt;/code&gt; with a subcommand for managing virtual environments:&lt;/p&gt;
7325 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;// Create virtual environment&lt;/span&gt;
7326 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pyenv virtualenv &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;.7.3 my-env
7327 
7328 &lt;span class=&quot;go&quot;&gt;// Activate virtual environment&lt;/span&gt;
7329 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pyenv activate my-env
7330 
7331 &lt;span class=&quot;go&quot;&gt;// Exit virtual environment&lt;/span&gt;
7332 &lt;span class=&quot;gp&quot;&gt;(my-env)$&lt;/span&gt; pyenv deactivate
7333 &lt;/pre&gt;&lt;/div&gt;
7334 
7335 &lt;p&gt;I switch contexts between a large handful of projects on a day-to-day basis. As a result, I have at least a dozen distinct virtual environments to manage in my Python environment. What&amp;rsquo;s really nice about &lt;code&gt;pyenv-virtualenv&lt;/code&gt; is that you can configure a virtual environment using the &lt;code&gt;pyenv local&lt;/code&gt; command and have &lt;code&gt;pyenv-virtualenv&lt;/code&gt; auto-activate the right environments as you switch to different directories:&lt;/p&gt;
7336 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pyenv virtualenv &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;.7.3 proj1
7337 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pyenv virtualenv &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;.7.3 proj2
7338 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /Users/dhillard/proj1
7339 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pyenv &lt;span class=&quot;nb&quot;&gt;local&lt;/span&gt; proj1
7340 &lt;span class=&quot;gp&quot;&gt;(proj1)$&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; ../proj2
7341 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pyenv &lt;span class=&quot;nb&quot;&gt;local&lt;/span&gt; proj2
7342 &lt;span class=&quot;gp&quot;&gt;(proj2)$&lt;/span&gt; pyenv versions
7343 &lt;span class=&quot;go&quot;&gt;  system&lt;/span&gt;
7344 &lt;span class=&quot;go&quot;&gt;  3.7.3&lt;/span&gt;
7345 &lt;span class=&quot;go&quot;&gt;  3.7.3/envs/proj1&lt;/span&gt;
7346 &lt;span class=&quot;go&quot;&gt;  3.7.3/envs/proj2&lt;/span&gt;
7347 &lt;span class=&quot;go&quot;&gt;  proj1&lt;/span&gt;
7348 &lt;span class=&quot;go&quot;&gt;* proj2 (set by /Users/dhillard/proj2/.python-version)&lt;/span&gt;
7349 &lt;/pre&gt;&lt;/div&gt;
7350 
7351 &lt;p&gt;&lt;code&gt;pyenv&lt;/code&gt; and &lt;code&gt;pyenv-virtualenv&lt;/code&gt; have provided a particularly fluid workflow in my Python environment.&lt;/p&gt;
7352 &lt;h3 id=&quot;conda_1&quot;&gt;&lt;code&gt;conda&lt;/code&gt;&lt;/h3&gt;
7353 &lt;p&gt;You saw earlier that &lt;code&gt;conda&lt;/code&gt; treats environments, rather than Python versions, as the main method of working. &lt;a href=&quot;https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html&quot;&gt;&lt;code&gt;conda&lt;/code&gt; has built-in support for managing virtual environments&lt;/a&gt;:&lt;/p&gt;
7354 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;// Create virtual environment&lt;/span&gt;
7355 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; conda create --name my-env &lt;span class=&quot;nv&quot;&gt;python&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;.7.3
7356 
7357 &lt;span class=&quot;go&quot;&gt;// Activate virtual environment&lt;/span&gt;
7358 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; conda activate my-env
7359 
7360 &lt;span class=&quot;go&quot;&gt;// Exit virtual environment&lt;/span&gt;
7361 &lt;span class=&quot;gp&quot;&gt;(my-env)$&lt;/span&gt; conda deactivate
7362 &lt;/pre&gt;&lt;/div&gt;
7363 
7364 &lt;p&gt;&lt;code&gt;conda&lt;/code&gt; will install the specified version of Python if it isn&amp;rsquo;t already installed, so you don&amp;rsquo;t have to run &lt;code&gt;conda install python=3.7.3&lt;/code&gt; first.&lt;/p&gt;
7365 &lt;h3 id=&quot;pipenv&quot;&gt;&lt;code&gt;pipenv&lt;/code&gt;&lt;/h3&gt;
7366 &lt;p&gt;&lt;a href=&quot;https://docs.pipenv.org/en/latest/&quot;&gt;&lt;code&gt;pipenv&lt;/code&gt;&lt;/a&gt; is a relatively new tool that seeks to combine package management (more on this in a moment) with virtual environment management. It mostly abstracts the virtual environment management from you, which can be great as long as things go smoothly:&lt;/p&gt;
7367 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /Users/dhillard/myproj
7368 
7369 &lt;span class=&quot;go&quot;&gt;// Create virtual environment&lt;/span&gt;
7370 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pipenv install
7371 &lt;span class=&quot;go&quot;&gt;Creating a virtualenv for this projectโ€ฆ&lt;/span&gt;
7372 &lt;span class=&quot;go&quot;&gt;Pipfile: /Users/dhillard/myproj/Pipfile&lt;/span&gt;
7373 &lt;span class=&quot;go&quot;&gt;Using /path/to/pipenv/python3.7 (3.7.3) to create virtualenvโ€ฆ&lt;/span&gt;
7374 &lt;span class=&quot;go&quot;&gt;โœ” Successfully created virtual environment!&lt;/span&gt;
7375 &lt;span class=&quot;go&quot;&gt;Virtualenv location: /Users/dhillard/.local/share/virtualenvs/myproj-nAbMEAt0&lt;/span&gt;
7376 &lt;span class=&quot;go&quot;&gt;Creating a Pipfile for this projectโ€ฆ&lt;/span&gt;
7377 &lt;span class=&quot;go&quot;&gt;Pipfile.lock not found, creatingโ€ฆ&lt;/span&gt;
7378 &lt;span class=&quot;go&quot;&gt;Locking [dev-packages] dependenciesโ€ฆ&lt;/span&gt;
7379 &lt;span class=&quot;go&quot;&gt;Locking [packages] dependenciesโ€ฆ&lt;/span&gt;
7380 &lt;span class=&quot;go&quot;&gt;Updated Pipfile.lock (a65489)!&lt;/span&gt;
7381 &lt;span class=&quot;go&quot;&gt;Installing dependencies from Pipfile.lock (a65489)โ€ฆ&lt;/span&gt;
7382 &lt;span class=&quot;go&quot;&gt;  ๐Ÿ   โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰ 0/0 โ€” 00:00:00&lt;/span&gt;
7383 &lt;span class=&quot;go&quot;&gt;To activate this project&amp;#39;s virtualenv, run pipenv shell.&lt;/span&gt;
7384 &lt;span class=&quot;go&quot;&gt;Alternatively, run a command inside the virtualenv with pipenv run.&lt;/span&gt;
7385 
7386 &lt;span class=&quot;go&quot;&gt;// Activate virtual environment (uses a subshell)&lt;/span&gt;
7387 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pipenv shell
7388 &lt;span class=&quot;go&quot;&gt;Launching subshell in virtual environmentโ€ฆ&lt;/span&gt;
7389 &lt;span class=&quot;go&quot;&gt; . /Users/dhillard/.local/share/virtualenvs/test-nAbMEAt0/bin/activate&lt;/span&gt;
7390 
7391 &lt;span class=&quot;go&quot;&gt;// Exit virtual environment (by exiting subshell)&lt;/span&gt;
7392 &lt;span class=&quot;gp&quot;&gt;(myproj-nAbMEAt0)$&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;exit&lt;/span&gt;
7393 &lt;/pre&gt;&lt;/div&gt;
7394 
7395 &lt;p&gt;&lt;code&gt;pipenv&lt;/code&gt; does all the heavy lifting of creating a virtual environment and activating it for you. If you look carefully, you can see that it also creates a file called &lt;code&gt;Pipfile&lt;/code&gt;. After you first run &lt;code&gt;pipenv install&lt;/code&gt;, this file contains just a few things:&lt;/p&gt;
7396 &lt;div class=&quot;highlight ini&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;[[source]]&lt;/span&gt;
7397 &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;pypi&amp;quot;&lt;/span&gt;
7398 &lt;span class=&quot;na&quot;&gt;url&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;https://pypi.org/simple&amp;quot;&lt;/span&gt;
7399 &lt;span class=&quot;na&quot;&gt;verify_ssl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;true&lt;/span&gt;
7400 
7401 &lt;span class=&quot;k&quot;&gt;[dev-packages]&lt;/span&gt;
7402 
7403 &lt;span class=&quot;k&quot;&gt;[packages]&lt;/span&gt;
7404 
7405 &lt;span class=&quot;k&quot;&gt;[requires]&lt;/span&gt;
7406 &lt;span class=&quot;na&quot;&gt;python_version&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;3.7&amp;quot;&lt;/span&gt;
7407 &lt;/pre&gt;&lt;/div&gt;
7408 
7409 &lt;p&gt;In particular, note that it shows &lt;code&gt;python_version = &quot;3.7&quot;&lt;/code&gt;. By default, &lt;code&gt;pipenv&lt;/code&gt; creates a virtual Python environment using the same Python version it was installed under. If you want to use a different Python version, then you can create the &lt;code&gt;Pipfile&lt;/code&gt; yourself before running &lt;code&gt;pipenv install&lt;/code&gt; and specify the version you want. If you have &lt;code&gt;pyenv&lt;/code&gt; installed, then &lt;code&gt;pipenv&lt;/code&gt; will use it to install the specified Python version if necessary.&lt;/p&gt;
7410 &lt;p&gt;Abstracting virtual environment management is a noble goal of &lt;code&gt;pipenv&lt;/code&gt;, but it does get hung up with hard-to-read errors occasionally. Give it a try, but don&amp;rsquo;t worry if you feel confused or overwhelmed by it. The tool, documentation, and community will grow and improve around it as it matures.&lt;/p&gt;
7411 &lt;p&gt;To get an in-depth introduction to virtual environments, be sure to read &lt;a href=&quot;https://realpython.com/python-virtual-environments-a-primer&quot;&gt;Python Virtual Environments: A Primer&lt;/a&gt;.&lt;/p&gt;
7412 &lt;h2 id=&quot;package-management&quot;&gt;Package Management&lt;/h2&gt;
7413 &lt;p&gt;For many of the projects you work on, you&amp;rsquo;ll probably need some number of third-party packages. Those packages may have their own dependencies in turn. In the early days of Python, using packages involved manually downloading files and pointing Python at them. Today, we&amp;rsquo;re fortunate to have a variety of package management tools available to us.&lt;/p&gt;
7414 &lt;p&gt;Most package managers work in tandem with virtual environments, isolating the packages you install in one Python environment from another. Using the two together is where you really start to see the power of the tools available to you.&lt;/p&gt;
7415 &lt;h3 id=&quot;pip&quot;&gt;&lt;code&gt;pip&lt;/code&gt;&lt;/h3&gt;
7416 &lt;p&gt;&lt;code&gt;pip&lt;/code&gt; (&lt;strong&gt;p&lt;/strong&gt;ip &lt;strong&gt;i&lt;/strong&gt;nstalls &lt;strong&gt;p&lt;/strong&gt;ackages) has been the de facto standard for package management in Python for several years. It was heavily inspired by an earlier tool called &lt;code&gt;easy_install&lt;/code&gt;. Python incorporated &lt;code&gt;pip&lt;/code&gt; into the standard distribution starting in version 3.4. &lt;code&gt;pip&lt;/code&gt; automates the process of downloading packages and making Python aware of them.&lt;/p&gt;
7417 &lt;p&gt;If you have multiple virtual environments, then you can see that they&amp;rsquo;re isolated by installing a few packages in one:&lt;/p&gt;
7418 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pyenv virtualenv &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;.7.3 proj1
7419 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pyenv activate proj1
7420 &lt;span class=&quot;gp&quot;&gt;(proj1)$&lt;/span&gt; pip list
7421 &lt;span class=&quot;go&quot;&gt;Package    Version&lt;/span&gt;
7422 &lt;span class=&quot;go&quot;&gt;---------- ---------&lt;/span&gt;
7423 &lt;span class=&quot;go&quot;&gt;pip        19.1.1&lt;/span&gt;
7424 &lt;span class=&quot;go&quot;&gt;setuptools 40.8.0&lt;/span&gt;
7425 
7426 &lt;span class=&quot;gp&quot;&gt;(proj1)$&lt;/span&gt; python -m pip install requests
7427 &lt;span class=&quot;go&quot;&gt;Collecting requests&lt;/span&gt;
7428 &lt;span class=&quot;go&quot;&gt;  Downloading .../requests-2.22.0-py2.py3-none-any.whl (57kB)&lt;/span&gt;
7429 &lt;span class=&quot;go&quot;&gt;    100% |โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 61kB 2.2MB/s&lt;/span&gt;
7430 &lt;span class=&quot;go&quot;&gt;Collecting chardet&amp;lt;3.1.0,&amp;gt;=3.0.2 (from requests)&lt;/span&gt;
7431 &lt;span class=&quot;go&quot;&gt;  Downloading .../chardet-3.0.4-py2.py3-none-any.whl (133kB)&lt;/span&gt;
7432 &lt;span class=&quot;go&quot;&gt;    100% |โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 143kB 1.7MB/s&lt;/span&gt;
7433 &lt;span class=&quot;go&quot;&gt;Collecting certifi&amp;gt;=2017.4.17 (from requests)&lt;/span&gt;
7434 &lt;span class=&quot;go&quot;&gt;  Downloading .../certifi-2019.6.16-py2.py3-none-any.whl (157kB)&lt;/span&gt;
7435 &lt;span class=&quot;go&quot;&gt;    100% |โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 163kB 6.0MB/s&lt;/span&gt;
7436 &lt;span class=&quot;go&quot;&gt;Collecting urllib3!=1.25.0,!=1.25.1,&amp;lt;1.26,&amp;gt;=1.21.1 (from requests)&lt;/span&gt;
7437 &lt;span class=&quot;go&quot;&gt;  Downloading .../urllib3-1.25.3-py2.py3-none-any.whl (150kB)&lt;/span&gt;
7438 &lt;span class=&quot;go&quot;&gt;    100% |โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 153kB 1.7MB/s&lt;/span&gt;
7439 &lt;span class=&quot;go&quot;&gt;Collecting idna&amp;lt;2.9,&amp;gt;=2.5 (from requests)&lt;/span&gt;
7440 &lt;span class=&quot;go&quot;&gt;  Downloading .../idna-2.8-py2.py3-none-any.whl (58kB)&lt;/span&gt;
7441 &lt;span class=&quot;go&quot;&gt;    100% |โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 61kB 26.6MB/s&lt;/span&gt;
7442 &lt;span class=&quot;go&quot;&gt;Installing collected packages: chardet, certifi, urllib3, idna, requests&lt;/span&gt;
7443 &lt;span class=&quot;go&quot;&gt;Successfully installed packages&lt;/span&gt;
7444 
7445 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pip list
7446 &lt;span class=&quot;go&quot;&gt;Package    Version&lt;/span&gt;
7447 &lt;span class=&quot;go&quot;&gt;---------- ---------&lt;/span&gt;
7448 &lt;span class=&quot;go&quot;&gt;certifi    2019.6.16&lt;/span&gt;
7449 &lt;span class=&quot;go&quot;&gt;chardet    3.0.4&lt;/span&gt;
7450 &lt;span class=&quot;go&quot;&gt;idna       2.8&lt;/span&gt;
7451 &lt;span class=&quot;go&quot;&gt;pip        19.1.1&lt;/span&gt;
7452 &lt;span class=&quot;go&quot;&gt;requests   2.22.0&lt;/span&gt;
7453 &lt;span class=&quot;go&quot;&gt;setuptools 40.8.0&lt;/span&gt;
7454 &lt;span class=&quot;go&quot;&gt;urllib3    1.25.3&lt;/span&gt;
7455 &lt;/pre&gt;&lt;/div&gt;
7456 
7457 &lt;p&gt;&lt;code&gt;pip&lt;/code&gt; installed &lt;code&gt;requests&lt;/code&gt;, along with several packages it depends on. &lt;code&gt;pip list&lt;/code&gt; shows you all the currently installed packages and their versions.&lt;/p&gt;
7458 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
7459 &lt;p&gt;&lt;strong&gt;Warning&lt;/strong&gt;: You can uninstall packages using &lt;code&gt;pip uninstall requests&lt;/code&gt;, for example, but this will &lt;em&gt;only&lt;/em&gt; uninstall &lt;code&gt;requests&lt;/code&gt;&amp;mdash;not any of its dependencies.&lt;/p&gt;
7460 &lt;/div&gt;
7461 &lt;p&gt;A common way to specify project dependencies for &lt;code&gt;pip&lt;/code&gt; is with a &lt;code&gt;requirements.txt&lt;/code&gt; file. Each line in the file specifies a package name and, optionally, the version to install:&lt;/p&gt;
7462 &lt;div class=&quot;highlight pyreq&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scipy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
7463 &lt;span class=&quot;n&quot;&gt;requests&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.22&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
7464 &lt;/pre&gt;&lt;/div&gt;
7465 
7466 &lt;p&gt;You can then run &lt;code&gt;python -m pip install -r requirements.txt&lt;/code&gt; to install all of the specified dependencies at once. For more on &lt;code&gt;pip&lt;/code&gt;, see &lt;a href=&quot;https://realpython.com/what-is-pip/&quot;&gt;What is Pip? A Guide for New Pythonistas&lt;/a&gt;.&lt;/p&gt;
7467 &lt;h3 id=&quot;pipenv_1&quot;&gt;&lt;code&gt;pipenv&lt;/code&gt;&lt;/h3&gt;
7468 &lt;p&gt;&lt;a href=&quot;https://docs.pipenv.org/en/latest/&quot;&gt;&lt;code&gt;pipenv&lt;/code&gt;&lt;/a&gt; has most of the same basic operations as &lt;code&gt;pip&lt;/code&gt; but thinks about packages a bit differently. Remember the &lt;code&gt;Pipfile&lt;/code&gt; that &lt;code&gt;pipenv&lt;/code&gt; creates? When you install a package, &lt;code&gt;pipenv&lt;/code&gt; adds that package to &lt;code&gt;Pipfile&lt;/code&gt; and also adds more detailed information to a new &lt;strong&gt;lock file&lt;/strong&gt; called &lt;code&gt;Pipfile.lock&lt;/code&gt;. Lock files act as a snapshot of the precise set of packages installed, including direct dependencies as well as their sub-dependencies.&lt;/p&gt;
7469 &lt;p&gt;You can see &lt;code&gt;pipenv&lt;/code&gt; sorting out the package management when you install a package:&lt;/p&gt;
7470 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pipenv install requests
7471 &lt;span class=&quot;go&quot;&gt;Installing requestsโ€ฆ&lt;/span&gt;
7472 &lt;span class=&quot;go&quot;&gt;Adding requests to Pipfile&amp;#39;s [packages]โ€ฆ&lt;/span&gt;
7473 &lt;span class=&quot;go&quot;&gt;โœ” Installation Succeeded&lt;/span&gt;
7474 &lt;span class=&quot;go&quot;&gt;Pipfile.lock (444a6d) out of date, updating to (a65489)โ€ฆ&lt;/span&gt;
7475 &lt;span class=&quot;go&quot;&gt;Locking [dev-packages] dependenciesโ€ฆ&lt;/span&gt;
7476 &lt;span class=&quot;go&quot;&gt;Locking [packages] dependenciesโ€ฆ&lt;/span&gt;
7477 &lt;span class=&quot;go&quot;&gt;โœ” Success!&lt;/span&gt;
7478 &lt;span class=&quot;go&quot;&gt;Updated Pipfile.lock (444a6d)!&lt;/span&gt;
7479 &lt;span class=&quot;go&quot;&gt;Installing dependencies from Pipfile.lock (444a6d)โ€ฆ&lt;/span&gt;
7480 &lt;span class=&quot;go&quot;&gt;  ๐Ÿ   โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰โ–‰ 5/5 โ€” 00:00:00&lt;/span&gt;
7481 &lt;/pre&gt;&lt;/div&gt;
7482 
7483 &lt;p&gt;&lt;code&gt;pipenv&lt;/code&gt; will use this lock file, if present, to install the same set of packages. You can ensure that you always have the same set of working dependencies in any Python environment you create using this approach.&lt;/p&gt;
7484 &lt;p&gt;&lt;code&gt;pipenv&lt;/code&gt; also distinguishes between &lt;strong&gt;development dependencies&lt;/strong&gt; and &lt;strong&gt;production (regular) dependencies&lt;/strong&gt;. You may need some tools during development, such as &lt;a href=&quot;https://github.com/python/black&quot;&gt;&lt;code&gt;black&lt;/code&gt;&lt;/a&gt; or &lt;a href=&quot;http://flake8.pycqa.org/en/latest/&quot;&gt;&lt;code&gt;flake8&lt;/code&gt;&lt;/a&gt;, that you don&amp;rsquo;t need when you run your application in production. You can specify that a package is for development when you install it:&lt;/p&gt;
7485 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pipenv install --dev flake8
7486 &lt;span class=&quot;go&quot;&gt;Installing flake8โ€ฆ&lt;/span&gt;
7487 &lt;span class=&quot;go&quot;&gt;Adding flake8 to Pipfile&amp;#39;s [dev-packages]โ€ฆ&lt;/span&gt;
7488 &lt;span class=&quot;go&quot;&gt;โœ” Installation Succeeded&lt;/span&gt;
7489 &lt;span class=&quot;go&quot;&gt;...&lt;/span&gt;
7490 &lt;/pre&gt;&lt;/div&gt;
7491 
7492 &lt;p&gt;&lt;code&gt;pipenv install&lt;/code&gt; (without any arguments) will only install your production packages by default, but you can tell it to install development dependencies as well with &lt;code&gt;pipenv install --dev&lt;/code&gt;.&lt;/p&gt;
7493 &lt;h3 id=&quot;poetry&quot;&gt;&lt;code&gt;poetry&lt;/code&gt;&lt;/h3&gt;
7494 &lt;p&gt;&lt;a href=&quot;https://poetry.eustace.io&quot;&gt;&lt;code&gt;poetry&lt;/code&gt;&lt;/a&gt; addresses additional facets of package management, including creating and publishing your own packages. After installing &lt;code&gt;poetry&lt;/code&gt;, you can use it to create a new project:&lt;/p&gt;
7495 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; poetry new myproj
7496 &lt;span class=&quot;go&quot;&gt;Created package myproj in myproj&lt;/span&gt;
7497 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; ls myproj/
7498 &lt;span class=&quot;go&quot;&gt;README.rst    myproj    pyproject.toml    tests&lt;/span&gt;
7499 &lt;/pre&gt;&lt;/div&gt;
7500 
7501 &lt;p&gt;Similarly to how &lt;code&gt;pipenv&lt;/code&gt; creates the &lt;code&gt;Pipfile&lt;/code&gt;, &lt;code&gt;poetry&lt;/code&gt; creates a &lt;code&gt;pyproject.toml&lt;/code&gt; file. This &lt;a href=&quot;https://www.python.org/dev/peps/pep-0518/#file-format&quot;&gt;recent standard&lt;/a&gt; contains metadata about the project as well as dependency versions:&lt;/p&gt;
7502 &lt;div class=&quot;highlight ini&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;[tool.poetry]&lt;/span&gt;
7503 &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;myproj&amp;quot;&lt;/span&gt;
7504 &lt;span class=&quot;na&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;0.1.0&amp;quot;&lt;/span&gt;
7505 &lt;span class=&quot;na&quot;&gt;description&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;
7506 &lt;span class=&quot;na&quot;&gt;authors&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;[&amp;quot;Dane Hillard &amp;lt;github@danehillard.com&amp;gt;&amp;quot;]&lt;/span&gt;
7507 
7508 &lt;span class=&quot;k&quot;&gt;[tool.poetry.dependencies]&lt;/span&gt;
7509 &lt;span class=&quot;na&quot;&gt;python&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;^3.7&amp;quot;&lt;/span&gt;
7510 
7511 &lt;span class=&quot;k&quot;&gt;[tool.poetry.dev-dependencies]&lt;/span&gt;
7512 &lt;span class=&quot;na&quot;&gt;pytest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;^3.0&amp;quot;&lt;/span&gt;
7513 
7514 &lt;span class=&quot;k&quot;&gt;[build-system]&lt;/span&gt;
7515 &lt;span class=&quot;na&quot;&gt;requires&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;[&amp;quot;poetry&amp;gt;=0.12&amp;quot;]&lt;/span&gt;
7516 &lt;span class=&quot;na&quot;&gt;build-backend&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;poetry.masonry.api&amp;quot;&lt;/span&gt;
7517 &lt;/pre&gt;&lt;/div&gt;
7518 
7519 &lt;p&gt;You can install packages with &lt;code&gt;poetry add&lt;/code&gt; (or as development dependencies with &lt;code&gt;poetry add --dev&lt;/code&gt;):&lt;/p&gt;
7520 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; poetry add requests
7521 &lt;span class=&quot;go&quot;&gt;Using version ^2.22 for requests&lt;/span&gt;
7522 
7523 &lt;span class=&quot;go&quot;&gt;Updating dependencies&lt;/span&gt;
7524 &lt;span class=&quot;go&quot;&gt;Resolving dependencies... (0.2s)&lt;/span&gt;
7525 
7526 &lt;span class=&quot;go&quot;&gt;Writing lock file&lt;/span&gt;
7527 
7528 
7529 &lt;span class=&quot;go&quot;&gt;Package operations: 5 installs, 0 updates, 0 removals&lt;/span&gt;
7530 
7531 &lt;span class=&quot;go&quot;&gt;  - Installing certifi (2019.6.16)&lt;/span&gt;
7532 &lt;span class=&quot;go&quot;&gt;  - Installing chardet (3.0.4)&lt;/span&gt;
7533 &lt;span class=&quot;go&quot;&gt;  - Installing idna (2.8)&lt;/span&gt;
7534 &lt;span class=&quot;go&quot;&gt;  - Installing urllib3 (1.25.3)&lt;/span&gt;
7535 &lt;span class=&quot;go&quot;&gt;  - Installing requests (2.22.0)&lt;/span&gt;
7536 &lt;/pre&gt;&lt;/div&gt;
7537 
7538 &lt;p&gt;&lt;code&gt;poetry&lt;/code&gt; also maintains a lock file, and it has a benefit over &lt;code&gt;pipenv&lt;/code&gt; because it keeps track of which packages are subdependencies. As a result, you can uninstall &lt;code&gt;requests&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; its dependencies with &lt;code&gt;poetry remove requests&lt;/code&gt;.&lt;/p&gt;
7539 &lt;h3 id=&quot;conda_2&quot;&gt;&lt;code&gt;conda&lt;/code&gt;&lt;/h3&gt;
7540 &lt;p&gt;With &lt;code&gt;conda&lt;/code&gt;, you can use &lt;code&gt;pip&lt;/code&gt; to install packages as usual, but you can also use &lt;code&gt;conda install&lt;/code&gt; to install packages from different &lt;strong&gt;channels &lt;/strong&gt;, which are collections of packages provided by Anaconda or other providers. To install &lt;code&gt;requests&lt;/code&gt; from the &lt;code&gt;conda-forge&lt;/code&gt; channel, you can run &lt;code&gt;conda install -c conda-forge requests&lt;/code&gt;.&lt;/p&gt;
7541 &lt;p&gt;Learn more about package management in &lt;code&gt;conda&lt;/code&gt; in &lt;a href=&quot;https://realpython.com/python-windows-machine-learning-setup/&quot;&gt;Setting Up Python for Machine Learning on Windows&lt;/a&gt;.&lt;/p&gt;
7542 &lt;h2 id=&quot;python-interpreters&quot;&gt;Python Interpreters&lt;/h2&gt;
7543 &lt;p&gt;If you&amp;rsquo;re interested in further customization of your Python environment, you can choose the command line experience you have when interacting with Python. The Python interpreter provides a &lt;strong&gt;read-eval-print loop&lt;/strong&gt; (REPL), which is what comes up when you type &lt;code&gt;python&lt;/code&gt; with no arguments in your shell:&lt;/p&gt;
7544 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;Python 3.7.3 (default, Jun 17 2019, 14:09:05)&lt;/span&gt;
7545 &lt;span class=&quot;go&quot;&gt;[Clang 10.0.1 (clang-1001.0.46.4)] on darwin&lt;/span&gt;
7546 &lt;span class=&quot;go&quot;&gt;Type &amp;quot;help&amp;quot;, &amp;quot;copyright&amp;quot;, &amp;quot;credits&amp;quot; or &amp;quot;license&amp;quot; for more information.&lt;/span&gt;
7547 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
7548 &lt;span class=&quot;go&quot;&gt;4&lt;/span&gt;
7549 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
7550 &lt;/pre&gt;&lt;/div&gt;
7551 
7552 &lt;p&gt;The REPL &lt;strong&gt;reads&lt;/strong&gt; what you type, &lt;strong&gt;evaluates&lt;/strong&gt; it as Python code, and &lt;strong&gt;prints&lt;/strong&gt; the result. Then it waits to do it all over again. This is about as much as the default Python REPL provides, which is sufficient for a good portion of typical work.&lt;/p&gt;
7553 &lt;h3 id=&quot;ipython&quot;&gt;IPython&lt;/h3&gt;
7554 &lt;p&gt;Like Anaconda, &lt;a href=&quot;https://ipython.org/&quot;&gt;IPython&lt;/a&gt; is a suite of tools supporting more than just Python, but one of its main features is an alternative Python REPL. IPython&amp;rsquo;s REPL numbers each command and explicitly labels each command&amp;rsquo;s input and output. After installing IPython (&lt;code&gt;python -m pip install ipython&lt;/code&gt;), you can run the &lt;code&gt;ipython&lt;/code&gt; command in place of the &lt;code&gt;python&lt;/code&gt; command to use the IPython REPL:&lt;/p&gt;
7555 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;Python 3.7.3&lt;/span&gt;
7556 &lt;span class=&quot;go&quot;&gt;Type &amp;#39;copyright&amp;#39;, &amp;#39;credits&amp;#39; or &amp;#39;license&amp;#39; for more information&lt;/span&gt;
7557 &lt;span class=&quot;go&quot;&gt;IPython 6.0.0.dev -- An enhanced Interactive Python. Type &amp;#39;?&amp;#39; for help.&lt;/span&gt;
7558 
7559 &lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
7560 &lt;span class=&quot;gh&quot;&gt;Out[1]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;4&lt;/span&gt;
7561 
7562 &lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Hello!&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7563 &lt;span class=&quot;gh&quot;&gt;Out[2]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;Hello!&lt;/span&gt;
7564 &lt;/pre&gt;&lt;/div&gt;
7565 
7566 &lt;p&gt;IPython also supports &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-tab&quot;&gt;Tab&lt;/kbd&gt;&lt;/span&gt; completion, more powerful help features, and strong integration with other tooling such as &lt;a href=&quot;https://matplotlib.org/&quot;&gt;&lt;code&gt;matplotlib&lt;/code&gt;&lt;/a&gt; for graphing. IPython provided the foundation for &lt;a href=&quot;https://jupyter.org/&quot;&gt;Jupyter&lt;/a&gt;, and both have been used extensively in the data science community because of their integration with other tools.&lt;/p&gt;
7567 &lt;p&gt;The IPython REPL is &lt;a href=&quot;https://ipython.readthedocs.io/en/stable/config/intro.html&quot;&gt;highly configurable&lt;/a&gt; too, so while it falls just shy of being a full development environment, it can still be a boon to your productivity. Its built-in and customizable &lt;a href=&quot;https://ipython.org/ipython-doc/3/interactive/tutorial.html#magic-functions&quot;&gt;magic commands&lt;/a&gt; are worth checking out.&lt;/p&gt;
7568 &lt;h3 id=&quot;bpython&quot;&gt;&lt;code&gt;bpython&lt;/code&gt;&lt;/h3&gt;
7569 &lt;p&gt;&lt;a href=&quot;https://bpython-interpreter.org&quot;&gt;&lt;code&gt;bpython&lt;/code&gt;&lt;/a&gt; is another alternative REPL that provides inline syntax highlighting, tab completion, and even auto-suggestions as you type. It provides quite a few of the quick benefits of IPython without altering the interface much. Without the weight of the integrations and so on, &lt;code&gt;bpython&lt;/code&gt; might be good to add to your repertoire for a while to see how it improves your use of the REPL.&lt;/p&gt;
7570 &lt;h2 id=&quot;text-editors&quot;&gt;Text Editors&lt;/h2&gt;
7571 &lt;p&gt;You spend a third of your life sleeping, so it makes sense to invest in a great bed. As a developer, you spend a great deal of your time reading and writing code, so it follows that you should invest time in setting up your Python environment&amp;rsquo;s text editor just the way you like it.&lt;/p&gt;
7572 &lt;p&gt;Each editor offers a different set of key bindings and model for manipulating text. Some require a mouse to interact with them effectively, whereas others can be controlled with only the keyboard. Some people consider their choice of text editor and customizations some of the most personal decisions they make!&lt;/p&gt;
7573 &lt;p&gt;There are so many options to choose from in this arena, so I won&amp;rsquo;t attempt to cover it in detail here. Check out &lt;a href=&quot;https://realpython.com/python-ides-code-editors-guide/&quot;&gt;Python IDEs and Code Editors (Guide)&lt;/a&gt; for a broad overview. A good strategy is to find a simple, small text editor for quick changes and a full-featured IDE for more involved work. &lt;a href=&quot;https://www.vim.org/&quot;&gt;Vim&lt;/a&gt; and &lt;a href=&quot;https://www.jetbrains.com/pycharm/&quot;&gt;PyCharm&lt;/a&gt;, respectively, are my editors of choice.&lt;/p&gt;
7574 &lt;h2 id=&quot;python-environment-tips-and-tricks&quot;&gt;Python Environment Tips and Tricks&lt;/h2&gt;
7575 &lt;p&gt;Once you&amp;rsquo;ve made the big decisions about your Python environment, the rest of the road is paved with little tweaks to make your life a little easier. These tweaks each save minutes or seconds alone, but they collectively save you hours of time.&lt;/p&gt;
7576 &lt;p&gt;Making a certain activity easier reduces your cognitive load so you can focus on the task at hand instead of the logistics surrounding it. If you notice yourself performing an action over and over, then consider automating it. Use &lt;a href=&quot;https://xkcd.com/1205/&quot;&gt;this wonderful chart&lt;/a&gt; from XKCD to determine if it&amp;rsquo;s worth automating a particular task.&lt;/p&gt;
7577 &lt;p&gt;Here are a few final tips.&lt;/p&gt;
7578 &lt;p&gt;&lt;strong&gt;Know your current virtual environment&lt;/strong&gt;&lt;/p&gt;
7579 &lt;p&gt;As mentioned earlier, it&amp;rsquo;s a great idea to display the active Python version or virtual environment in your command prompt. Most tools will do this for you, but if not (or if you want to customize the prompt), the value is usually contained in the &lt;code&gt;VIRTUAL_ENV&lt;/code&gt; environment variable.&lt;/p&gt;
7580 &lt;p&gt;&lt;strong&gt;Disable unnecessary, temporary files&lt;/strong&gt;&lt;/p&gt;
7581 &lt;p&gt;Have you ever noticed &lt;code&gt;*.pyc&lt;/code&gt; files all over your project directories? These files are pre-compiled Python bytecode&amp;mdash;they help Python start your application faster. In production, these are a great idea because they&amp;rsquo;ll give you some performance gain. During local development, however, they&amp;rsquo;re rarely useful. Set &lt;code&gt;PYTHONDONTWRITEBYTECODE=1&lt;/code&gt; to disable this behavior. If you find use cases for them later, then you can easily remove this from your Python environment.&lt;/p&gt;
7582 &lt;p&gt;&lt;strong&gt;Customize your Python interpreter&lt;/strong&gt;&lt;/p&gt;
7583 &lt;p&gt;You can affect how the REPL behaves using a &lt;strong&gt;startup file&lt;/strong&gt;. Python will read this startup file and execute the code it contains before entering the REPL. Set the &lt;code&gt;PYTHONSTARTUP&lt;/code&gt; environment variable to the path of your startup file. (Mine&amp;rsquo;s at &lt;code&gt;~/.pystartup&lt;/code&gt;.) If you&amp;rsquo;d like to hit &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-arrow-up&quot;&gt;Up&lt;/kbd&gt;&lt;/span&gt; for command history and &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-tab&quot;&gt;Tab&lt;/kbd&gt;&lt;/span&gt; for completion like your shell provides, then give &lt;a href=&quot;https://github.com/daneah/dotfiles/blob/master/source/pystartup&quot;&gt;this startup file&lt;/a&gt; a try.&lt;/p&gt;
7584 &lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
7585 &lt;p&gt;You learned about many facets of the typical Python environment. Armed with this knowledge, you can:&lt;/p&gt;
7586 &lt;ul&gt;
7587 &lt;li&gt;Choose a terminal with the aesthetics and enhanced features you like&lt;/li&gt;
7588 &lt;li&gt;Choose a shell with as many (or as few) customization options as you need&lt;/li&gt;
7589 &lt;li&gt;Manage multiple versions of Python on your system&lt;/li&gt;
7590 &lt;li&gt;Manage multiple projects that use a single version of Python, using virtual Python environments&lt;/li&gt;
7591 &lt;li&gt;Install packages in your virtual environments&lt;/li&gt;
7592 &lt;li&gt;Choose a REPL that suits your interactive coding needs&lt;/li&gt;
7593 &lt;/ul&gt;
7594 &lt;p&gt;When you&amp;rsquo;ve got your Python environment just so, I hope you&amp;rsquo;ll share screenshots, screencasts, or blog posts about your perfect setup โœจ&lt;/p&gt;
7595         &lt;hr /&gt;
7596         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
7597       </content>
7598     </entry>
7599   
7600     <entry>
7601       <title>Traditional Face Detection With Python</title>
7602       <id>https://realpython.com/courses/traditional-face-detection-python/</id>
7603       <link href="https://realpython.com/courses/traditional-face-detection-python/"/>
7604       <updated>2019-08-13T14:00:00+00:00</updated>
7605       <summary>In this course on face detection with Python, you&#39;ll learn about a historically important algorithm for object detection that can be successfully applied to finding the location of a human face within an image.</summary>
7606       <content type="html">
7607         &lt;p&gt;&lt;strong&gt;Computer vision&lt;/strong&gt; is an exciting and growing field. There are tons of interesting problems to solve! One of them is face detection: the ability of a computer to recognize that a photograph contains a human face, and tell you where it is located. In this course, you&amp;rsquo;ll learn about &lt;strong&gt;face detection&lt;/strong&gt; with Python.&lt;/p&gt;
7608 &lt;p&gt;To detect any object in an image, it is necessary to understand how images are represented inside a computer, and how that object differs &lt;em&gt;visually&lt;/em&gt; from any other object.&lt;/p&gt;
7609 &lt;p&gt;Once that is done, the process of scanning an image and looking for those visual cues needs to be automated and optimized. All these steps come together to form a fast and reliable computer vision algorithm.&lt;/p&gt;
7610 &lt;p&gt;&lt;strong&gt;In this course, you&amp;rsquo;ll learn:&lt;/strong&gt;&lt;/p&gt;
7611 &lt;ul&gt;
7612 &lt;li&gt;What face detection is&lt;/li&gt;
7613 &lt;li&gt;How computers understand features in images&lt;/li&gt;
7614 &lt;li&gt;How to quickly analyze many different features to reach a decision&lt;/li&gt;
7615 &lt;li&gt;How to use a minimal Python solution for detecting human faces in images&lt;/li&gt;
7616 &lt;/ul&gt;
7617         &lt;hr /&gt;
7618         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
7619       </content>
7620     </entry>
7621   
7622     <entry>
7623       <title>Your Guide to the Python Print Function</title>
7624       <id>https://realpython.com/python-print/</id>
7625       <link href="https://realpython.com/python-print/"/>
7626       <updated>2019-08-12T14:00:00+00:00</updated>
7627       <summary>In this step-by-step tutorial, you&#39;ll learn about the print() function in Python and discover some of its lesser-known features. Avoid common mistakes, take your &quot;hello world&quot; to the next level, and know when to use a better alternative.</summary>
7628       <content type="html">
7629         &lt;p&gt;If you&amp;rsquo;re like most Python users, including me, then you probably started your Python journey by learning about &lt;code&gt;print()&lt;/code&gt;. It helped you write your very own &lt;code&gt;hello world&lt;/code&gt; one-liner. You can use it to display formatted messages onto the screen and perhaps find some bugs. But if you think that&amp;rsquo;s all there is to know about Python&amp;rsquo;s &lt;code&gt;print()&lt;/code&gt; function, then you&amp;rsquo;re missing out on a lot!&lt;/p&gt;
7630 &lt;p&gt;Keep reading to take full advantage of this seemingly boring and unappreciated little function. This tutorial will get you up to speed with using Python &lt;code&gt;print()&lt;/code&gt; effectively. However, prepare for a deep dive as you go through the sections. You may be surprised how much &lt;code&gt;print()&lt;/code&gt; has to offer!&lt;/p&gt;
7631 &lt;p&gt;&lt;strong&gt;By the end of this tutorial, you&amp;rsquo;ll know how to:&lt;/strong&gt;&lt;/p&gt;
7632 &lt;ul&gt;
7633 &lt;li&gt;Avoid common mistakes with Python&amp;rsquo;s &lt;code&gt;print()&lt;/code&gt;&lt;/li&gt;
7634 &lt;li&gt;Deal with newlines, character encodings, and buffering&lt;/li&gt;
7635 &lt;li&gt;Write text to files&lt;/li&gt;
7636 &lt;li&gt;Mock &lt;code&gt;print()&lt;/code&gt; in unit tests&lt;/li&gt;
7637 &lt;li&gt;Build advanced user interfaces in the terminal&lt;/li&gt;
7638 &lt;/ul&gt;
7639 &lt;p&gt;If you&amp;rsquo;re a complete beginner, then you&amp;rsquo;ll benefit most from reading the first part of this tutorial, which illustrates the essentials of printing in Python. Otherwise, feel free to skip that part and jump around as you see fit.&lt;/p&gt;
7640 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
7641 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;code&gt;print()&lt;/code&gt; was a major addition to Python 3, in which it replaced the old &lt;code&gt;print&lt;/code&gt; statement available in Python 2.&lt;/p&gt;
7642 &lt;p&gt;There were a number of good reasons for that, as you&amp;rsquo;ll see shortly. Although this tutorial focuses on Python 3, it does show the old way of printing in Python for reference.&lt;/p&gt;
7643 &lt;/div&gt;
7644 &lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Free Bonus:&lt;/strong&gt; &lt;a href=&quot;&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-cheat-sheet-experiment&quot; data-focus=&quot;false&quot;&gt;Click here to get our free Python Cheat Sheet&lt;/a&gt; that shows you the basics of Python 3, like working with data types, dictionaries, lists, and Python functions.&lt;/p&gt;&lt;/div&gt;
7645 
7646 &lt;h2 id=&quot;printing-in-a-nutshell&quot;&gt;Printing in a Nutshell&lt;/h2&gt;
7647 &lt;p&gt;Let&amp;rsquo;s jump in by looking at a few real-life examples of printing in Python. By the end of this section, you&amp;rsquo;ll know every possible way of calling &lt;code&gt;print()&lt;/code&gt;. Or, in programmer lingo, you&amp;rsquo;d say you&amp;rsquo;ll be familiar with the &lt;strong&gt;function signature&lt;/strong&gt;.&lt;/p&gt;
7648 &lt;h3 id=&quot;calling-print&quot;&gt;Calling Print&lt;/h3&gt;
7649 &lt;p&gt;The simplest example of using Python &lt;code&gt;print()&lt;/code&gt; requires just a few keystrokes:&lt;/p&gt;
7650 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
7651 &lt;/pre&gt;&lt;/div&gt;
7652 
7653 &lt;p&gt;You don&amp;rsquo;t pass any arguments, but you still need to put empty parentheses at the end, which tell Python to actually &lt;a href=&quot;https://realpython.com/lessons/example-function/&quot;&gt;execute the function&lt;/a&gt; rather than just refer to it by name.&lt;/p&gt;
7654 &lt;p&gt;This will produce an invisible newline character, which in turn will cause a blank line to appear on your screen. You can call &lt;code&gt;print()&lt;/code&gt; multiple times like this to add vertical space. It&amp;rsquo;s just as if you were hitting &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-enter&quot;&gt;Enter&lt;/kbd&gt;&lt;/span&gt; on your keyboard in a word processor.&lt;/p&gt;
7655 &lt;div class=&quot;card mb-3&quot; id=&quot;collapse_card5787b1&quot;&gt;
7656 &lt;div class=&quot;card-header border-0&quot;&gt;&lt;p class=&quot;m-0&quot;&gt;&lt;button class=&quot;btn&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapse5787b1&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapse5787b1&quot;&gt;Newline Character&lt;/button&gt; &lt;button class=&quot;btn btn-link float-right&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapse5787b1&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapse5787b1&quot;&gt;Show/Hide&lt;/button&gt;&lt;/p&gt;&lt;/div&gt;
7657 &lt;div id=&quot;collapse5787b1&quot; class=&quot;collapse&quot; data-parent=&quot;#collapse_card5787b1&quot;&gt;&lt;div class=&quot;card-body&quot; markdown=&quot;1&quot;&gt;
7658 
7659 &lt;p&gt;A &lt;strong&gt;newline character&lt;/strong&gt; is a special control character used to indicate the end of a line (EOL). It usually doesn&amp;rsquo;t have a visible representation on the screen, but some text editors can display such non-printable characters with little graphics.&lt;/p&gt;
7660 &lt;p&gt;The word &amp;ldquo;character&amp;rdquo; is somewhat of a misnomer in this case, because a newline is often more than one character long. For example, the Windows operating system, as well as the HTTP protocol, represent newlines with a pair of characters. Sometimes you need to take those differences into account to design truly portable programs.&lt;/p&gt;
7661 &lt;p&gt;To find out what constitutes a newline in your operating system, use Python&amp;rsquo;s built-in &lt;code&gt;os&lt;/code&gt; module.&lt;/p&gt;
7662 &lt;p&gt;This will immediately tell you that &lt;strong&gt;Windows&lt;/strong&gt; and &lt;strong&gt;DOS&lt;/strong&gt; represent the newline as a sequence of &lt;code&gt;\r&lt;/code&gt; followed by &lt;code&gt;\n&lt;/code&gt;:&lt;/p&gt;
7663 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
7664 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linesep&lt;/span&gt;
7665 &lt;span class=&quot;go&quot;&gt;&amp;#39;\r\n&amp;#39;&lt;/span&gt;
7666 &lt;/pre&gt;&lt;/div&gt;
7667 
7668 &lt;p&gt;On &lt;strong&gt;Unix&lt;/strong&gt;, &lt;strong&gt;Linux&lt;/strong&gt;, and recent versions of &lt;strong&gt;macOS&lt;/strong&gt;, it&amp;rsquo;s a single &lt;code&gt;\n&lt;/code&gt; character:&lt;/p&gt;
7669 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
7670 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linesep&lt;/span&gt;
7671 &lt;span class=&quot;go&quot;&gt;&amp;#39;\n&amp;#39;&lt;/span&gt;
7672 &lt;/pre&gt;&lt;/div&gt;
7673 
7674 &lt;p&gt;The classic &lt;strong&gt;Mac OS X&lt;/strong&gt;, however, sticks to its own &amp;ldquo;think different&amp;rdquo; philosophy by choosing yet another representation:&lt;/p&gt;
7675 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
7676 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linesep&lt;/span&gt;
7677 &lt;span class=&quot;go&quot;&gt;&amp;#39;\r&amp;#39;&lt;/span&gt;
7678 &lt;/pre&gt;&lt;/div&gt;
7679 
7680 &lt;p&gt;Notice how these characters appear in string literals. They use special syntax with a preceding backslash (&lt;code&gt;\&lt;/code&gt;) to denote the start of an &lt;strong&gt;escape character sequence&lt;/strong&gt;. Such sequences allow for representing control characters, which would be otherwise invisible on screen.&lt;/p&gt;
7681 &lt;p&gt;Most programming languages come with a predefined set of escape sequences for special characters such as these:&lt;/p&gt;
7682 &lt;ul&gt;
7683 &lt;li&gt;&lt;strong&gt;&lt;code&gt;\\&lt;/code&gt;:&lt;/strong&gt; backslash&lt;/li&gt;
7684 &lt;li&gt;&lt;strong&gt;&lt;code&gt;\b&lt;/code&gt;:&lt;/strong&gt; backspace&lt;/li&gt;
7685 &lt;li&gt;&lt;strong&gt;&lt;code&gt;\t&lt;/code&gt;:&lt;/strong&gt; tab&lt;/li&gt;
7686 &lt;li&gt;&lt;strong&gt;&lt;code&gt;\r&lt;/code&gt;:&lt;/strong&gt; carriage return (CR)&lt;/li&gt;
7687 &lt;li&gt;&lt;strong&gt;&lt;code&gt;\n&lt;/code&gt;:&lt;/strong&gt; newline, also known as line feed (LF)&lt;/li&gt;
7688 &lt;/ul&gt;
7689 &lt;p&gt;The last two are reminiscent of mechanical typewriters, which required two separate commands to insert a newline. The first command would move the carriage back to the beginning of the current line, while the second one would advance the roll to the next line.&lt;/p&gt;
7690 &lt;p&gt;By comparing the corresponding &lt;strong&gt;ASCII character codes&lt;/strong&gt;, you&amp;rsquo;ll see that putting a backslash in front of a character changes its meaning completely. However, not all characters allow for this&amp;ndash;only the special ones.&lt;/p&gt;
7691 &lt;p&gt;To compare ASCII character codes, you may want to use the built-in &lt;code&gt;ord()&lt;/code&gt; function:&lt;/p&gt;
7692 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;ord&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;r&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7693 &lt;span class=&quot;go&quot;&gt;114&lt;/span&gt;
7694 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;ord&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7695 &lt;span class=&quot;go&quot;&gt;13&lt;/span&gt;
7696 &lt;/pre&gt;&lt;/div&gt;
7697 
7698 &lt;p&gt;Keep in mind that, in order to form a correct escape sequence, there must be no space between the backslash character and a letter!&lt;/p&gt;
7699 &lt;/div&gt;&lt;/div&gt;
7700 
7701 &lt;/div&gt;
7702 &lt;p&gt;As you just saw, calling &lt;code&gt;print()&lt;/code&gt; without arguments results in a &lt;strong&gt;blank line&lt;/strong&gt;, which is a line comprised solely of the newline character. Don&amp;rsquo;t confuse this with an &lt;strong&gt;empty line&lt;/strong&gt;, which doesn&amp;rsquo;t contain any characters at all, not even the newline!&lt;/p&gt;
7703 &lt;p&gt;You can use Python&amp;rsquo;s &lt;a href=&quot;https://realpython.com/python-strings/&quot;&gt;string&lt;/a&gt; literals to visualize these two:&lt;/p&gt;
7704 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Blank line&lt;/span&gt;
7705 &lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Empty line&lt;/span&gt;
7706 &lt;/pre&gt;&lt;/div&gt;
7707 
7708 &lt;p&gt;The first one is one character long, whereas the second one has no content.&lt;/p&gt;
7709 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
7710 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; To remove the newline character from a string in Python, use its &lt;code&gt;.rstrip()&lt;/code&gt; method, like this:&lt;/p&gt;
7711 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;A line of text.&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rstrip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
7712 &lt;span class=&quot;go&quot;&gt;&amp;#39;A line of text.&amp;#39;&lt;/span&gt;
7713 &lt;/pre&gt;&lt;/div&gt;
7714 
7715 &lt;p&gt;This strips any trailing whitespace from the right edge of the string of characters.&lt;/p&gt;
7716 &lt;/div&gt;
7717 &lt;p&gt;In a more common scenario, you&amp;rsquo;d want to communicate some message to the end user. There are a few ways to achieve this.&lt;/p&gt;
7718 &lt;p&gt;First, you may pass a string literal directly to &lt;code&gt;print()&lt;/code&gt;:&lt;/p&gt;
7719 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Please wait while the program is loading...&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7720 &lt;/pre&gt;&lt;/div&gt;
7721 
7722 &lt;p&gt;This will print the message verbatim onto the screen.&lt;/p&gt;
7723 &lt;div class=&quot;card mb-3&quot; id=&quot;collapse_cardf395ab&quot;&gt;
7724 &lt;div class=&quot;card-header border-0&quot;&gt;&lt;p class=&quot;m-0&quot;&gt;&lt;button class=&quot;btn&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapsef395ab&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapsef395ab&quot;&gt;String Literals&lt;/button&gt; &lt;button class=&quot;btn btn-link float-right&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapsef395ab&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapsef395ab&quot;&gt;Show/Hide&lt;/button&gt;&lt;/p&gt;&lt;/div&gt;
7725 &lt;div id=&quot;collapsef395ab&quot; class=&quot;collapse&quot; data-parent=&quot;#collapse_cardf395ab&quot;&gt;&lt;div class=&quot;card-body&quot; markdown=&quot;1&quot;&gt;
7726 
7727 &lt;p&gt;&lt;strong&gt;String literals&lt;/strong&gt; in Python can be enclosed either in single quotes (&lt;code&gt;&#39;&lt;/code&gt;) or  double quotes (&lt;code&gt;&quot;&lt;/code&gt;). According to the official &lt;a href=&quot;https://www.python.org/dev/peps/pep-0008/#string-quotes&quot;&gt;PEP 8&lt;/a&gt; style guide, you should just pick one and keep using it consistently. There&amp;rsquo;s no difference, unless you need to nest one in another.&lt;/p&gt;
7728 &lt;p&gt;For example, you can&amp;rsquo;t use double quotes for the literal and also include double quotes inside of it, because that&amp;rsquo;s ambiguous for the Python interpreter:&lt;/p&gt;
7729 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;My favorite book is &amp;quot;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Python&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Tricks&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Wrong!&lt;/span&gt;
7730 &lt;/pre&gt;&lt;/div&gt;
7731 
7732 &lt;p&gt;What you want to do is enclose the text, which contains double quotes, within single quotes:&lt;/p&gt;
7733 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;My favorite book is &amp;quot;Python Tricks&amp;quot;&amp;#39;&lt;/span&gt;
7734 &lt;/pre&gt;&lt;/div&gt;
7735 
7736 &lt;p&gt;The same trick would work the other way around:&lt;/p&gt;
7737 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;My favorite book is &amp;#39;Python Tricks&amp;#39;&amp;quot;&lt;/span&gt;
7738 &lt;/pre&gt;&lt;/div&gt;
7739 
7740 &lt;p&gt;Alternatively, you could use escape character sequences mentioned earlier, to make Python treat those internal double quotes literally as part of the string literal:&lt;/p&gt;
7741 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;My favorite book is &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Python Tricks&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;
7742 &lt;/pre&gt;&lt;/div&gt;
7743 
7744 &lt;p&gt;Escaping is fine and dandy, but it can sometimes get in the way. Specifically, when you need your string to contain relatively many backslash characters in literal form.&lt;/p&gt;
7745 &lt;p&gt;One classic example is a file path on Windows:&lt;/p&gt;
7746 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;C:\Users\jdoe&amp;#39;&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Wrong!&lt;/span&gt;
7747 &lt;span class=&quot;s1&quot;&gt;&amp;#39;C:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Users&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;jdoe&amp;#39;&lt;/span&gt;
7748 &lt;/pre&gt;&lt;/div&gt;
7749 
7750 &lt;p&gt;Notice how each backslash character needs to be escaped with yet another backslash.&lt;/p&gt;
7751 &lt;p&gt;This is even more prominent with regular expressions, which quickly get convoluted due to the heavy use of special characters:&lt;/p&gt;
7752 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;^&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;w:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\\\&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;(?:(?:(?:[^&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\\\&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;]+)?|(?:[^&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\\\&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;]+)&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\\\&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;[^&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\\\&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;]+)*)$&amp;#39;&lt;/span&gt;
7753 &lt;/pre&gt;&lt;/div&gt;
7754 
7755 &lt;p&gt;Fortunately, you can turn off character escaping entirely with the help of raw-string literals. Simply prepend an &lt;code&gt;r&lt;/code&gt; or &lt;code&gt;R&lt;/code&gt; before the opening quote, and now you end up with this:&lt;/p&gt;
7756 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;C:\Users\jdoe&amp;#39;&lt;/span&gt;
7757 &lt;span class=&quot;sa&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;^\w:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;(?:(?:(?:[^&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;]+)?|(?:[^&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;]+)&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;[^&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;]+)*)$&amp;#39;&lt;/span&gt;
7758 &lt;/pre&gt;&lt;/div&gt;
7759 
7760 &lt;p&gt;That&amp;rsquo;s much better, isn&amp;rsquo;t it?&lt;/p&gt;
7761 &lt;p&gt;There are a few more prefixes that give special meaning to string literals in Python, but you won&amp;rsquo;t get into them here.&lt;/p&gt;
7762 &lt;p&gt;Lastly, you can define multi-line string literals by enclosing them between &lt;code&gt;&#39;&#39;&#39;&lt;/code&gt; or &lt;code&gt;&quot;&quot;&quot;&lt;/code&gt;, which are often used as &lt;strong&gt;docstrings&lt;/strong&gt;.&lt;/p&gt;
7763 &lt;p&gt;Here&amp;rsquo;s an example:&lt;/p&gt;
7764 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
7765 &lt;span class=&quot;sd&quot;&gt;This is an example&lt;/span&gt;
7766 &lt;span class=&quot;sd&quot;&gt;of a multi-line string&lt;/span&gt;
7767 &lt;span class=&quot;sd&quot;&gt;in Python.&lt;/span&gt;
7768 &lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
7769 &lt;/pre&gt;&lt;/div&gt;
7770 
7771 &lt;p&gt;To prevent an initial newline, simply put the text right after the opening &lt;code&gt;&quot;&quot;&quot;&lt;/code&gt;:&lt;/p&gt;
7772 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;This is an example&lt;/span&gt;
7773 &lt;span class=&quot;sd&quot;&gt;of a multi-line string&lt;/span&gt;
7774 &lt;span class=&quot;sd&quot;&gt;in Python.&lt;/span&gt;
7775 &lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
7776 &lt;/pre&gt;&lt;/div&gt;
7777 
7778 &lt;p&gt;You can also use a backslash to get rid of the newline:&lt;/p&gt;
7779 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;\&lt;/span&gt;
7780 &lt;span class=&quot;sd&quot;&gt;This is an example&lt;/span&gt;
7781 &lt;span class=&quot;sd&quot;&gt;of a multi-line string&lt;/span&gt;
7782 &lt;span class=&quot;sd&quot;&gt;in Python.&lt;/span&gt;
7783 &lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt; 
7784 &lt;/pre&gt;&lt;/div&gt;
7785 
7786 &lt;p&gt;To remove indentation from a multi-line string, you might take advantage of the built-in &lt;code&gt;textwrap&lt;/code&gt; module:&lt;/p&gt;
7787 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;textwrap&lt;/span&gt;
7788 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paragraph&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&amp;#39;&lt;/span&gt;
7789 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;    This is an example&lt;/span&gt;
7790 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;    of a multi-line string&lt;/span&gt;
7791 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;    in Python.&lt;/span&gt;
7792 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;    &amp;#39;&amp;#39;&amp;#39;&lt;/span&gt;
7793 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
7794 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paragraph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7795 
7796 &lt;span class=&quot;go&quot;&gt;    This is an example&lt;/span&gt;
7797 &lt;span class=&quot;go&quot;&gt;    of a multi-line string&lt;/span&gt;
7798 &lt;span class=&quot;go&quot;&gt;    in Python.&lt;/span&gt;
7799 
7800 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;textwrap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dedent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paragraph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
7801 &lt;span class=&quot;go&quot;&gt;This is an example&lt;/span&gt;
7802 &lt;span class=&quot;go&quot;&gt;of a multi-line string&lt;/span&gt;
7803 &lt;span class=&quot;go&quot;&gt;in Python.&lt;/span&gt;
7804 &lt;/pre&gt;&lt;/div&gt;
7805 
7806 &lt;p&gt;This will take care of unindenting paragraphs for you. There are also a few other useful functions in &lt;code&gt;textwrap&lt;/code&gt; for text alignment you&amp;rsquo;d find in a word processor.&lt;/p&gt;
7807 &lt;/div&gt;&lt;/div&gt;
7808 
7809 &lt;/div&gt;
7810 &lt;p&gt;Secondly, you could extract that message into its own variable with a meaningful name to enhance readability and promote code reuse:&lt;/p&gt;
7811 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Please wait while the program is loading...&amp;#39;&lt;/span&gt;
7812 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7813 &lt;/pre&gt;&lt;/div&gt;
7814 
7815 &lt;p&gt;Lastly, you could pass an expression, like &lt;a href=&quot;https://realpython.com/lessons/concatenating-joining-strings-python/&quot;&gt;string concatenation&lt;/a&gt;, to be evaluated before printing the result:&lt;/p&gt;
7816 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
7817 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello, &amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getlogin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;! How are you?&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7818 &lt;span class=&quot;go&quot;&gt;Hello, jdoe! How are you?&lt;/span&gt;
7819 &lt;/pre&gt;&lt;/div&gt;
7820 
7821 &lt;p&gt;In fact, there are a dozen ways to format messages in Python. I highly encourage you to take a look at &lt;a href=&quot;https://realpython.com/python-f-strings/&quot;&gt;f-strings&lt;/a&gt;, introduced in Python 3.6, because they offer the most concise syntax of them all:&lt;/p&gt;
7822 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
7823 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello, {os.getlogin()}! How are you?&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7824 &lt;/pre&gt;&lt;/div&gt;
7825 
7826 &lt;p&gt;Moreover, f-strings will prevent you from making a common mistake, which is forgetting to type cast concatenated operands. Python is a strongly typed language, which means it won&amp;rsquo;t allow you to do this:&lt;/p&gt;
7827 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;My age is &amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;
7828 &lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
7829   File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;input&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
7830     &lt;span class=&quot;s1&quot;&gt;&amp;#39;My age is &amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;
7831 &lt;span class=&quot;gr&quot;&gt;TypeError&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;can only concatenate str (not &amp;quot;int&amp;quot;) to str&lt;/span&gt;
7832 &lt;/pre&gt;&lt;/div&gt;
7833 
7834 &lt;p&gt;That&amp;rsquo;s wrong because adding numbers to strings doesn&amp;rsquo;t make sense. You need to explicitly convert the number to string first, in order to join them together:&lt;/p&gt;
7835 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;My age is &amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7836 &lt;span class=&quot;go&quot;&gt;&amp;#39;My age is 42&amp;#39;&lt;/span&gt;
7837 &lt;/pre&gt;&lt;/div&gt;
7838 
7839 &lt;p&gt;Unless you &lt;a href=&quot;https://realpython.com/courses/python-exceptions-101/&quot;&gt;handle such errors&lt;/a&gt; yourself, the Python interpreter will let you know about a problem by showing a &lt;a href=&quot;https://realpython.com/python-traceback/&quot;&gt;traceback&lt;/a&gt;.&lt;/p&gt;
7840 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
7841 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;code&gt;str()&lt;/code&gt; is a global built-in function that converts an object into its string representation.&lt;/p&gt;
7842 &lt;p&gt;You can call it directly on any object, for example, a number:&lt;/p&gt;
7843 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;3.14&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7844 &lt;span class=&quot;go&quot;&gt;&amp;#39;3.14&amp;#39;&lt;/span&gt;
7845 &lt;/pre&gt;&lt;/div&gt;
7846 
7847 &lt;p&gt;Built-in data types have a predefined string representation out of the box, but later in this article, you&amp;rsquo;ll find out how to provide one for your custom classes.&lt;/p&gt;
7848 &lt;/div&gt;
7849 &lt;p&gt;As with any function, it doesn&amp;rsquo;t matter whether you pass a literal, a variable, or an expression. Unlike many other functions, however, &lt;code&gt;print()&lt;/code&gt; will accept anything regardless of its type.&lt;/p&gt;
7850 &lt;p&gt;So far, you only looked at the string, but how about other data types? Let&amp;rsquo;s try literals of different built-in types and see what comes out:&lt;/p&gt;
7851 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;                            &lt;span class=&quot;c1&quot;&gt;# &amp;lt;class &amp;#39;int&amp;#39;&amp;gt;&lt;/span&gt;
7852 &lt;span class=&quot;go&quot;&gt;42&lt;/span&gt;
7853 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;3.14&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;                          &lt;span class=&quot;c1&quot;&gt;# &amp;lt;class &amp;#39;float&amp;#39;&amp;gt;&lt;/span&gt;
7854 &lt;span class=&quot;go&quot;&gt;3.14&lt;/span&gt;
7855 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;                        &lt;span class=&quot;c1&quot;&gt;# &amp;lt;class &amp;#39;complex&amp;#39;&amp;gt;&lt;/span&gt;
7856 &lt;span class=&quot;go&quot;&gt;(1+2j)&lt;/span&gt;
7857 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;                          &lt;span class=&quot;c1&quot;&gt;# &amp;lt;class &amp;#39;bool&amp;#39;&amp;gt;&lt;/span&gt;
7858 &lt;span class=&quot;go&quot;&gt;True&lt;/span&gt;
7859 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;                     &lt;span class=&quot;c1&quot;&gt;# &amp;lt;class &amp;#39;list&amp;#39;&amp;gt;&lt;/span&gt;
7860 &lt;span class=&quot;go&quot;&gt;[1, 2, 3]&lt;/span&gt;
7861 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;                     &lt;span class=&quot;c1&quot;&gt;# &amp;lt;class &amp;#39;tuple&amp;#39;&amp;gt;&lt;/span&gt;
7862 &lt;span class=&quot;go&quot;&gt;(1, 2, 3)&lt;/span&gt;
7863 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;red&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;green&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;blue&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;# &amp;lt;class &amp;#39;set&amp;#39;&amp;gt;&lt;/span&gt;
7864 &lt;span class=&quot;go&quot;&gt;{&amp;#39;red&amp;#39;, &amp;#39;green&amp;#39;, &amp;#39;blue&amp;#39;}&lt;/span&gt;
7865 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Alice&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;age&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# &amp;lt;class &amp;#39;dict&amp;#39;&amp;gt;&lt;/span&gt;
7866 &lt;span class=&quot;go&quot;&gt;{&amp;#39;name&amp;#39;: &amp;#39;Alice&amp;#39;, &amp;#39;age&amp;#39;: 42}&lt;/span&gt;
7867 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hello&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;                       &lt;span class=&quot;c1&quot;&gt;# &amp;lt;class &amp;#39;str&amp;#39;&amp;gt;&lt;/span&gt;
7868 &lt;span class=&quot;go&quot;&gt;hello&lt;/span&gt;
7869 &lt;/pre&gt;&lt;/div&gt;
7870 
7871 &lt;p&gt;Watch out for the &lt;code&gt;None&lt;/code&gt; constant, though. Despite being used to indicate an absence of a value, it will show up as &lt;code&gt;&#39;None&#39;&lt;/code&gt; rather than an empty string:&lt;/p&gt;
7872 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7873 &lt;span class=&quot;go&quot;&gt;None&lt;/span&gt;
7874 &lt;/pre&gt;&lt;/div&gt;
7875 
7876 &lt;p&gt;How does &lt;code&gt;print()&lt;/code&gt; know how to work with all these different types? Well, the short answer is that it doesn&amp;rsquo;t. It implicitly calls &lt;code&gt;str()&lt;/code&gt; behind the scenes to type cast any object into a string. Afterward, it treats strings in a uniform way.&lt;/p&gt;
7877 &lt;p&gt;Later in this tutorial, you&amp;rsquo;ll learn how to use this mechanism for printing custom data types such as your classes.&lt;/p&gt;
7878 &lt;p&gt;Okay, you&amp;rsquo;re now able to call &lt;code&gt;print()&lt;/code&gt; with a single argument or without any arguments. You know how to print fixed or formatted messages onto the screen. The next subsection will expand on message formatting a little bit.&lt;/p&gt;
7879 &lt;div class=&quot;card mb-3&quot; id=&quot;collapse_card09ba8e&quot;&gt;
7880 &lt;div class=&quot;card-header border-0&quot;&gt;&lt;p class=&quot;m-0&quot;&gt;&lt;button class=&quot;btn&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapse09ba8e&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapse09ba8e&quot;&gt;Syntax in Python 2&lt;/button&gt; &lt;button class=&quot;btn btn-link float-right&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapse09ba8e&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapse09ba8e&quot;&gt;Show/Hide&lt;/button&gt;&lt;/p&gt;&lt;/div&gt;
7881 &lt;div id=&quot;collapse09ba8e&quot; class=&quot;collapse&quot; data-parent=&quot;#collapse_card09ba8e&quot;&gt;&lt;div class=&quot;card-body&quot; markdown=&quot;1&quot;&gt;
7882 
7883 &lt;p&gt;To achieve the same result in the previous language generation, you&amp;rsquo;d normally want to drop the parentheses enclosing the text:&lt;/p&gt;
7884 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Python 2&lt;/span&gt;
7885 &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;
7886 &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Please wait...&amp;#39;&lt;/span&gt;
7887 &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;! How are you?&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getlogin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
7888 &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;. Your age is &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%d&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;.&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7889 &lt;/pre&gt;&lt;/div&gt;
7890 
7891 &lt;p&gt;That&amp;rsquo;s because &lt;code&gt;print&lt;/code&gt; wasn&amp;rsquo;t a function back then, as you&amp;rsquo;ll see in the &lt;a href=&quot;#understanding-python-print&quot;&gt;next section&lt;/a&gt;. Note, however, that in some cases parentheses in Python are redundant. It wouldn&amp;rsquo;t harm to include them as they&amp;rsquo;d just get ignored. Does that mean you should be using the &lt;code&gt;print&lt;/code&gt; statement as if it were a function? Absolutely not!&lt;/p&gt;
7892 &lt;p&gt;For example, parentheses enclosing a single expression or a literal are optional. Both instructions produce the same result in Python 2:&lt;/p&gt;
7893 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Python 2&lt;/span&gt;
7894 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Please wait...&amp;#39;&lt;/span&gt;
7895 &lt;span class=&quot;go&quot;&gt;Please wait...&lt;/span&gt;
7896 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Please wait...&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7897 &lt;span class=&quot;go&quot;&gt;Please wait...&lt;/span&gt;
7898 &lt;/pre&gt;&lt;/div&gt;
7899 
7900 &lt;p&gt;Round brackets are actually part of the expression rather than the &lt;code&gt;print&lt;/code&gt; statement. If your expression happens to contain only one item, then it&amp;rsquo;s as if you didn&amp;rsquo;t include the brackets at all.&lt;/p&gt;
7901 &lt;p&gt;On the other hand, putting parentheses around multiple items forms a &lt;a href=&quot;https://realpython.com/python-lists-tuples/#python-tuples&quot;&gt;tuple&lt;/a&gt;:&lt;/p&gt;
7902 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Python 2&lt;/span&gt;
7903 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;My name is&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;John&amp;#39;&lt;/span&gt;
7904 &lt;span class=&quot;go&quot;&gt;My name is John&lt;/span&gt;
7905 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;My name is&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;John&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7906 &lt;span class=&quot;go&quot;&gt;(&amp;#39;My name is&amp;#39;, &amp;#39;John&amp;#39;)&lt;/span&gt;
7907 &lt;/pre&gt;&lt;/div&gt;
7908 
7909 &lt;p&gt;This is a known source of confusion. In fact, you&amp;rsquo;d also get a tuple by appending a trailing comma to the only item surrounded by parentheses:&lt;/p&gt;
7910 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Python 2&lt;/span&gt;
7911 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Please wait...&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7912 &lt;span class=&quot;go&quot;&gt;Please wait...&lt;/span&gt;
7913 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Please wait...&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Notice the comma&lt;/span&gt;
7914 &lt;span class=&quot;go&quot;&gt;(&amp;#39;Please wait...&amp;#39;,)&lt;/span&gt;
7915 &lt;/pre&gt;&lt;/div&gt;
7916 
7917 &lt;p&gt;The bottom line is that you shouldn&amp;rsquo;t call &lt;code&gt;print&lt;/code&gt; with brackets in Python 2. Although, to be completely accurate, you can work around this with the help of a &lt;code&gt;__future__&lt;/code&gt; import, which you&amp;rsquo;ll read more about in the relevant section.&lt;/p&gt;
7918 &lt;/div&gt;&lt;/div&gt;
7919 
7920 &lt;/div&gt;
7921 &lt;h3 id=&quot;separating-multiple-arguments&quot;&gt;Separating Multiple Arguments&lt;/h3&gt;
7922 &lt;p&gt;You saw &lt;code&gt;print()&lt;/code&gt; called without any arguments to produce a blank line and then called with a single argument to display either a fixed or a formatted message.&lt;/p&gt;
7923 &lt;p&gt;However, it turns out that this function can accept any number of &lt;strong&gt;positional arguments&lt;/strong&gt;, including zero, one, or more arguments. That&amp;rsquo;s very handy in a common case of message formatting, where you&amp;rsquo;d want to join a few elements together.&lt;/p&gt;
7924 &lt;div class=&quot;card mb-3&quot; id=&quot;collapse_card9620b8&quot;&gt;
7925 &lt;div class=&quot;card-header border-0&quot;&gt;&lt;p class=&quot;m-0&quot;&gt;&lt;button class=&quot;btn&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapse9620b8&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapse9620b8&quot;&gt;Positional Arguments&lt;/button&gt; &lt;button class=&quot;btn btn-link float-right&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapse9620b8&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapse9620b8&quot;&gt;Show/Hide&lt;/button&gt;&lt;/p&gt;&lt;/div&gt;
7926 &lt;div id=&quot;collapse9620b8&quot; class=&quot;collapse&quot; data-parent=&quot;#collapse_card9620b8&quot;&gt;&lt;div class=&quot;card-body&quot; markdown=&quot;1&quot;&gt;
7927 
7928 &lt;p&gt;Arguments can be passed to a function in one of several ways. One way is by explicitly naming the arguments when you&amp;rsquo;re calling the function, like this:&lt;/p&gt;
7929 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
7930 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
7931 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
7932 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7933 &lt;span class=&quot;go&quot;&gt;0.75&lt;/span&gt;
7934 &lt;/pre&gt;&lt;/div&gt;
7935 
7936 &lt;p&gt;Since arguments can be uniquely identified by name, their order doesn&amp;rsquo;t matter. Swapping them out will still give the same result:&lt;/p&gt;
7937 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7938 &lt;span class=&quot;go&quot;&gt;0.75&lt;/span&gt;
7939 &lt;/pre&gt;&lt;/div&gt;
7940 
7941 &lt;p&gt;Conversely, arguments passed without names are identified by their position. That&amp;rsquo;s why &lt;strong&gt;positional arguments&lt;/strong&gt; need to follow strictly the order imposed by the function signature:&lt;/p&gt;
7942 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7943 &lt;span class=&quot;go&quot;&gt;0.75&lt;/span&gt;
7944 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7945 &lt;span class=&quot;go&quot;&gt;1.3333333333333333&lt;/span&gt;
7946 &lt;/pre&gt;&lt;/div&gt;
7947 
7948 &lt;p&gt;&lt;code&gt;print()&lt;/code&gt; allows an &lt;a href=&quot;https://docs.python.org/dev/tutorial/controlflow.html#arbitrary-argument-lists&quot;&gt;arbitrary number of positional arguments&lt;/a&gt; thanks to the &lt;code&gt;*args&lt;/code&gt; parameter.&lt;/p&gt;
7949 &lt;/div&gt;&lt;/div&gt;
7950 
7951 &lt;/div&gt;
7952 &lt;p&gt;Let&amp;rsquo;s have a look at this example:&lt;/p&gt;
7953 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
7954 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;My name is&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getlogin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;and I am&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7955 &lt;span class=&quot;go&quot;&gt;My name is jdoe and I am 42&lt;/span&gt;
7956 &lt;/pre&gt;&lt;/div&gt;
7957 
7958 &lt;p&gt;&lt;code&gt;print()&lt;/code&gt; concatenated all four arguments passed to it, and it inserted a single space between them so that you didn&amp;rsquo;t end up with a squashed message like &lt;code&gt;&#39;My name isjdoeand I am42&#39;&lt;/code&gt;.&lt;/p&gt;
7959 &lt;p&gt;Notice that it also took care of proper type casting by implicitly calling &lt;code&gt;str()&lt;/code&gt; on each argument before joining them together. If you recall from the previous subsection, a naรฏve concatenation may easily result in an error due to incompatible types:&lt;/p&gt;
7960 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;My age is: &amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7961 &lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
7962   File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;input&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
7963     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;My age is: &amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7964 &lt;span class=&quot;gr&quot;&gt;TypeError&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;can only concatenate str (not &amp;quot;int&amp;quot;) to str&lt;/span&gt;
7965 &lt;/pre&gt;&lt;/div&gt;
7966 
7967 &lt;p&gt;Apart from accepting a variable number of positional arguments, &lt;code&gt;print()&lt;/code&gt; defines four named or &lt;strong&gt;keyword arguments&lt;/strong&gt;, which are optional since they all have default values. You can view their brief documentation by calling &lt;code&gt;help(print)&lt;/code&gt; from the interactive interpreter.&lt;/p&gt;
7968 &lt;p&gt;Let&amp;rsquo;s focus on &lt;code&gt;sep&lt;/code&gt; just for now. It stands for &lt;strong&gt;separator&lt;/strong&gt; and is assigned a single space (&lt;code&gt;&#39; &#39;&lt;/code&gt;) by default. It determines the value to join elements with.&lt;/p&gt;
7969 &lt;p&gt;It has to be either a string or &lt;code&gt;None&lt;/code&gt;, but the latter has the same effect as the default space:&lt;/p&gt;
7970 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hello&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;world&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7971 &lt;span class=&quot;go&quot;&gt;hello world&lt;/span&gt;
7972 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hello&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;world&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39; &amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7973 &lt;span class=&quot;go&quot;&gt;hello world&lt;/span&gt;
7974 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hello&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;world&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7975 &lt;span class=&quot;go&quot;&gt;hello world&lt;/span&gt;
7976 &lt;/pre&gt;&lt;/div&gt;
7977 
7978 &lt;p&gt;If you wanted to suppress the separator completely, you&amp;rsquo;d have to pass an empty string (&lt;code&gt;&#39;&#39;&lt;/code&gt;) instead:&lt;/p&gt;
7979 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hello&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;world&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7980 &lt;span class=&quot;go&quot;&gt;helloworld&lt;/span&gt;
7981 &lt;/pre&gt;&lt;/div&gt;
7982 
7983 &lt;p&gt;You may want &lt;code&gt;print()&lt;/code&gt; to join its arguments as separate lines. In that case, simply pass the escaped newline character described earlier:&lt;/p&gt;
7984 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hello&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;world&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7985 &lt;span class=&quot;go&quot;&gt;hello&lt;/span&gt;
7986 &lt;span class=&quot;go&quot;&gt;world&lt;/span&gt;
7987 &lt;/pre&gt;&lt;/div&gt;
7988 
7989 &lt;p&gt;A more useful example of the &lt;code&gt;sep&lt;/code&gt; parameter would be printing something like file paths:&lt;/p&gt;
7990 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;home&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;user&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;documents&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;/&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7991 &lt;span class=&quot;go&quot;&gt;home/user/documents&lt;/span&gt;
7992 &lt;/pre&gt;&lt;/div&gt;
7993 
7994 &lt;p&gt;Remember that the separator comes between the elements, not around them, so you need to account for that in one way or another:&lt;/p&gt;
7995 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;/home&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;user&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;documents&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;/&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7996 &lt;span class=&quot;go&quot;&gt;/home/user/documents&lt;/span&gt;
7997 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;home&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;user&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;documents&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;/&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
7998 &lt;span class=&quot;go&quot;&gt;/home/user/documents&lt;/span&gt;
7999 &lt;/pre&gt;&lt;/div&gt;
8000 
8001 &lt;p&gt;Specifically, you can insert a slash character (&lt;code&gt;/&lt;/code&gt;) into the first positional argument, or use an empty string as the first argument to enforce the leading slash.&lt;/p&gt;
8002 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
8003 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Be careful about joining elements of a list or tuple.&lt;/p&gt;
8004 &lt;p&gt;Doing it manually will result in a well-known &lt;code&gt;TypeError&lt;/code&gt; if at least one of the elements isn&amp;rsquo;t a string:&lt;/p&gt;
8005 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39; &amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;jdoe is&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;years old&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
8006 &lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
8007   File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;input&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
8008     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;,&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;jdoe is&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;years old&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
8009 &lt;span class=&quot;gr&quot;&gt;TypeError&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;sequence item 1: expected str instance, int found&lt;/span&gt;
8010 &lt;/pre&gt;&lt;/div&gt;
8011 
8012 &lt;p&gt;It&amp;rsquo;s safer to just unpack the sequence with the star operator (&lt;code&gt;*&lt;/code&gt;) and let &lt;code&gt;print()&lt;/code&gt; handle type casting:&lt;/p&gt;
8013 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;jdoe is&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;years old&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
8014 &lt;span class=&quot;go&quot;&gt;jdoe is 42 years old&lt;/span&gt;
8015 &lt;/pre&gt;&lt;/div&gt;
8016 
8017 &lt;p&gt;Unpacking is effectively the same as calling &lt;code&gt;print()&lt;/code&gt; with individual elements of the list.&lt;/p&gt;
8018 &lt;/div&gt;
8019 &lt;p&gt;One more interesting example could be exporting data to a &lt;a href=&quot;https://realpython.com/courses/reading-and-writing-csv-files/&quot;&gt;comma-separated values&lt;/a&gt; (CSV) format:&lt;/p&gt;
8020 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Python Tricks&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Dan Bader&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;,&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8021 &lt;span class=&quot;go&quot;&gt;1,Python Tricks,Dan Bader&lt;/span&gt;
8022 &lt;/pre&gt;&lt;/div&gt;
8023 
8024 &lt;p&gt;This wouldn&amp;rsquo;t handle edge cases such as escaping commas correctly, but for simple use cases, it should do. The line above would show up in your terminal window. In order to save it to a file, you&amp;rsquo;d have to redirect the output. Later in this section, you&amp;rsquo;ll see how to use &lt;code&gt;print()&lt;/code&gt; to write text to files straight from Python.&lt;/p&gt;
8025 &lt;p&gt;Finally, the &lt;code&gt;sep&lt;/code&gt; parameter isn&amp;rsquo;t constrained to a single character only. You can join elements with strings of any length:&lt;/p&gt;
8026 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;node&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;child&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;child&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39; -&amp;gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8027 &lt;span class=&quot;go&quot;&gt;node -&amp;gt; child -&amp;gt; child&lt;/span&gt;
8028 &lt;/pre&gt;&lt;/div&gt;
8029 
8030 &lt;p&gt;In the upcoming subsections, you&amp;rsquo;ll explore the remaining keyword arguments of the &lt;code&gt;print()&lt;/code&gt; function.&lt;/p&gt;
8031 &lt;div class=&quot;card mb-3&quot; id=&quot;collapse_cardab50a5&quot;&gt;
8032 &lt;div class=&quot;card-header border-0&quot;&gt;&lt;p class=&quot;m-0&quot;&gt;&lt;button class=&quot;btn&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapseab50a5&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapseab50a5&quot;&gt;Syntax in Python 2&lt;/button&gt; &lt;button class=&quot;btn btn-link float-right&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapseab50a5&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapseab50a5&quot;&gt;Show/Hide&lt;/button&gt;&lt;/p&gt;&lt;/div&gt;
8033 &lt;div id=&quot;collapseab50a5&quot; class=&quot;collapse&quot; data-parent=&quot;#collapse_cardab50a5&quot;&gt;&lt;div class=&quot;card-body&quot; markdown=&quot;1&quot;&gt;
8034 
8035 &lt;p&gt;To print multiple elements in Python 2, you must drop the parentheses around them, just like before:&lt;/p&gt;
8036 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Python 2&lt;/span&gt;
8037 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
8038 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;My name is&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getlogin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;and I am&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;
8039 &lt;span class=&quot;go&quot;&gt;My name is jdoe and I am 42&lt;/span&gt;
8040 &lt;/pre&gt;&lt;/div&gt;
8041 
8042 &lt;p&gt;If you kept them, on the other hand, you&amp;rsquo;d be passing a single tuple element to the &lt;code&gt;print&lt;/code&gt; statement:&lt;/p&gt;
8043 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Python 2&lt;/span&gt;
8044 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
8045 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;My name is&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getlogin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;and I am&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8046 &lt;span class=&quot;go&quot;&gt;(&amp;#39;My name is&amp;#39;, &amp;#39;jdoe&amp;#39;, &amp;#39;and I am&amp;#39;, 42)&lt;/span&gt;
8047 &lt;/pre&gt;&lt;/div&gt;
8048 
8049 &lt;p&gt;Moreover, there&amp;rsquo;s no way of altering the default separator of joined elements in Python 2, so one workaround is to use string interpolation like so:&lt;/p&gt;
8050 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Python 2&lt;/span&gt;
8051 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;
8052 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;My name is &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; and I am &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%d&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getlogin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8053 &lt;span class=&quot;go&quot;&gt;My name is jdoe and I am 42&lt;/span&gt;
8054 &lt;/pre&gt;&lt;/div&gt;
8055 
8056 &lt;p&gt;That was the default way of formatting strings until the &lt;code&gt;.format()&lt;/code&gt; method got backported from Python 3.&lt;/p&gt;
8057 &lt;/div&gt;&lt;/div&gt;
8058 
8059 &lt;/div&gt;
8060 &lt;h3 id=&quot;preventing-line-breaks&quot;&gt;Preventing Line Breaks&lt;/h3&gt;
8061 &lt;p&gt;Sometimes you don&amp;rsquo;t want to end your message with a trailing newline so that subsequent calls to &lt;code&gt;print()&lt;/code&gt; will continue on the same line. Classic examples include updating the progress of a long-running operation or prompting the user for input. In the latter case, you want the user to type in the answer on the same line:&lt;/p&gt;
8062 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;Are you sure you want to do this? [y/n] y
8063 &lt;/pre&gt;&lt;/div&gt;
8064 
8065 &lt;p&gt;Many programming languages expose functions similar to &lt;code&gt;print()&lt;/code&gt; through their standard libraries, but they let you decide whether to add a newline or not. For example, in Java and C#, you have two distinct functions, while other languages require you to explicitly append &lt;code&gt;\n&lt;/code&gt; at the end of a string literal.&lt;/p&gt;
8066 &lt;p&gt;Here are a few examples of syntax in such languages:&lt;/p&gt;
8067 &lt;div class=&quot;table-responsive&quot;&gt;
8068 &lt;table class=&quot;table table-hover&quot;&gt;
8069 &lt;thead&gt;
8070 &lt;tr&gt;
8071 &lt;th&gt;Language&lt;/th&gt;
8072 &lt;th&gt;Example&lt;/th&gt;
8073 &lt;/tr&gt;
8074 &lt;/thead&gt;
8075 &lt;tbody&gt;
8076 &lt;tr&gt;
8077 &lt;td&gt;Perl&lt;/td&gt;
8078 &lt;td&gt;&lt;code&gt;print &quot;hello world\n&quot;&lt;/code&gt;&lt;/td&gt;
8079 &lt;/tr&gt;
8080 &lt;tr&gt;
8081 &lt;td&gt;C&lt;/td&gt;
8082 &lt;td&gt;&lt;code&gt;printf(&quot;hello world\n&quot;);&lt;/code&gt;&lt;/td&gt;
8083 &lt;/tr&gt;
8084 &lt;tr&gt;
8085 &lt;td&gt;C++&lt;/td&gt;
8086 &lt;td&gt;&lt;code&gt;std::cout &amp;lt;&amp;lt; &quot;hello world&quot; &amp;lt;&amp;lt; std::endl;&lt;/code&gt;&lt;/td&gt;
8087 &lt;/tr&gt;
8088 &lt;/tbody&gt;
8089 &lt;/table&gt;
8090 &lt;/div&gt;
8091 &lt;p&gt;In contrast, Python&amp;rsquo;s &lt;code&gt;print()&lt;/code&gt; function always adds &lt;code&gt;\n&lt;/code&gt; without asking, because that&amp;rsquo;s what you want in most cases. To disable it, you can take advantage of yet another keyword argument, &lt;code&gt;end&lt;/code&gt;, which dictates what to end the line with.&lt;/p&gt;
8092 &lt;p&gt;In terms of semantics, the &lt;code&gt;end&lt;/code&gt; parameter is almost identical to the &lt;code&gt;sep&lt;/code&gt; one that you saw earlier:&lt;/p&gt;
8093 &lt;ul&gt;
8094 &lt;li&gt;It must be a string or &lt;code&gt;None&lt;/code&gt;.&lt;/li&gt;
8095 &lt;li&gt;It can be arbitrarily long.&lt;/li&gt;
8096 &lt;li&gt;It has a default value of &lt;code&gt;&#39;\n&#39;&lt;/code&gt;.&lt;/li&gt;
8097 &lt;li&gt;If equal to &lt;code&gt;None&lt;/code&gt;, it&amp;rsquo;ll have the same effect as the default value.&lt;/li&gt;
8098 &lt;li&gt;If equal to an empty string (&lt;code&gt;&#39;&#39;&lt;/code&gt;), it&amp;rsquo;ll suppress the newline.&lt;/li&gt;
8099 &lt;/ul&gt;
8100 &lt;p&gt;Now you understand what&amp;rsquo;s happening under the hood when you&amp;rsquo;re calling &lt;code&gt;print()&lt;/code&gt; without arguments. Since you don&amp;rsquo;t provide any positional arguments to the function, there&amp;rsquo;s nothing to be joined, and so the default separator isn&amp;rsquo;t used at all. However, the default value of &lt;code&gt;end&lt;/code&gt; still applies, and a blank line shows up.&lt;/p&gt;
8101 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
8102 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You may be wondering why the &lt;code&gt;end&lt;/code&gt; parameter has a fixed default value rather than whatever makes sense on your operating system.&lt;/p&gt;
8103 &lt;p&gt;Well, you don&amp;rsquo;t have to worry about newline representation across different operating systems when printing, because &lt;code&gt;print()&lt;/code&gt; will handle the conversion automatically. Just remember to always use the &lt;code&gt;\n&lt;/code&gt; escape sequence in string literals.&lt;/p&gt;
8104 &lt;p&gt;This is currently the most portable way of printing a newline character in Python:&lt;/p&gt;
8105 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;line1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;line2&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;line3&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8106 &lt;span class=&quot;go&quot;&gt;line1&lt;/span&gt;
8107 &lt;span class=&quot;go&quot;&gt;line2&lt;/span&gt;
8108 &lt;span class=&quot;go&quot;&gt;line3&lt;/span&gt;
8109 &lt;/pre&gt;&lt;/div&gt;
8110 
8111 &lt;p&gt;If you were to try to forcefully print a Windows-specific newline character on a Linux machine, for example, you&amp;rsquo;d end up with broken output:&lt;/p&gt;
8112 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;line1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\r\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;line2&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\r\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;line3&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8113 
8114 
8115 &lt;span class=&quot;go&quot;&gt;line3&lt;/span&gt;
8116 &lt;/pre&gt;&lt;/div&gt;
8117 
8118 &lt;p&gt;On the flip side, when you open a file for reading with &lt;code&gt;open()&lt;/code&gt;, you don&amp;rsquo;t need to care about newline representation either. The function will translate any system-specific newline it encounters into a universal &lt;code&gt;&#39;\n&#39;&lt;/code&gt;. At the same time, you have control over how the newlines should be treated both on input and output if you really need that.&lt;/p&gt;
8119 &lt;/div&gt;
8120 &lt;p&gt;To disable the newline, you must specify an empty string through the &lt;code&gt;end&lt;/code&gt; keyword argument:&lt;/p&gt;
8121 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Checking file integrity...&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8122 &lt;span class=&quot;c1&quot;&gt;# (...)&lt;/span&gt;
8123 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;ok&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8124 &lt;/pre&gt;&lt;/div&gt;
8125 
8126 &lt;p&gt;Even though these are two separate &lt;code&gt;print()&lt;/code&gt; calls, which can execute a long time apart, you&amp;rsquo;ll eventually see only one line. First, it&amp;rsquo;ll look like this:&lt;/p&gt;
8127 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;Checking file integrity...
8128 &lt;/pre&gt;&lt;/div&gt;
8129 
8130 &lt;p&gt;However, after the second call to &lt;code&gt;print()&lt;/code&gt;, the same line will appear on the screen as:&lt;/p&gt;
8131 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;Checking file integrity...ok
8132 &lt;/pre&gt;&lt;/div&gt;
8133 
8134 &lt;p&gt;As with &lt;code&gt;sep&lt;/code&gt;, you can use &lt;code&gt;end&lt;/code&gt; to join individual pieces into a big blob of text with a custom separator. Instead of joining multiple arguments, however, it&amp;rsquo;ll append text from each function call to the same line:&lt;/p&gt;
8135 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;The first sentence&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;. &amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8136 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;The second sentence&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;. &amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8137 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;The last sentence.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8138 &lt;/pre&gt;&lt;/div&gt;
8139 
8140 &lt;p&gt;These three instructions will output a single line of text:&lt;/p&gt;
8141 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;The first sentence. The second sentence. The last sentence.
8142 &lt;/pre&gt;&lt;/div&gt;
8143 
8144 &lt;p&gt;You can mix the two keyword arguments:&lt;/p&gt;
8145 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Mercury&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Venus&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Earth&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;, &amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;, &amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8146 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Mars&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Jupiter&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Saturn&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;, &amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;, &amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8147 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Uranus&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Neptune&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Pluto&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;, &amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8148 &lt;/pre&gt;&lt;/div&gt;
8149 
8150 &lt;p&gt;Not only do you get a single line of text, but all items are separated with a comma:&lt;/p&gt;
8151 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto
8152 &lt;/pre&gt;&lt;/div&gt;
8153 
8154 &lt;p&gt;There&amp;rsquo;s nothing to stop you from using the newline character with some extra padding around it:&lt;/p&gt;
8155 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Printing in a Nutshell&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; * &amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8156 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Calling Print&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; * &amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8157 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Separating Multiple Arguments&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; * &amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8158 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Preventing Line Breaks&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8159 &lt;/pre&gt;&lt;/div&gt;
8160 
8161 &lt;p&gt;It would print out the following piece of text:&lt;/p&gt;
8162 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;Printing in a Nutshell
8163  * Calling Print
8164  * Separating Multiple Arguments
8165  * Preventing Line Breaks
8166 &lt;/pre&gt;&lt;/div&gt;
8167 
8168 &lt;p&gt;As you can see, the &lt;code&gt;end&lt;/code&gt; keyword argument will accept arbitrary strings.&lt;/p&gt;
8169 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
8170 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Looping over lines in a text file preserves their own newline characters, which combined with the &lt;code&gt;print()&lt;/code&gt; function&amp;rsquo;s default behavior will result in a redundant newline character:&lt;/p&gt;
8171 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;file.txt&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
8172 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
8173 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8174 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
8175 &lt;span class=&quot;go&quot;&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod&lt;/span&gt;
8176 
8177 &lt;span class=&quot;go&quot;&gt;tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,&lt;/span&gt;
8178 
8179 &lt;span class=&quot;go&quot;&gt;quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo&lt;/span&gt;
8180 &lt;/pre&gt;&lt;/div&gt;
8181 
8182 &lt;p&gt;There are two newlines after each line of text. You want to strip one of the them, as shown earlier in this article, before printing the line:&lt;/p&gt;
8183 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rstrip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
8184 &lt;/pre&gt;&lt;/div&gt;
8185 
8186 &lt;p&gt;Alternatively, you can keep the newline in the content but suppress the one appended by &lt;code&gt;print()&lt;/code&gt; automatically. You&amp;rsquo;d use the &lt;code&gt;end&lt;/code&gt; keyword argument to do that:&lt;/p&gt;
8187 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;file.txt&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
8188 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
8189 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8190 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
8191 &lt;span class=&quot;go&quot;&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod&lt;/span&gt;
8192 &lt;span class=&quot;go&quot;&gt;tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,&lt;/span&gt;
8193 &lt;span class=&quot;go&quot;&gt;quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo&lt;/span&gt;
8194 &lt;/pre&gt;&lt;/div&gt;
8195 
8196 &lt;p&gt;By ending a line with an empty string, you effectively disable one of the newlines.&lt;/p&gt;
8197 &lt;/div&gt;
8198 &lt;p&gt;You&amp;rsquo;re getting more acquainted with printing in Python, but there&amp;rsquo;s still a lot of useful information ahead. In the upcoming subsection, you&amp;rsquo;ll learn how to intercept and redirect the &lt;code&gt;print()&lt;/code&gt; function&amp;rsquo;s output.&lt;/p&gt;
8199 &lt;div class=&quot;card mb-3&quot; id=&quot;collapse_card37ad87&quot;&gt;
8200 &lt;div class=&quot;card-header border-0&quot;&gt;&lt;p class=&quot;m-0&quot;&gt;&lt;button class=&quot;btn&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapse37ad87&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapse37ad87&quot;&gt;Syntax in Python 2&lt;/button&gt; &lt;button class=&quot;btn btn-link float-right&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapse37ad87&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapse37ad87&quot;&gt;Show/Hide&lt;/button&gt;&lt;/p&gt;&lt;/div&gt;
8201 &lt;div id=&quot;collapse37ad87&quot; class=&quot;collapse&quot; data-parent=&quot;#collapse_card37ad87&quot;&gt;&lt;div class=&quot;card-body&quot; markdown=&quot;1&quot;&gt;
8202 
8203 &lt;p&gt;Preventing a line break in Python 2 requires that you append a trailing comma to the expression:&lt;/p&gt;
8204 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;hello world&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
8205 &lt;/pre&gt;&lt;/div&gt;
8206 
8207 &lt;p&gt;However, that&amp;rsquo;s not ideal because it also adds an unwanted space, which would translate to &lt;code&gt;end=&#39; &#39;&lt;/code&gt; instead of &lt;code&gt;end=&#39;&#39;&lt;/code&gt; in Python 3. You can test this with the following code snippet:&lt;/p&gt;
8208 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;BEFORE&amp;#39;&lt;/span&gt;
8209 &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;hello&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
8210 &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;AFTER&amp;#39;&lt;/span&gt;
8211 &lt;/pre&gt;&lt;/div&gt;
8212 
8213 &lt;p&gt;Notice there&amp;rsquo;s a space between the words &lt;code&gt;hello&lt;/code&gt; and &lt;code&gt;AFTER&lt;/code&gt;:&lt;/p&gt;
8214 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;BEFORE
8215 hello AFTER
8216 &lt;/pre&gt;&lt;/div&gt;
8217 
8218 &lt;p&gt;In order to get the expected result, you&amp;rsquo;d need to use one of the tricks explained later, which is either importing the &lt;code&gt;print()&lt;/code&gt; function from &lt;code&gt;__future__&lt;/code&gt; or falling back to the &lt;code&gt;sys&lt;/code&gt; module:&lt;/p&gt;
8219 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
8220 &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;BEFORE&amp;#39;&lt;/span&gt;
8221 &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stdout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hello&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8222 &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;AFTER&amp;#39;&lt;/span&gt; 
8223 &lt;/pre&gt;&lt;/div&gt;
8224 
8225 &lt;p&gt;This will print the correct output without extra space:&lt;/p&gt;
8226 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;BEFORE
8227 helloAFTER
8228 &lt;/pre&gt;&lt;/div&gt;
8229 
8230 &lt;p&gt;While using the &lt;code&gt;sys&lt;/code&gt; module gives you control over what gets printed to the standard output, the code becomes a little bit more cluttered.&lt;/p&gt;
8231 &lt;/div&gt;&lt;/div&gt;
8232 
8233 &lt;/div&gt;
8234 &lt;h3 id=&quot;printing-to-a-file&quot;&gt;Printing to a File&lt;/h3&gt;
8235 &lt;p&gt;Believe it or not, &lt;code&gt;print()&lt;/code&gt; doesn&amp;rsquo;t know how to turn messages into text on your screen, and frankly it doesn&amp;rsquo;t need to. That&amp;rsquo;s a job for lower-level layers of code, which understand bytes and know how to push them around.&lt;/p&gt;
8236 &lt;p&gt;&lt;code&gt;print()&lt;/code&gt; is an abstraction over these layers, providing a convenient interface that merely delegates the actual printing to a stream or &lt;strong&gt;file-like object&lt;/strong&gt;. A stream can be any file on your disk, a network socket, or perhaps an in-memory buffer.&lt;/p&gt;
8237 &lt;p&gt;In addition to this, there are three standard streams provided by the operating system:&lt;/p&gt;
8238 &lt;ol&gt;
8239 &lt;li&gt;&lt;strong&gt;&lt;code&gt;stdin&lt;/code&gt;:&lt;/strong&gt; standard input&lt;/li&gt;
8240 &lt;li&gt;&lt;strong&gt;&lt;code&gt;stdout&lt;/code&gt;:&lt;/strong&gt; standard output&lt;/li&gt;
8241 &lt;li&gt;&lt;strong&gt;&lt;code&gt;stderr&lt;/code&gt;:&lt;/strong&gt; standard error&lt;/li&gt;
8242 &lt;/ol&gt;
8243 &lt;div class=&quot;card mb-3&quot; id=&quot;collapse_cardd994b2&quot;&gt;
8244 &lt;div class=&quot;card-header border-0&quot;&gt;&lt;p class=&quot;m-0&quot;&gt;&lt;button class=&quot;btn&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapsed994b2&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapsed994b2&quot;&gt;Standard Streams&lt;/button&gt; &lt;button class=&quot;btn btn-link float-right&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapsed994b2&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapsed994b2&quot;&gt;Show/Hide&lt;/button&gt;&lt;/p&gt;&lt;/div&gt;
8245 &lt;div id=&quot;collapsed994b2&quot; class=&quot;collapse&quot; data-parent=&quot;#collapse_cardd994b2&quot;&gt;&lt;div class=&quot;card-body&quot; markdown=&quot;1&quot;&gt;
8246 
8247 &lt;p&gt;&lt;strong&gt;Standard output&lt;/strong&gt; is what you see in the terminal when you run various command-line programs including your own &lt;a href=&quot;https://realpython.com/run-python-scripts/&quot;&gt;Python scripts&lt;/a&gt;:&lt;/p&gt;
8248 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; cat hello.py 
8249 &lt;span class=&quot;go&quot;&gt;print(&amp;#39;This will appear on stdout&amp;#39;)&lt;/span&gt;
8250 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python hello.py
8251 &lt;span class=&quot;go&quot;&gt;This will appear on stdout&lt;/span&gt;
8252 &lt;/pre&gt;&lt;/div&gt;
8253 
8254 &lt;p&gt;Unless otherwise instructed, &lt;code&gt;print()&lt;/code&gt; will default to writing to standard output. However, you can tell your operating system to temporarily swap out &lt;code&gt;stdout&lt;/code&gt; for a file stream, so that any output ends up in that file rather than the screen:&lt;/p&gt;
8255 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python hello.py &amp;gt; file.txt
8256 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; cat file.txt
8257 &lt;span class=&quot;go&quot;&gt;This will appear on stdout&lt;/span&gt;
8258 &lt;/pre&gt;&lt;/div&gt;
8259 
8260 &lt;p&gt;That&amp;rsquo;s called stream redirection.&lt;/p&gt;
8261 &lt;p&gt;The standard error is similar to &lt;code&gt;stdout&lt;/code&gt; in that it also shows up on the screen. Nonetheless, it&amp;rsquo;s a separate stream, whose purpose is to log error messages for diagnostics. By redirecting one or both of them, you can keep things clean.&lt;/p&gt;
8262 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
8263 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; To redirect &lt;code&gt;stderr&lt;/code&gt;, you need to know about &lt;strong&gt;file descriptors&lt;/strong&gt;, also known as &lt;strong&gt;file handles&lt;/strong&gt;.&lt;/p&gt;
8264 &lt;p&gt;They&amp;rsquo;re arbitrary, albeit constant, numbers associated with standard streams. Below, you&amp;rsquo;ll find a summary of the file descriptors for a family of POSIX-compliant operating systems:&lt;/p&gt;
8265 &lt;div class=&quot;table-responsive&quot;&gt;
8266 &lt;table class=&quot;table table-hover&quot;&gt;
8267 &lt;thead&gt;
8268 &lt;tr&gt;
8269 &lt;th&gt;Stream&lt;/th&gt;
8270 &lt;th&gt;File Descriptor&lt;/th&gt;
8271 &lt;/tr&gt;
8272 &lt;/thead&gt;
8273 &lt;tbody&gt;
8274 &lt;tr&gt;
8275 &lt;td&gt;&lt;code&gt;stdin&lt;/code&gt;&lt;/td&gt;
8276 &lt;td&gt;0&lt;/td&gt;
8277 &lt;/tr&gt;
8278 &lt;tr&gt;
8279 &lt;td&gt;&lt;code&gt;stdout&lt;/code&gt;&lt;/td&gt;
8280 &lt;td&gt;1&lt;/td&gt;
8281 &lt;/tr&gt;
8282 &lt;tr&gt;
8283 &lt;td&gt;&lt;code&gt;stderr&lt;/code&gt;&lt;/td&gt;
8284 &lt;td&gt;2&lt;/td&gt;
8285 &lt;/tr&gt;
8286 &lt;/tbody&gt;
8287 &lt;/table&gt;
8288 &lt;/div&gt;
8289 &lt;p&gt;Knowing those descriptors allows you to redirect one or more streams at a time:&lt;/p&gt;
8290 &lt;div class=&quot;table-responsive&quot;&gt;
8291 &lt;table class=&quot;table table-hover&quot;&gt;
8292 &lt;thead&gt;
8293 &lt;tr&gt;
8294 &lt;th&gt;Command&lt;/th&gt;
8295 &lt;th&gt;Description&lt;/th&gt;
8296 &lt;/tr&gt;
8297 &lt;/thead&gt;
8298 &lt;tbody&gt;
8299 &lt;tr&gt;
8300 &lt;td&gt;&lt;code&gt;./program &amp;gt; out.txt&lt;/code&gt;&lt;/td&gt;
8301 &lt;td&gt;Redirect &lt;code&gt;stdout&lt;/code&gt;&lt;/td&gt;
8302 &lt;/tr&gt;
8303 &lt;tr&gt;
8304 &lt;td&gt;&lt;code&gt;./program 2&amp;gt; err.txt&lt;/code&gt;&lt;/td&gt;
8305 &lt;td&gt;Redirect &lt;code&gt;stderr&lt;/code&gt;&lt;/td&gt;
8306 &lt;/tr&gt;
8307 &lt;tr&gt;
8308 &lt;td&gt;&lt;code&gt;./program &amp;gt; out.txt 2&amp;gt; err.txt&lt;/code&gt;&lt;/td&gt;
8309 &lt;td&gt;Redirect &lt;code&gt;stdout&lt;/code&gt; and &lt;code&gt;stderr&lt;/code&gt; to separate files&lt;/td&gt;
8310 &lt;/tr&gt;
8311 &lt;tr&gt;
8312 &lt;td&gt;&lt;code&gt;./program &amp;amp;&amp;gt; out_err.txt&lt;/code&gt;&lt;/td&gt;
8313 &lt;td&gt;Redirect &lt;code&gt;stdout&lt;/code&gt; and &lt;code&gt;stderr&lt;/code&gt; to the same file&lt;/td&gt;
8314 &lt;/tr&gt;
8315 &lt;/tbody&gt;
8316 &lt;/table&gt;
8317 &lt;/div&gt;
8318 &lt;p&gt;Note that &lt;code&gt;&amp;gt;&lt;/code&gt; is the same as &lt;code&gt;1&amp;gt;&lt;/code&gt;.&lt;/p&gt;
8319 &lt;/div&gt;
8320 &lt;p&gt;Some programs use different coloring to distinguish between messages printed to &lt;code&gt;stdout&lt;/code&gt; and &lt;code&gt;stderr&lt;/code&gt;:&lt;/p&gt;
8321 &lt;figure class=&quot;figure mx-auto d-block&quot;&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-console-streams.69affb3462e4.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-console-streams.69affb3462e4.png&quot; width=&quot;999&quot; height=&quot;286&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-console-streams.69affb3462e4.png&amp;amp;w=249&amp;amp;sig=6f17bc7db6fe5ea5fb93f3575f95a4d46eb88af0 249w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-console-streams.69affb3462e4.png&amp;amp;w=499&amp;amp;sig=ccfce4ba25553f534f07f65b483f9af2b106109b 499w, https://files.realpython.com/media/pycharm-console-streams.69affb3462e4.png 999w&quot; sizes=&quot;75vw&quot; alt=&quot;The output of a program executed in PyCharm&quot;/&gt;&lt;/a&gt;&lt;figcaption class=&quot;figure-caption text-center&quot;&gt;Run Tool Window in PyCharm&lt;/figcaption&gt;&lt;/figure&gt;
8322 
8323 
8324 &lt;p&gt;While both &lt;code&gt;stdout&lt;/code&gt; and &lt;code&gt;stderr&lt;/code&gt; are write-only, &lt;code&gt;stdin&lt;/code&gt; is read-only. You can think of standard input as your keyboard, but just like with the other two, you can swap out &lt;code&gt;stdin&lt;/code&gt; for a file to read data from.&lt;/p&gt;
8325 &lt;/div&gt;&lt;/div&gt;
8326 
8327 &lt;/div&gt;
8328 &lt;p&gt;In Python, you can access all standard streams through the built-in &lt;code&gt;sys&lt;/code&gt; module:&lt;/p&gt;
8329 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
8330 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stdin&lt;/span&gt;
8331 &lt;span class=&quot;go&quot;&gt;&amp;lt;_io.TextIOWrapper name=&amp;#39;&amp;lt;stdin&amp;gt;&amp;#39; mode=&amp;#39;r&amp;#39; encoding=&amp;#39;UTF-8&amp;#39;&amp;gt;&lt;/span&gt;
8332 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stdin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fileno&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
8333 &lt;span class=&quot;go&quot;&gt;0&lt;/span&gt;
8334 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stdout&lt;/span&gt;
8335 &lt;span class=&quot;go&quot;&gt;&amp;lt;_io.TextIOWrapper name=&amp;#39;&amp;lt;stdout&amp;gt;&amp;#39; mode=&amp;#39;w&amp;#39; encoding=&amp;#39;UTF-8&amp;#39;&amp;gt;&lt;/span&gt;
8336 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stdout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fileno&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
8337 &lt;span class=&quot;go&quot;&gt;1&lt;/span&gt;
8338 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stderr&lt;/span&gt;
8339 &lt;span class=&quot;go&quot;&gt;&amp;lt;_io.TextIOWrapper name=&amp;#39;&amp;lt;stderr&amp;gt;&amp;#39; mode=&amp;#39;w&amp;#39; encoding=&amp;#39;UTF-8&amp;#39;&amp;gt;&lt;/span&gt;
8340 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fileno&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
8341 &lt;span class=&quot;go&quot;&gt;2&lt;/span&gt;
8342 &lt;/pre&gt;&lt;/div&gt;
8343 
8344 &lt;p&gt;As you can see, these predefined values resemble file-like objects with &lt;code&gt;mode&lt;/code&gt; and &lt;code&gt;encoding&lt;/code&gt; attributes as well as &lt;code&gt;.read()&lt;/code&gt; and &lt;code&gt;.write()&lt;/code&gt; methods among many others.&lt;/p&gt;
8345 &lt;p&gt;By default, &lt;code&gt;print()&lt;/code&gt; is bound to &lt;code&gt;sys.stdout&lt;/code&gt; through its &lt;code&gt;file&lt;/code&gt; argument, but you can change that. Use that keyword argument to indicate a file that was open in write or append mode, so that messages go straight to it:&lt;/p&gt;
8346 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;file.txt&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;w&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
8347     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hello world&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8348 &lt;/pre&gt;&lt;/div&gt;
8349 
8350 &lt;p&gt;This will make your code immune to stream redirection at the operating system level, which might or might not be desired.&lt;/p&gt;
8351 &lt;p&gt;For more information on &lt;a href=&quot;https://realpython.com/working-with-files-in-python/&quot;&gt;working with files in Python&lt;/a&gt;, you can check out &lt;a href=&quot;https://realpython.com/read-write-files-python&quot;&gt;Reading and Writing Files in Python (Guide)&lt;/a&gt;.&lt;/p&gt;
8352 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
8353 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Don&amp;rsquo;t try using &lt;code&gt;print()&lt;/code&gt; for writing binary data as it&amp;rsquo;s only well suited for text.&lt;/p&gt;
8354 &lt;p&gt;Just call the binary file&amp;rsquo;s &lt;code&gt;.write()&lt;/code&gt; directly:&lt;/p&gt;
8355 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;file.dat&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;wb&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
8356     &lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
8357     &lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xff&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8358 &lt;/pre&gt;&lt;/div&gt;
8359 
8360 &lt;p&gt;If you wanted to write raw bytes on the standard output, then this will fail too because &lt;code&gt;sys.stdout&lt;/code&gt; is a character stream:&lt;/p&gt;
8361 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
8362 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stdout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
8363 &lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
8364   File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
8365 &lt;span class=&quot;gr&quot;&gt;TypeError&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;write() argument must be str, not bytes&lt;/span&gt;
8366 &lt;/pre&gt;&lt;/div&gt;
8367 
8368 &lt;p&gt;You must dig deeper to get a handle of the underlying byte stream instead:&lt;/p&gt;
8369 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
8370 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_bytes_written&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stdout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x41\x0a&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8371 &lt;span class=&quot;go&quot;&gt;A&lt;/span&gt;
8372 &lt;/pre&gt;&lt;/div&gt;
8373 
8374 &lt;p&gt;This prints an uppercase letter &lt;code&gt;A&lt;/code&gt; and a newline character, which correspond to decimal values of 65 and 10 in ASCII. However, they&amp;rsquo;re encoded using hexadecimal notation in the bytes literal.&lt;/p&gt;
8375 &lt;/div&gt;
8376 &lt;p&gt;Note that &lt;code&gt;print()&lt;/code&gt; has no control over &lt;a href=&quot;https://realpython.com/python-encodings-guide/&quot;&gt;character encoding&lt;/a&gt;. It&amp;rsquo;s the stream&amp;rsquo;s responsibility to encode received Unicode strings into bytes correctly. In most cases, you won&amp;rsquo;t set the encoding yourself, because the default UTF-8 is what you want. If you really need to, perhaps for legacy systems, you can use the &lt;code&gt;encoding&lt;/code&gt; argument of &lt;code&gt;open()&lt;/code&gt;:&lt;/p&gt;
8377 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;file.txt&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;w&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;iso-8859-1&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
8378     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;รผber naรฏve cafรฉ&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8379 &lt;/pre&gt;&lt;/div&gt;
8380 
8381 &lt;p&gt;Instead of a real file existing somewhere in your file system, you can provide a fake one, which would reside in your computer&amp;rsquo;s memory. You&amp;rsquo;ll use this technique later for mocking &lt;code&gt;print()&lt;/code&gt; in unit tests:&lt;/p&gt;
8382 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;io&lt;/span&gt;
8383 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fake_file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StringIO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
8384 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hello world&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fake_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8385 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fake_file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getvalue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
8386 &lt;span class=&quot;go&quot;&gt;&amp;#39;hello world\n&amp;#39;&lt;/span&gt;
8387 &lt;/pre&gt;&lt;/div&gt;
8388 
8389 &lt;p&gt;If you got to this point, then you&amp;rsquo;re left with only one keyword argument in &lt;code&gt;print()&lt;/code&gt;, which you&amp;rsquo;ll see in the next subsection. It&amp;rsquo;s probably the least used of them all. Nevertheless, there are times when it&amp;rsquo;s absolutely necessary.&lt;/p&gt;
8390 &lt;div class=&quot;card mb-3&quot; id=&quot;collapse_card80f59e&quot;&gt;
8391 &lt;div class=&quot;card-header border-0&quot;&gt;&lt;p class=&quot;m-0&quot;&gt;&lt;button class=&quot;btn&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapse80f59e&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapse80f59e&quot;&gt;Syntax in Python 2&lt;/button&gt; &lt;button class=&quot;btn btn-link float-right&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapse80f59e&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapse80f59e&quot;&gt;Show/Hide&lt;/button&gt;&lt;/p&gt;&lt;/div&gt;
8392 &lt;div id=&quot;collapse80f59e&quot; class=&quot;collapse&quot; data-parent=&quot;#collapse_card80f59e&quot;&gt;&lt;div class=&quot;card-body&quot; markdown=&quot;1&quot;&gt;
8393 
8394 &lt;p&gt;There&amp;rsquo;s a special syntax in Python 2 for replacing the default &lt;code&gt;sys.stdout&lt;/code&gt; with a custom file in the &lt;code&gt;print&lt;/code&gt; statement:&lt;/p&gt;
8395 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;file.txt&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;w&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
8396     &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;hello world&amp;#39;&lt;/span&gt;
8397 &lt;/pre&gt;&lt;/div&gt;
8398 
8399 &lt;p&gt;Because strings and bytes are represented with the same &lt;code&gt;str&lt;/code&gt; type in Python 2, the &lt;code&gt;print&lt;/code&gt; statement can handle binary data just fine:&lt;/p&gt;
8400 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;file.dat&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;wb&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
8401     &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x41\x0a&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;
8402 &lt;/pre&gt;&lt;/div&gt;
8403 
8404 &lt;p&gt;Although, there&amp;rsquo;s a problem with character encoding. The &lt;code&gt;open()&lt;/code&gt; function in Python 2 lacks the &lt;code&gt;encoding&lt;/code&gt; parameter, which would often result in the dreadful  &lt;code&gt;UnicodeEncodeError&lt;/code&gt;:&lt;/p&gt;
8405 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;file.txt&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;w&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
8406 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;unicode_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xfc&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;ber na&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xef&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;ve caf&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xe9&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;
8407 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unicode_text&lt;/span&gt;
8408 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;
8409 &lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
8410   File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
8411 &lt;span class=&quot;gr&quot;&gt;UnicodeEncodeError&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;&amp;#39;ascii&amp;#39; codec can&amp;#39;t encode character u&amp;#39;\xfc&amp;#39;...&lt;/span&gt;
8412 &lt;/pre&gt;&lt;/div&gt;
8413 
8414 &lt;p&gt;Notice how non-Latin characters must be escaped in both Unicode and string literals to avoid a syntax error. Take a look at this example:&lt;/p&gt;
8415 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unicode_literal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xfc&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;ber na&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xef&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;ve caf&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xe9&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;
8416 &lt;span class=&quot;n&quot;&gt;string_literal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xc3\xbc&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;ber na&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xc3\xaf&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;ve caf&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xc3\xa9&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;
8417 &lt;/pre&gt;&lt;/div&gt;
8418 
8419 &lt;p&gt;Alternatively, you could specify source code encoding according to &lt;a href=&quot;https://www.python.org/dev/peps/pep-0263/&quot;&gt;PEP 263&lt;/a&gt; at the top of the file, but that wasn&amp;rsquo;t the best practice due to portability issues:&lt;/p&gt;
8420 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;#!/usr/bin/env python2&lt;/span&gt;
8421 &lt;span class=&quot;c1&quot;&gt;# -*- coding: utf-8 -*-&lt;/span&gt;
8422 
8423 &lt;span class=&quot;n&quot;&gt;unescaped_unicode_literal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;รผber naรฏve cafรฉ&amp;#39;&lt;/span&gt;
8424 &lt;span class=&quot;n&quot;&gt;unescaped_string_literal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;รผber naรฏve cafรฉ&amp;#39;&lt;/span&gt;
8425 &lt;/pre&gt;&lt;/div&gt;
8426 
8427 &lt;p&gt;Your best bet is to encode the Unicode string just before printing it. You can do this manually:&lt;/p&gt;
8428 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;file.txt&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;w&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
8429     &lt;span class=&quot;n&quot;&gt;unicode_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xfc&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;ber na&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xef&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;ve caf&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xe9&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;
8430     &lt;span class=&quot;n&quot;&gt;encoded_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unicode_text&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;utf-8&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8431     &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encoded_text&lt;/span&gt;
8432 &lt;/pre&gt;&lt;/div&gt;
8433 
8434 &lt;p&gt;However, a more convenient option is to use the built-in &lt;code&gt;codecs&lt;/code&gt; module:&lt;/p&gt;
8435 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;codecs&lt;/span&gt;
8436 
8437 &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;codecs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;file.txt&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;w&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;utf-8&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
8438     &lt;span class=&quot;n&quot;&gt;unicode_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xfc&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;ber na&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xef&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;ve caf&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xe9&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;
8439     &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unicode_text&lt;/span&gt;
8440 &lt;/pre&gt;&lt;/div&gt;
8441 
8442 &lt;p&gt;It&amp;rsquo;ll take care of making appropriate conversions when you need to read or write files.&lt;/p&gt;
8443 &lt;/div&gt;&lt;/div&gt;
8444 
8445 &lt;/div&gt;
8446 &lt;h3 id=&quot;buffering-print-calls&quot;&gt;Buffering Print Calls&lt;/h3&gt;
8447 &lt;p&gt;In the previous subsection, you learned that &lt;code&gt;print()&lt;/code&gt; delegates printing to a file-like object such as &lt;code&gt;sys.stdout&lt;/code&gt;. Some streams, however, buffer certain I/O operations to enhance performance, which can get in the way. Let&amp;rsquo;s take a look at an example.&lt;/p&gt;
8448 &lt;p&gt;Imagine you were writing a countdown timer, which should append the remaining time to the same line every second:&lt;/p&gt;
8449 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;3...2...1...Go!
8450 &lt;/pre&gt;&lt;/div&gt;
8451 
8452 &lt;p&gt;Your first attempt may look something like this:&lt;/p&gt;
8453 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt;
8454 
8455 &lt;span class=&quot;n&quot;&gt;num_seconds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
8456 &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;countdown&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;reversed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_seconds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
8457     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;countdown&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
8458         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;countdown&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;...&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8459         &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8460     &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
8461         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Go!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8462 &lt;/pre&gt;&lt;/div&gt;
8463 
8464 &lt;p&gt;As long as the &lt;code&gt;countdown&lt;/code&gt; variable is greater than zero, the code keeps appending text without a trailing newline and then goes to sleep for one second.  Finally, when the countdown is finished, it prints &lt;code&gt;Go!&lt;/code&gt; and terminates the line.&lt;/p&gt;
8465 &lt;p&gt;Unexpectedly, instead of counting down every second, the program idles wastefully for three seconds, and then suddenly prints the entire line at once:&lt;/p&gt;
8466 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/print_countdown.ba38eb242915.gif&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/print_countdown.ba38eb242915.gif&quot; width=&quot;576&quot; height=&quot;236&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/print_countdown.ba38eb242915.gif&amp;amp;w=144&amp;amp;sig=1084be0d7eb723df32fe7d0e16f06724e2666c04 144w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/print_countdown.ba38eb242915.gif&amp;amp;w=288&amp;amp;sig=0618ab48bb5a9e8b729d8a97e4736f78f9b3560f 288w, https://files.realpython.com/media/print_countdown.ba38eb242915.gif 576w&quot; sizes=&quot;75vw&quot; alt=&quot;Terminal with buffered output&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
8467 &lt;p&gt;That&amp;rsquo;s because the operating system buffers subsequent writes to the standard output in this case. You need to know that there are three kinds of streams with respect to buffering:&lt;/p&gt;
8468 &lt;ol&gt;
8469 &lt;li&gt;Unbuffered&lt;/li&gt;
8470 &lt;li&gt;Line-buffered&lt;/li&gt;
8471 &lt;li&gt;Block-buffered&lt;/li&gt;
8472 &lt;/ol&gt;
8473 &lt;p&gt;&lt;strong&gt;Unbuffered&lt;/strong&gt; is self-explanatory, that is, no buffering is taking place, and all writes have immediate effect. A &lt;strong&gt;line-buffered&lt;/strong&gt; stream waits before firing any I/O calls until a line break appears somewhere in the buffer, whereas a &lt;strong&gt;block-buffered&lt;/strong&gt; one simply allows the buffer to fill up to a certain size regardless of its content. Standard output is both &lt;strong&gt;line-buffered&lt;/strong&gt; and &lt;strong&gt;block-buffered&lt;/strong&gt;, depending on which event comes first.&lt;/p&gt;
8474 &lt;p&gt;Buffering helps to reduce the number of expensive I/O calls. Think about sending messages over a high-latency network, for example. When you connect to a remote server to execute commands over the SSH protocol, each of your keystrokes may actually produce an individual data packet, which is orders of magnitude bigger than its payload. What an overhead! It would make sense to wait until at least a few characters are typed and then send them together. That&amp;rsquo;s where buffering steps in.&lt;/p&gt;
8475 &lt;p&gt;On the other hand, buffering can sometimes have undesired effects as you just saw with the countdown example. To fix it, you can simply tell &lt;code&gt;print()&lt;/code&gt; to forcefully flush the stream without waiting for a newline character in the buffer using its &lt;code&gt;flush&lt;/code&gt; flag:&lt;/p&gt;
8476 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;countdown&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;...&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flush&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8477 &lt;/pre&gt;&lt;/div&gt;
8478 
8479 &lt;p&gt;That&amp;rsquo;s all. Your countdown should work as expected now, but don&amp;rsquo;t take my word for it. Go ahead and test it to see the difference.&lt;/p&gt;
8480 &lt;p&gt;Congratulations! At this point, you&amp;rsquo;ve seen examples of calling &lt;code&gt;print()&lt;/code&gt; that cover all of its parameters. You know their purpose and when to use them. Understanding the signature is only the beginning, however. In the upcoming sections, you&amp;rsquo;ll see why.&lt;/p&gt;
8481 &lt;div class=&quot;card mb-3&quot; id=&quot;collapse_cardec4147&quot;&gt;
8482 &lt;div class=&quot;card-header border-0&quot;&gt;&lt;p class=&quot;m-0&quot;&gt;&lt;button class=&quot;btn&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapseec4147&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapseec4147&quot;&gt;Syntax in Python 2&lt;/button&gt; &lt;button class=&quot;btn btn-link float-right&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapseec4147&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapseec4147&quot;&gt;Show/Hide&lt;/button&gt;&lt;/p&gt;&lt;/div&gt;
8483 &lt;div id=&quot;collapseec4147&quot; class=&quot;collapse&quot; data-parent=&quot;#collapse_cardec4147&quot;&gt;&lt;div class=&quot;card-body&quot; markdown=&quot;1&quot;&gt;
8484 
8485 &lt;p&gt;There isn&amp;rsquo;t an easy way to flush the stream in Python 2, because the &lt;code&gt;print&lt;/code&gt; statement doesn&amp;rsquo;t allow for it by itself. You need to get a handle of its lower-level layer, which is the standard output, and call it directly:&lt;/p&gt;
8486 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt;
8487 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
8488 
8489 &lt;span class=&quot;n&quot;&gt;num_seconds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
8490 &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;countdown&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;reversed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_seconds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
8491     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;countdown&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
8492         &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stdout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;...&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;countdown&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8493         &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stdout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flush&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
8494         &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8495     &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
8496         &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Go!&amp;#39;&lt;/span&gt;
8497 &lt;/pre&gt;&lt;/div&gt;
8498 
8499 &lt;p&gt;Alternatively, you could disable buffering of the standard streams either by providing the &lt;code&gt;-u&lt;/code&gt; flag to the Python interpreter or by setting up the &lt;code&gt;PYTHONUNBUFFERED&lt;/code&gt; environment variable:&lt;/p&gt;
8500 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python2 -u countdown.py
8501 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;PYTHONUNBUFFERED&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; python2 countdown.py
8502 &lt;/pre&gt;&lt;/div&gt;
8503 
8504 &lt;p&gt;Note that &lt;code&gt;print()&lt;/code&gt; was backported to Python 2 and made available through the &lt;code&gt;__future__&lt;/code&gt; module. Unfortunately, it doesn&amp;rsquo;t come with the &lt;code&gt;flush&lt;/code&gt; parameter:&lt;/p&gt;
8505 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;__future__&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print_function&lt;/span&gt;
8506 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;help&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8507 &lt;span class=&quot;go&quot;&gt;Help on built-in function print in module __builtin__:&lt;/span&gt;
8508 
8509 &lt;span class=&quot;go&quot;&gt;print(...)&lt;/span&gt;
8510 &lt;span class=&quot;go&quot;&gt;    print(value, ..., sep=&amp;#39; &amp;#39;, end=&amp;#39;\n&amp;#39;, file=sys.stdout)&lt;/span&gt;
8511 &lt;/pre&gt;&lt;/div&gt;
8512 
8513 &lt;p&gt;What you&amp;rsquo;re seeing here is a &lt;strong&gt;docstring&lt;/strong&gt; of the &lt;code&gt;print()&lt;/code&gt; function. You can display docstrings of various objects in Python using the built-in &lt;code&gt;help()&lt;/code&gt; function.&lt;/p&gt;
8514 &lt;/div&gt;&lt;/div&gt;
8515 
8516 &lt;/div&gt;
8517 &lt;h3 id=&quot;printing-custom-data-types&quot;&gt;Printing Custom Data Types&lt;/h3&gt;
8518 &lt;p&gt;Up until now, you only dealt with built-in data types such as strings and numbers, but you&amp;rsquo;ll often want to print your own abstract data types. Let&amp;rsquo;s have a look at different ways of defining them.&lt;/p&gt;
8519 &lt;p&gt;For simple objects without any logic, whose purpose is to carry data, you&amp;rsquo;ll typically take advantage of &lt;a href=&quot;https://docs.python.org/3/library/collections.html#collections.namedtuple&quot;&gt;&lt;code&gt;namedtuple&lt;/code&gt;&lt;/a&gt;, which is available in the standard library. Named tuples have a neat textual representation out of the box:&lt;/p&gt;
8520 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;collections&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;namedtuple&lt;/span&gt;
8521 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Person&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;namedtuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Person&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;name age&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8522 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jdoe&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;John Doe&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8523 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jdoe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8524 &lt;span class=&quot;go&quot;&gt;Person(name=&amp;#39;John Doe&amp;#39;, age=42)&lt;/span&gt;
8525 &lt;/pre&gt;&lt;/div&gt;
8526 
8527 &lt;p&gt;That&amp;rsquo;s great as long as holding data is enough, but in order to add behaviors to the &lt;code&gt;Person&lt;/code&gt; type, you&amp;rsquo;ll eventually need to define a class. Take a look at this example:&lt;/p&gt;
8528 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
8529     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
8530         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;age&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt;
8531 &lt;/pre&gt;&lt;/div&gt;
8532 
8533 &lt;p&gt;If you now create an instance of the &lt;code&gt;Person&lt;/code&gt; class and try to print it, you&amp;rsquo;ll get this bizarre output, which is quite different from the equivalent &lt;code&gt;namedtuple&lt;/code&gt;:&lt;/p&gt;
8534 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jdoe&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;John Doe&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8535 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jdoe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8536 &lt;span class=&quot;go&quot;&gt;&amp;lt;__main__.Person object at 0x7fcac3fed1d0&amp;gt;&lt;/span&gt;
8537 &lt;/pre&gt;&lt;/div&gt;
8538 
8539 &lt;p&gt;It&amp;rsquo;s the default representation of objects, which comprises their address in memory, the corresponding class name and a module in which they were defined. You&amp;rsquo;ll fix that in a bit, but just for the record, as a quick workaround you could combine &lt;code&gt;namedtuple&lt;/code&gt; and a custom class through &lt;a href=&quot;https://realpython.com/inheritance-composition-python/&quot;&gt;inheritance&lt;/a&gt;:&lt;/p&gt;
8540 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;collections&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;namedtuple&lt;/span&gt;
8541 
8542 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;namedtuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Person&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;name age&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
8543     &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
8544 &lt;/pre&gt;&lt;/div&gt;
8545 
8546 &lt;p&gt;Your &lt;code&gt;Person&lt;/code&gt; class has just become a specialized kind of &lt;code&gt;namedtuple&lt;/code&gt; with two attributes, which you can customize.&lt;/p&gt;
8547 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
8548 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In Python 3, the &lt;code&gt;pass&lt;/code&gt; statement can be replaced with the &lt;a href=&quot;https://docs.python.org/dev/library/constants.html#Ellipsis&quot;&gt;ellipsis&lt;/a&gt; (&lt;code&gt;...&lt;/code&gt;) literal to indicate a placeholder:&lt;/p&gt;
8549 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;delta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
8550     &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
8551 &lt;/pre&gt;&lt;/div&gt;
8552 
8553 &lt;p&gt;This prevents the interpreter from raising &lt;code&gt;IndentationError&lt;/code&gt; due to missing indented block of code.&lt;/p&gt;
8554 &lt;/div&gt;
8555 &lt;p&gt;That&amp;rsquo;s better than a plain &lt;code&gt;namedtuple&lt;/code&gt;, because not only do you get printing right for free, but you can also add custom methods and properties to the class. However, it solves one problem while introducing another. Remember that tuples, including named tuples, are immutable in Python, so they can&amp;rsquo;t change their values once created.&lt;/p&gt;
8556 &lt;p&gt;It&amp;rsquo;s true that designing immutable data types is desirable, but in many cases, you&amp;rsquo;ll want them to allow for change, so you&amp;rsquo;re back with regular classes again.&lt;/p&gt;
8557 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
8558 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Following other languages and frameworks, Python 3.7 introduced &lt;a href=&quot;https://realpython.com/python-data-classes/&quot;&gt;data classes&lt;/a&gt;, which you can think of as mutable tuples. This way, you get the best of both worlds:&lt;/p&gt;
8559 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dataclasses&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataclass&lt;/span&gt;
8560 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@dataclass&lt;/span&gt;
8561 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
8562 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
8563 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;
8564 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    
8565 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;celebrate_birthday&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
8566 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;age&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
8567 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;
8568 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jdoe&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;John Doe&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8569 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jdoe&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;celebrate_birthday&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
8570 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jdoe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8571 &lt;span class=&quot;go&quot;&gt;Person(name=&amp;#39;John Doe&amp;#39;, age=43)&lt;/span&gt;
8572 &lt;/pre&gt;&lt;/div&gt;
8573 
8574 &lt;p&gt;The syntax for &lt;a href=&quot;https://www.python.org/dev/peps/pep-0526/&quot;&gt;variable annotations&lt;/a&gt;, which is required to specify class fields with their corresponding types, was defined in Python 3.6.&lt;/p&gt;
8575 &lt;/div&gt;
8576 &lt;p&gt;From earlier subsections, you already know that &lt;code&gt;print()&lt;/code&gt; implicitly calls the built-in &lt;code&gt;str()&lt;/code&gt; function to convert its positional arguments into strings. Indeed, calling &lt;code&gt;str()&lt;/code&gt; manually against an instance of the regular &lt;code&gt;Person&lt;/code&gt; class yields the same result as printing it:&lt;/p&gt;
8577 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jdoe&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;John Doe&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8578 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jdoe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8579 &lt;span class=&quot;go&quot;&gt;&amp;#39;&amp;lt;__main__.Person object at 0x7fcac3fed1d0&amp;gt;&amp;#39;&lt;/span&gt;
8580 &lt;/pre&gt;&lt;/div&gt;
8581 
8582 &lt;p&gt;&lt;code&gt;str()&lt;/code&gt;, in turn, looks for one of two &lt;strong&gt;magic methods&lt;/strong&gt; within the class body, which you typically implement. If it doesn&amp;rsquo;t find one, then it falls back to the ugly default representation. Those magic methods are, in order of search:&lt;/p&gt;
8583 &lt;ol&gt;
8584 &lt;li&gt;&lt;code&gt;def __str__(self)&lt;/code&gt;&lt;/li&gt;
8585 &lt;li&gt;&lt;code&gt;def __repr__(self)&lt;/code&gt;&lt;/li&gt;
8586 &lt;/ol&gt;
8587 &lt;p&gt;The first one is recommended to return a short, human-readable text, which includes information from the most relevant attributes. After all, you don&amp;rsquo;t want to expose sensitive data, such as user passwords, when printing objects.&lt;/p&gt;
8588 &lt;p&gt;However, the other one should provide complete information about an object, to allow for restoring its state from a string. Ideally, it should return valid Python code, so that you can pass it directly to &lt;code&gt;eval()&lt;/code&gt;:&lt;/p&gt;
8589 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;repr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jdoe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8590 &lt;span class=&quot;go&quot;&gt;&amp;quot;Person(name=&amp;#39;John Doe&amp;#39;, age=42)&amp;quot;&lt;/span&gt;
8591 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;repr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jdoe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
8592 &lt;span class=&quot;go&quot;&gt;&amp;lt;class &amp;#39;__main__.Person&amp;#39;&amp;gt;&lt;/span&gt;
8593 &lt;/pre&gt;&lt;/div&gt;
8594 
8595 &lt;p&gt;Notice the use of another built-in function, &lt;code&gt;repr()&lt;/code&gt;, which always tries to call &lt;code&gt;.__repr__()&lt;/code&gt; in an object, but falls back to the default representation if it doesn&amp;rsquo;t find that method.&lt;/p&gt;
8596 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
8597 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Even though &lt;code&gt;print()&lt;/code&gt; itself uses &lt;code&gt;str()&lt;/code&gt; for type casting, some compound data types delegate that call to &lt;code&gt;repr()&lt;/code&gt; on their members. This happens to lists and tuples, for example.&lt;/p&gt;
8598 &lt;p&gt;Consider this class with both magic methods, which return alternative string representations of the same object:&lt;/p&gt;
8599 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
8600     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;login&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
8601         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;login&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;login&lt;/span&gt;
8602         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;
8603 
8604     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__str__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
8605         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;login&lt;/span&gt;
8606 
8607     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__repr__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
8608         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;User(&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.login}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;#39;, &amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.password}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;#39;)&amp;quot;&lt;/span&gt;
8609 &lt;/pre&gt;&lt;/div&gt;
8610 
8611 &lt;p&gt;If you print a single object of the &lt;code&gt;User&lt;/code&gt; class, then you won&amp;rsquo;t see the password, because &lt;code&gt;print(user)&lt;/code&gt; will call &lt;code&gt;str(user)&lt;/code&gt;, which eventually will invoke &lt;code&gt;user.__str__()&lt;/code&gt;:&lt;/p&gt;
8612 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;jdoe&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;s3cret&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8613 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8614 &lt;span class=&quot;go&quot;&gt;jdoe&lt;/span&gt;
8615 &lt;/pre&gt;&lt;/div&gt;
8616 
8617 &lt;p&gt;However, if you put the same &lt;code&gt;user&lt;/code&gt; variable inside a list by wrapping it in square brackets, then the password will become clearly visible:&lt;/p&gt;
8618 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
8619 &lt;span class=&quot;go&quot;&gt;[User(&amp;#39;jdoe&amp;#39;, &amp;#39;s3cret&amp;#39;)]&lt;/span&gt;
8620 &lt;/pre&gt;&lt;/div&gt;
8621 
8622 &lt;p&gt;That&amp;rsquo;s because sequences, such as lists and tuples, implement their &lt;code&gt;.__str__()&lt;/code&gt; method so that all of their elements are first converted with &lt;code&gt;repr()&lt;/code&gt;.&lt;/p&gt;
8623 &lt;/div&gt;
8624 &lt;p&gt;Python gives you a lot of freedom when it comes to defining your own data types if none of the built-in ones meet your needs. Some of them, such as named tuples and data classes, offer string representations that look good without requiring any work on your part. Still, for the most flexibility, you&amp;rsquo;ll have to define a class and override its magic methods described above.&lt;/p&gt;
8625 &lt;div class=&quot;card mb-3&quot; id=&quot;collapse_card996300&quot;&gt;
8626 &lt;div class=&quot;card-header border-0&quot;&gt;&lt;p class=&quot;m-0&quot;&gt;&lt;button class=&quot;btn&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapse996300&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapse996300&quot;&gt;Syntax in Python 2&lt;/button&gt; &lt;button class=&quot;btn btn-link float-right&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapse996300&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapse996300&quot;&gt;Show/Hide&lt;/button&gt;&lt;/p&gt;&lt;/div&gt;
8627 &lt;div id=&quot;collapse996300&quot; class=&quot;collapse&quot; data-parent=&quot;#collapse_card996300&quot;&gt;&lt;div class=&quot;card-body&quot; markdown=&quot;1&quot;&gt;
8628 
8629 &lt;p&gt;The semantics of &lt;code&gt;.__str__()&lt;/code&gt; and &lt;code&gt;.__repr__()&lt;/code&gt; didn&amp;rsquo;t change since Python 2, but you must remember that strings were nothing more than glorified byte arrays back then. To convert your objects into proper Unicode, which was a separate data type, you&amp;rsquo;d have to provide yet another magic method: &lt;code&gt;.__unicode__()&lt;/code&gt;.&lt;/p&gt;
8630 &lt;p&gt;Here&amp;rsquo;s an example of the same &lt;code&gt;User&lt;/code&gt; class in Python 2:&lt;/p&gt;
8631 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
8632     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;login&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
8633         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;login&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;login&lt;/span&gt;
8634         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;
8635 
8636     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fm&quot;&gt;__unicode__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
8637         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;login&lt;/span&gt;
8638 
8639     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fm&quot;&gt;__str__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
8640         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;unicode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;utf-8&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8641 
8642     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fm&quot;&gt;__repr__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
8643         &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;User(&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;#39;, &amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;#39;)&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;login&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8644         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;unicode_escape&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8645 &lt;/pre&gt;&lt;/div&gt;
8646 
8647 &lt;p&gt;As you can see, this implementation delegates some work to avoid duplication by calling the built-in &lt;code&gt;unicode()&lt;/code&gt; function on itself.&lt;/p&gt;
8648 &lt;p&gt;Both &lt;code&gt;.__str__()&lt;/code&gt; and &lt;code&gt;.__repr__()&lt;/code&gt; methods must return strings, so they encode Unicode characters into specific byte representations called &lt;strong&gt;character sets&lt;/strong&gt;. UTF-8 is the most widespread and safest encoding, while &lt;code&gt;unicode_escape&lt;/code&gt; is a special constant to express funky characters, such as &lt;code&gt;รฉ&lt;/code&gt;, as escape sequences in plain ASCII, such as &lt;code&gt;\xe9&lt;/code&gt;.&lt;/p&gt;
8649 &lt;p&gt;The &lt;code&gt;print&lt;/code&gt; statement is looking for the magic &lt;code&gt;.__str__()&lt;/code&gt; method in the class, so the chosen &lt;strong&gt;charset&lt;/strong&gt; must correspond to the one used by the terminal. For example, default encoding in DOS and Windows is CP 852 rather than UTF-8, so running this can result in a &lt;code&gt;UnicodeEncodeError&lt;/code&gt; or even garbled output:&lt;/p&gt;
8650 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\u043d\u0438\u043a\u0438\u0442\u0430&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;s3cret&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8651 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;
8652 &lt;span class=&quot;go&quot;&gt;ฤ‘ลปฤ‘ลžฤ‘โ•‘ฤ‘ลžฤรฉฤ‘โ–‘&lt;/span&gt;
8653 &lt;/pre&gt;&lt;/div&gt;
8654 
8655 &lt;p&gt;However, if you ran the same code on a system with UTF-8 encoding, then you&amp;rsquo;d get the proper spelling of a popular Russian name:&lt;/p&gt;
8656 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\u043d\u0438\u043a\u0438\u0442\u0430&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;s3cret&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8657 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;
8658 &lt;span class=&quot;go&quot;&gt;ะฝะธะบะธั‚ะฐ&lt;/span&gt;
8659 &lt;/pre&gt;&lt;/div&gt;
8660 
8661 &lt;p&gt;It&amp;rsquo;s recommended to convert strings to Unicode as early as possible, for example, when you&amp;rsquo;re reading data from a file, and use it consistently everywhere in your code. At the same time, you should encode Unicode back to the chosen character set right before presenting it to the user.&lt;/p&gt;
8662 &lt;p&gt;It seems as if you have more control over string representation of objects in Python 2 because there&amp;rsquo;s no magic &lt;code&gt;.__unicode__()&lt;/code&gt; method in Python 3 anymore. You may be asking yourself if it&amp;rsquo;s possible to convert an object to its byte string representation rather than a Unicode string in Python 3. It&amp;rsquo;s possible, with a special &lt;code&gt;.__bytes__()&lt;/code&gt; method that does just that:&lt;/p&gt;
8663 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
8664 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;login&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
8665 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;login&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;login&lt;/span&gt;
8666 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;
8667 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    
8668 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__bytes__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Python 3&lt;/span&gt;
8669 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;login&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;utf-8&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8670 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
8671 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\u043d\u0438\u043a\u0438\u0442\u0430&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;s3cret&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8672 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8673 &lt;span class=&quot;go&quot;&gt;b&amp;#39;\xd0\xbd\xd0\xb8\xd0\xba\xd0\xb8\xd1\x82\xd0\xb0&amp;#39;&lt;/span&gt;
8674 &lt;/pre&gt;&lt;/div&gt;
8675 
8676 &lt;p&gt;Using the built-in &lt;code&gt;bytes()&lt;/code&gt; function on an instance delegates the call to its &lt;code&gt;__bytes__()&lt;/code&gt; method defined in the corresponding class.&lt;/p&gt;
8677 &lt;/div&gt;&lt;/div&gt;
8678 
8679 &lt;/div&gt;
8680 &lt;h2 id=&quot;understanding-python-print&quot;&gt;Understanding Python Print&lt;/h2&gt;
8681 &lt;p&gt;You know &lt;strong&gt;how&lt;/strong&gt; to use &lt;code&gt;print()&lt;/code&gt; quite well at this point, but knowing &lt;strong&gt;what&lt;/strong&gt; it is will allow you to use it even more effectively and consciously. After reading this section, you&amp;rsquo;ll understand how printing in Python has improved over the years.&lt;/p&gt;
8682 &lt;h3 id=&quot;print-is-a-function-in-python-3&quot;&gt;Print Is a Function in Python 3&lt;/h3&gt;
8683 &lt;p&gt;You&amp;rsquo;ve seen that &lt;code&gt;print()&lt;/code&gt; is a function in Python 3. More specifically, it&amp;rsquo;s a built-in function, which means that you don&amp;rsquo;t need to import it from anywhere:&lt;/p&gt;
8684 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;
8685 &lt;span class=&quot;go&quot;&gt;&amp;lt;built-in function print&amp;gt;&lt;/span&gt;
8686 &lt;/pre&gt;&lt;/div&gt;
8687 
8688 &lt;p&gt;It&amp;rsquo;s always available in the global namespace so that you can call it directly, but you can also access it through a module from the standard library:&lt;/p&gt;
8689 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;builtins&lt;/span&gt;
8690 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builtins&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;
8691 &lt;span class=&quot;go&quot;&gt;&amp;lt;built-in function print&amp;gt;&lt;/span&gt;
8692 &lt;/pre&gt;&lt;/div&gt;
8693 
8694 &lt;p&gt;This way, you can avoid name collisions with custom functions. Let&amp;rsquo;s say you wanted to &lt;strong&gt;redefine&lt;/strong&gt; &lt;code&gt;print()&lt;/code&gt; so that it doesn&amp;rsquo;t append a trailing newline. At the same time, you wanted to rename the original function to something like &lt;code&gt;println()&lt;/code&gt;:&lt;/p&gt;
8695 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;builtins&lt;/span&gt;
8696 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;builtins&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;
8697 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
8698 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;builtins&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8699 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
8700 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hello&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8701 &lt;span class=&quot;go&quot;&gt;hello&lt;/span&gt;
8702 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hello&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8703 &lt;span class=&quot;go&quot;&gt;hello&lt;/span&gt;
8704 &lt;/pre&gt;&lt;/div&gt;
8705 
8706 &lt;p&gt;Now you have two separate printing functions just like in the Java programming language. You&amp;rsquo;ll define custom &lt;code&gt;print()&lt;/code&gt; functions in the &lt;a href=&quot;#mocking-python-print-in-unit-tests&quot;&gt;mocking section&lt;/a&gt; later as well. Also, note that you wouldn&amp;rsquo;t be able to overwrite &lt;code&gt;print()&lt;/code&gt; in the first place if it wasn&amp;rsquo;t a function.&lt;/p&gt;
8707 &lt;p&gt;On the other hand, &lt;code&gt;print()&lt;/code&gt; isn&amp;rsquo;t a function in the mathematical sense, because it doesn&amp;rsquo;t return any meaningful value other than the implicit &lt;code&gt;None&lt;/code&gt;:&lt;/p&gt;
8708 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hello world&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8709 &lt;span class=&quot;go&quot;&gt;hello world&lt;/span&gt;
8710 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8711 &lt;span class=&quot;go&quot;&gt;None&lt;/span&gt;
8712 &lt;/pre&gt;&lt;/div&gt;
8713 
8714 &lt;p&gt;Such functions are, in fact, procedures or subroutines that you call to achieve some kind of side-effect, which ultimately is a change of a global state. In the case of &lt;code&gt;print()&lt;/code&gt;, that side-effect is showing a message on the standard output or writing to a file.&lt;/p&gt;
8715 &lt;p&gt;Because &lt;code&gt;print()&lt;/code&gt; is a function, it has a well-defined signature with known attributes. You can quickly find its &lt;strong&gt;documentation&lt;/strong&gt; using the editor of your choice, without having to remember some weird syntax for performing a certain task.&lt;/p&gt;
8716 &lt;p&gt;Besides, functions are easier to &lt;strong&gt;extend&lt;/strong&gt;. Adding a new feature to a function is as easy as adding another keyword argument, whereas changing the language to support that new feature is much more cumbersome. Think of stream redirection or buffer flushing, for example.&lt;/p&gt;
8717 &lt;p&gt;Another benefit of &lt;code&gt;print()&lt;/code&gt; being a function is &lt;strong&gt;composability&lt;/strong&gt;. Functions are so-called &lt;a href=&quot;https://realpython.com/lessons/functions-first-class-objects-python/&quot;&gt;first-class objects&lt;/a&gt; or &lt;a href=&quot;https://realpython.com/lessons/functions-are-first-class-citizens-python/&quot;&gt;first-class citizens&lt;/a&gt; in Python, which is a fancy way of saying they&amp;rsquo;re values just like strings or numbers. This way, you can assign a function to a variable, pass it to another function, or even return one from another. &lt;code&gt;print()&lt;/code&gt; isn&amp;rsquo;t different in this regard. For instance, you can take advantage of it for dependency injection:&lt;/p&gt;
8718 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;download&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
8719     &lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Downloading &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{url}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8720     &lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;
8721 
8722 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;custom_print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
8723     &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Do not print anything&lt;/span&gt;
8724 
8725 &lt;span class=&quot;n&quot;&gt;download&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;/js/app.js&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8726 &lt;/pre&gt;&lt;/div&gt;
8727 
8728 &lt;p&gt;Here, the &lt;code&gt;log&lt;/code&gt; parameter lets you inject a callback function, which defaults to &lt;code&gt;print()&lt;/code&gt; but can be any callable. In this example, printing is completely disabled by substituting &lt;code&gt;print()&lt;/code&gt; with a dummy function that does nothing.&lt;/p&gt;
8729 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
8730 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; A &lt;strong&gt;dependency&lt;/strong&gt; is any piece of code required by another bit of code.&lt;/p&gt;
8731 &lt;p&gt;&lt;strong&gt;Dependency injection&lt;/strong&gt; is a technique used in code design to make it more testable, reusable, and open for extension. You can achieve it by referring to dependencies indirectly through abstract interfaces and by providing them in a &lt;strong&gt;push&lt;/strong&gt; rather than &lt;strong&gt;pull&lt;/strong&gt; fashion.&lt;/p&gt;
8732 &lt;p&gt;There&amp;rsquo;s a funny explanation of dependency injection circulating on the Internet:&lt;/p&gt;
8733 &lt;blockquote&gt;
8734 &lt;p&gt;Dependency injection for five-year-olds&lt;/p&gt;
8735 &lt;p&gt;When you go and get things out of the refrigerator for yourself, you can cause problems. You might leave the door open, you might get something Mommy or Daddy doesn&amp;rsquo;t want you to have. You might even be looking for something we don&amp;rsquo;t even have or which has expired.&lt;/p&gt;
8736 &lt;p&gt;What you should be doing is stating a need, &amp;ldquo;I need something to drink with lunch,&amp;rdquo; and then we will make sure you have something when you sit down to eat.&lt;/p&gt;
8737 &lt;p&gt;&amp;mdash; &lt;em&gt;John Munsch, 28 October 2009.&lt;/em&gt; (&lt;a href=&quot;https://stackoverflow.com/a/1638961&quot;&gt;Source&lt;/a&gt;)&lt;/p&gt;
8738 &lt;/blockquote&gt;
8739 &lt;/div&gt;
8740 &lt;p&gt;Composition allows you to combine a few functions into a new one of the same kind. Let&amp;rsquo;s see this in action by specifying a custom &lt;code&gt;error()&lt;/code&gt; function that prints to the standard error stream and prefixes all messages with a given log level:&lt;/p&gt;
8741 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;functools&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;partial&lt;/span&gt;
8742 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
8743 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;redirect&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;partial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8744 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prefix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;partial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8745 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;redirect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;[ERROR]&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8746 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Something went wrong&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8747 &lt;span class=&quot;go&quot;&gt;[ERROR] Something went wrong&lt;/span&gt;
8748 &lt;/pre&gt;&lt;/div&gt;
8749 
8750 &lt;p&gt;This custom function uses &lt;strong&gt;partial functions&lt;/strong&gt; to achieve the desired effect. It&amp;rsquo;s an advanced concept borrowed from the &lt;a href=&quot;https://realpython.com/courses/functional-programming-python/&quot;&gt;functional programming&lt;/a&gt; paradigm, so you don&amp;rsquo;t need to go too deep into that topic for now. However, if you&amp;rsquo;re interested in this topic, I recommend taking a look at the &lt;a href=&quot;https://pymotw.com/3/functools/&quot;&gt;&lt;code&gt;functools&lt;/code&gt;&lt;/a&gt; module.&lt;/p&gt;
8751 &lt;p&gt;Unlike statements, functions are values. That means you can mix them with &lt;strong&gt;expressions&lt;/strong&gt;, in particular, &lt;a href=&quot;https://realpython.com/python-lambda/&quot;&gt;&lt;strong&gt;lambda&lt;/strong&gt; expressions&lt;/a&gt;. Instead of defining a full-blown function to replace &lt;code&gt;print()&lt;/code&gt; with, you can make an anonymous lambda expression that calls it:&lt;/p&gt;
8752 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;download&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;/js/app.js&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;[INFO]&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
8753 &lt;span class=&quot;go&quot;&gt;[INFO] Downloading /js/app.js&lt;/span&gt;
8754 &lt;/pre&gt;&lt;/div&gt;
8755 
8756 &lt;p&gt;However, because a lambda expression is defined in place, there&amp;rsquo;s no way of referring to it elsewhere in the code.&lt;/p&gt;
8757 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
8758 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In Python, you can&amp;rsquo;t put statements, such as assignments, conditional statements, loops, and so on, in an &lt;strong&gt;anonymous lambda function&lt;/strong&gt;. It has to be a single expression!&lt;/p&gt;
8759 &lt;/div&gt;
8760 &lt;p&gt;Another kind of expression is a ternary conditional expression:&lt;/p&gt;
8761 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;jdoe&amp;#39;&lt;/span&gt;
8762 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Hi!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Hi, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{user}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8763 &lt;span class=&quot;go&quot;&gt;Hi, jdoe.&lt;/span&gt;
8764 &lt;/pre&gt;&lt;/div&gt;
8765 
8766 &lt;p&gt;Python has both &lt;a href=&quot;https://realpython.com/python-conditional-statements/&quot;&gt;conditional statements&lt;/a&gt; and &lt;a href=&quot;https://realpython.com/python-conditional-statements/#conditional-expressions-pythons-ternary-operator&quot;&gt;conditional expressions&lt;/a&gt;. The latter is evaluated to a single value that can be assigned to a variable or passed to a function. In the example above, you&amp;rsquo;re interested in the side-effect rather than the value, which evaluates to &lt;code&gt;None&lt;/code&gt;, so you simply ignore it.&lt;/p&gt;
8767 &lt;p&gt;As you can see, functions allow for an elegant and extensible solution, which is consistent with the rest of the language. In the next subsection, you&amp;rsquo;ll discover how not having &lt;code&gt;print()&lt;/code&gt; as a function caused a lot of headaches.&lt;/p&gt;
8768 &lt;h3 id=&quot;print-was-a-statement-in-python-2&quot;&gt;Print Was a Statement in Python 2&lt;/h3&gt;
8769 &lt;p&gt;A &lt;strong&gt;statement&lt;/strong&gt; is an instruction that may evoke a side-effect when executed but never evaluates to a value. In other words, you wouldn&amp;rsquo;t be able to print a statement or assign it to a variable like this:&lt;/p&gt;
8770 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;hello world&amp;#39;&lt;/span&gt;
8771 &lt;/pre&gt;&lt;/div&gt;
8772 
8773 &lt;p&gt;That&amp;rsquo;s a syntax error in Python 2.&lt;/p&gt;
8774 &lt;p&gt;Here are a few more examples of statements in Python:&lt;/p&gt;
8775 &lt;ul&gt;
8776 &lt;li&gt;&lt;strong&gt;assignment:&lt;/strong&gt; &lt;code&gt;=&lt;/code&gt;&lt;/li&gt;
8777 &lt;li&gt;&lt;strong&gt;conditional:&lt;/strong&gt; &lt;code&gt;if&lt;/code&gt;&lt;/li&gt;
8778 &lt;li&gt;&lt;strong&gt;loop:&lt;/strong&gt; &lt;code&gt;while&lt;/code&gt;&lt;/li&gt;
8779 &lt;li&gt;&lt;strong&gt;assertion&lt;/strong&gt;: &lt;code&gt;assert&lt;/code&gt;&lt;/li&gt;
8780 &lt;/ul&gt;
8781 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
8782 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Python 3.8 brings a controversial &lt;strong&gt;walrus operator&lt;/strong&gt; (&lt;code&gt;:=&lt;/code&gt;), which is an &lt;a href=&quot;https://www.python.org/dev/peps/pep-0572/&quot;&gt;assignment expression&lt;/a&gt;. With it, you can evaluate an expression and assign the result to a variable at the same time, even within another expression!&lt;/p&gt;
8783 &lt;p&gt;Take a look at this example, which calls an expensive function once and then reuses the result for further computation:&lt;/p&gt;
8784 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Python 3.8+&lt;/span&gt;
8785 &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
8786 &lt;/pre&gt;&lt;/div&gt;
8787 
8788 &lt;p&gt;This is useful for simplifying the code without losing its efficiency. Typically, performant code tends to be more verbose:&lt;/p&gt;
8789 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8790 &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
8791 &lt;/pre&gt;&lt;/div&gt;
8792 
8793 &lt;p&gt;The controversy behind this new piece of syntax caused a lot of argument. An abundance of negative comments and heated debates eventually led Guido van Rossum to step down from the &lt;strong&gt;Benevolent Dictator For Life&lt;/strong&gt; or BDFL position.&lt;/p&gt;
8794 &lt;/div&gt;
8795 &lt;p&gt;Statements are usually comprised of reserved keywords such as &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;for&lt;/code&gt;, or &lt;code&gt;print&lt;/code&gt; that have fixed meaning in the language. You can&amp;rsquo;t use them to name your variables or other symbols. That&amp;rsquo;s why redefining or mocking the &lt;code&gt;print&lt;/code&gt; statement isn&amp;rsquo;t possible in Python 2. You&amp;rsquo;re stuck with what you get.&lt;/p&gt;
8796 &lt;p&gt;Furthermore, you can&amp;rsquo;t print from anonymous functions, because statements aren&amp;rsquo;t accepted in lambda expressions:&lt;/p&gt;
8797 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;hello world&amp;#39;&lt;/span&gt;
8798   File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;
8799     &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;hello world&amp;#39;&lt;/span&gt;
8800                 &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;
8801 &lt;span class=&quot;gr&quot;&gt;SyntaxError&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;invalid syntax&lt;/span&gt;
8802 &lt;/pre&gt;&lt;/div&gt;
8803 
8804 &lt;p&gt;The syntax of the &lt;code&gt;print&lt;/code&gt; statement is ambiguous. Sometimes you can add parentheses around the message, and they&amp;rsquo;re completely optional:&lt;/p&gt;
8805 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Please wait...&amp;#39;&lt;/span&gt;
8806 &lt;span class=&quot;go&quot;&gt;Please wait...&lt;/span&gt;
8807 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Please wait...&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8808 &lt;span class=&quot;go&quot;&gt;Please wait...&lt;/span&gt;
8809 &lt;/pre&gt;&lt;/div&gt;
8810 
8811 &lt;p&gt;At other times they change how the message is printed:&lt;/p&gt;
8812 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;My name is&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;John&amp;#39;&lt;/span&gt;
8813 &lt;span class=&quot;go&quot;&gt;My name is John&lt;/span&gt;
8814 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;My name is&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;John&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8815 &lt;span class=&quot;go&quot;&gt;(&amp;#39;My name is&amp;#39;, &amp;#39;John&amp;#39;)&lt;/span&gt;
8816 &lt;/pre&gt;&lt;/div&gt;
8817 
8818 &lt;p&gt;String concatenation can raise a &lt;code&gt;TypeError&lt;/code&gt; due to incompatible types, which you have to handle manually, for example:&lt;/p&gt;
8819 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;jdoe&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;is&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;years old&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
8820 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39; &amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
8821 &lt;span class=&quot;go&quot;&gt;jdoe is 42 years old&lt;/span&gt;
8822 &lt;/pre&gt;&lt;/div&gt;
8823 
8824 &lt;p&gt;Compare this with similar code in Python 3, which leverages sequence unpacking:&lt;/p&gt;
8825 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;jdoe&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;is&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;years old&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
8826 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Python 3&lt;/span&gt;
8827 &lt;span class=&quot;go&quot;&gt;jdoe is 42 years old&lt;/span&gt;
8828 &lt;/pre&gt;&lt;/div&gt;
8829 
8830 &lt;p&gt;There aren&amp;rsquo;t any keyword arguments for common tasks such as flushing the buffer or stream redirection. You need to remember the quirky syntax instead. Even the built-in &lt;code&gt;help()&lt;/code&gt; function isn&amp;rsquo;t that helpful with regards to the &lt;code&gt;print&lt;/code&gt; statement:&lt;/p&gt;
8831 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;help&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8832   File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;
8833     &lt;span class=&quot;n&quot;&gt;help&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8834              &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;
8835 &lt;span class=&quot;gr&quot;&gt;SyntaxError&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;invalid syntax&lt;/span&gt;
8836 &lt;/pre&gt;&lt;/div&gt;
8837 
8838 &lt;p&gt;Trailing newline removal doesn&amp;rsquo;t work quite right, because it adds an unwanted space. You can&amp;rsquo;t compose multiple &lt;code&gt;print&lt;/code&gt; statements together, and, on top of that, you have to be extra diligent about character encoding.&lt;/p&gt;
8839 &lt;p&gt;The list of problems goes on and on. If you&amp;rsquo;re curious, you can jump back to the &lt;a href=&quot;#printing-in-a-nutshell&quot;&gt;previous section&lt;/a&gt; and look for more detailed explanations of the syntax in Python 2.&lt;/p&gt;
8840 &lt;p&gt;However, you can mitigate some of those problems with a much simpler approach. It turns out the &lt;code&gt;print()&lt;/code&gt; function was backported to ease the migration to Python 3. You can import it from a special &lt;code&gt;__future__&lt;/code&gt; module, which exposes a selection of language features released in later Python versions.&lt;/p&gt;
8841 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
8842 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You may import future functions as well as baked-in language constructs such as the &lt;code&gt;with&lt;/code&gt; statement.&lt;/p&gt;
8843 &lt;p&gt;To find out exactly what features are available to you, inspect the module:&lt;/p&gt;
8844 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;__future__&lt;/span&gt;
8845 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__future__&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all_feature_names&lt;/span&gt;
8846 &lt;span class=&quot;go&quot;&gt;[&amp;#39;nested_scopes&amp;#39;,&lt;/span&gt;
8847 &lt;span class=&quot;go&quot;&gt; &amp;#39;generators&amp;#39;,&lt;/span&gt;
8848 &lt;span class=&quot;go&quot;&gt; &amp;#39;division&amp;#39;,&lt;/span&gt;
8849 &lt;span class=&quot;go&quot;&gt; &amp;#39;absolute_import&amp;#39;,&lt;/span&gt;
8850 &lt;span class=&quot;go&quot;&gt; &amp;#39;with_statement&amp;#39;,&lt;/span&gt;
8851 &lt;span class=&quot;go&quot;&gt; &amp;#39;print_function&amp;#39;,&lt;/span&gt;
8852 &lt;span class=&quot;go&quot;&gt; &amp;#39;unicode_literals&amp;#39;]&lt;/span&gt;
8853 &lt;/pre&gt;&lt;/div&gt;
8854 
8855 &lt;p&gt;You could also call &lt;code&gt;dir(__future__)&lt;/code&gt;, but that would show a lot of uninteresting internal details of the module.&lt;/p&gt;
8856 &lt;/div&gt;
8857 &lt;p&gt;To enable the &lt;code&gt;print()&lt;/code&gt; function in Python 2, you need to add this import statement at the beginning of your source code:&lt;/p&gt;
8858 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;__future__&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print_function&lt;/span&gt;
8859 &lt;/pre&gt;&lt;/div&gt;
8860 
8861 &lt;p&gt;From now on the &lt;code&gt;print&lt;/code&gt; statement is no longer available, but you have the &lt;code&gt;print()&lt;/code&gt; function at your disposal. Note that it isn&amp;rsquo;t the same function like the one in Python 3, because it&amp;rsquo;s missing the &lt;code&gt;flush&lt;/code&gt; keyword argument, but the rest of the arguments are the same.&lt;/p&gt;
8862 &lt;p&gt;Other than that, it doesn&amp;rsquo;t spare you from managing character encodings properly.&lt;/p&gt;
8863 &lt;p&gt;Here&amp;rsquo;s an example of calling the &lt;code&gt;print()&lt;/code&gt; function in Python 2:&lt;/p&gt;
8864 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;__future__&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print_function&lt;/span&gt;
8865 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
8866 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;I am a function in Python&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;version_info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;major&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8867 &lt;span class=&quot;go&quot;&gt;I am a function in Python 2&lt;/span&gt;
8868 &lt;/pre&gt;&lt;/div&gt;
8869 
8870 &lt;p&gt;You now have an idea of how printing in Python evolved and, most importantly, understand why these backward-incompatible changes were necessary. Knowing this will surely help you become a better Python programmer.&lt;/p&gt;
8871 &lt;h2 id=&quot;printing-with-style&quot;&gt;Printing With Style&lt;/h2&gt;
8872 &lt;p&gt;If you thought that printing was only about lighting pixels up on the screen, then technically you&amp;rsquo;d be right. However, there are ways to make it look cool. In this section, you&amp;rsquo;ll find out how to format complex data structures, add colors and other decorations, build interfaces, use animation, and even play sounds with text! &lt;/p&gt;
8873 &lt;h3 id=&quot;pretty-printing-nested-data-structures&quot;&gt;Pretty-Printing Nested Data Structures&lt;/h3&gt;
8874 &lt;p&gt;Computer languages allow you to represent data as well as executable code in a structured way. Unlike Python, however, most languages give you a lot of freedom in using whitespace and formatting. This can be useful, for example in compression, but it sometimes leads to less readable code.&lt;/p&gt;
8875 &lt;p&gt;Pretty-printing is about making a piece of data or code look more appealing to the human eye so that it can be understood more easily. This is done by indenting certain lines, inserting newlines, reordering elements, and so forth.&lt;/p&gt;
8876 &lt;p&gt;Python comes with the &lt;code&gt;pprint&lt;/code&gt; module in its standard library, which will help you in pretty-printing large data structures that don&amp;rsquo;t fit on a single line. Because it prints in a more human-friendly way, many popular &lt;a href=&quot;https://realpython.com/interacting-with-python/&quot;&gt;REPL&lt;/a&gt; tools, including &lt;a href=&quot;https://realpython.com/jupyter-notebook-introduction/&quot;&gt;JupyterLab and IPython&lt;/a&gt;, use it by default in place of the regular &lt;code&gt;print()&lt;/code&gt; function.&lt;/p&gt;
8877 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
8878 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; To toggle pretty printing in IPython, issue the following command:&lt;/p&gt;
8879 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;pprint&lt;/span&gt;
8880 &lt;span class=&quot;go&quot;&gt;Pretty printing has been turned OFF&lt;/span&gt;
8881 &lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;pprint&lt;/span&gt;
8882 &lt;span class=&quot;go&quot;&gt;Pretty printing has been turned ON&lt;/span&gt;
8883 &lt;/pre&gt;&lt;/div&gt;
8884 
8885 &lt;p&gt;This is an example of &lt;strong&gt;Magic&lt;/strong&gt; in IPython. There are a lot of built-in commands that start with a percent sign (&lt;code&gt;%&lt;/code&gt;), but you can find more on &lt;a href=&quot;https://pypi.org/&quot;&gt;PyPI&lt;/a&gt;, or even create your own.&lt;/p&gt;
8886 &lt;/div&gt;
8887 &lt;p&gt;If you don&amp;rsquo;t care about not having access to the original &lt;code&gt;print()&lt;/code&gt; function, then you can replace it with &lt;code&gt;pprint()&lt;/code&gt; in your code using import renaming:&lt;/p&gt;
8888 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pprint&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pprint&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;
8889 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;
8890 &lt;span class=&quot;go&quot;&gt;&amp;lt;function pprint at 0x7f7a775a3510&amp;gt;&lt;/span&gt;
8891 &lt;/pre&gt;&lt;/div&gt;
8892 
8893 &lt;p&gt;Personally, I like to have both functions at my fingertips, so I&amp;rsquo;d rather use something like &lt;code&gt;pp&lt;/code&gt; as a short alias:&lt;/p&gt;
8894 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pprint&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pprint&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pp&lt;/span&gt;
8895 &lt;/pre&gt;&lt;/div&gt;
8896 
8897 &lt;p&gt;At first glance, there&amp;rsquo;s hardly any difference between the two functions, and in some cases there&amp;rsquo;s virtually none:&lt;/p&gt;
8898 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8899 &lt;span class=&quot;go&quot;&gt;42&lt;/span&gt;
8900 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8901 &lt;span class=&quot;go&quot;&gt;42&lt;/span&gt;
8902 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hello&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8903 &lt;span class=&quot;go&quot;&gt;hello&lt;/span&gt;
8904 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hello&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8905 &lt;span class=&quot;go&quot;&gt;&amp;#39;hello&amp;#39;  # Did you spot the difference?&lt;/span&gt;
8906 &lt;/pre&gt;&lt;/div&gt;
8907 
8908 &lt;p&gt;That&amp;rsquo;s because &lt;code&gt;pprint()&lt;/code&gt; calls &lt;code&gt;repr()&lt;/code&gt; instead of the usual &lt;code&gt;str()&lt;/code&gt; for type casting, so that you may evaluate its output as Python code if you want to. The differences become apparent as you start feeding it more complex data structures:&lt;/p&gt;
8909 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;powers&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]}&lt;/span&gt;
8910 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8911 &lt;span class=&quot;go&quot;&gt;{&amp;#39;powers&amp;#39;: [0,&lt;/span&gt;
8912 &lt;span class=&quot;go&quot;&gt;            1,&lt;/span&gt;
8913 &lt;span class=&quot;go&quot;&gt;            1024,&lt;/span&gt;
8914 &lt;span class=&quot;go&quot;&gt;            59049,&lt;/span&gt;
8915 &lt;span class=&quot;go&quot;&gt;            1048576,&lt;/span&gt;
8916 &lt;span class=&quot;go&quot;&gt;            9765625,&lt;/span&gt;
8917 &lt;span class=&quot;go&quot;&gt;            60466176,&lt;/span&gt;
8918 &lt;span class=&quot;go&quot;&gt;            282475249,&lt;/span&gt;
8919 &lt;span class=&quot;go&quot;&gt;            1073741824,&lt;/span&gt;
8920 &lt;span class=&quot;go&quot;&gt;            3486784401]}&lt;/span&gt;
8921 &lt;/pre&gt;&lt;/div&gt;
8922 
8923 &lt;p&gt;The function applies reasonable formatting to improve readability, but you can customize it even further with a couple of parameters. For example, you may limit a deeply nested hierarchy by showing an ellipsis below a given level:&lt;/p&gt;
8924 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;USA&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Texas&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Dallas&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Irving&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]}}}&lt;/span&gt;
8925 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;depth&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8926 &lt;span class=&quot;go&quot;&gt;{&amp;#39;USA&amp;#39;: {&amp;#39;Texas&amp;#39;: {&amp;#39;Dallas&amp;#39;: [...]}}}&lt;/span&gt;
8927 &lt;/pre&gt;&lt;/div&gt;
8928 
8929 &lt;p&gt;The ordinary &lt;code&gt;print()&lt;/code&gt; also uses ellipses but for displaying recursive data structures, which form a cycle, to avoid stack overflow error:&lt;/p&gt;
8930 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
8931 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8932 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8933 &lt;span class=&quot;go&quot;&gt;[1, 2, 3, [...]]&lt;/span&gt;
8934 &lt;/pre&gt;&lt;/div&gt;
8935 
8936 &lt;p&gt;However, &lt;code&gt;pprint()&lt;/code&gt; is more explicit about it by including the unique identity of a self-referencing object:&lt;/p&gt;
8937 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8938 &lt;span class=&quot;go&quot;&gt;[1, 2, 3, &amp;lt;Recursion on list with id=140635757287688&amp;gt;]&lt;/span&gt;
8939 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8940 &lt;span class=&quot;go&quot;&gt;140635757287688&lt;/span&gt;
8941 &lt;/pre&gt;&lt;/div&gt;
8942 
8943 &lt;p&gt;The last element in the list is the same object as the entire list.&lt;/p&gt;
8944 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
8945 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Recursive or very large data sets can be dealt with using the &lt;code&gt;reprlib&lt;/code&gt; module as well:&lt;/p&gt;
8946 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;reprlib&lt;/span&gt;
8947 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reprlib&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;repr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;
8948 &lt;span class=&quot;go&quot;&gt;&amp;#39;[0, 1, 1024, 59049, 1048576, 9765625, ...]&amp;#39;&lt;/span&gt;
8949 &lt;/pre&gt;&lt;/div&gt;
8950 
8951 &lt;p&gt;This module supports most of the built-in types and is used by the Python debugger.&lt;/p&gt;
8952 &lt;/div&gt;
8953 &lt;p&gt;&lt;code&gt;pprint()&lt;/code&gt; automatically sorts dictionary keys for you before printing, which allows for consistent comparison. When you&amp;rsquo;re comparing strings, you often don&amp;rsquo;t care about a particular order of serialized attributes. Anyways, it&amp;rsquo;s always best to compare actual dictionaries before serialization.&lt;/p&gt;
8954 &lt;p&gt;Dictionaries often represent &lt;a href=&quot;https://realpython.com/python-json/&quot;&gt;JSON data&lt;/a&gt;, which is widely used on the Internet. To correctly serialize a dictionary into a valid JSON-formatted string, you can take advantage of the &lt;code&gt;json&lt;/code&gt; module. It too has pretty-printing capabilities:&lt;/p&gt;
8955 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;json&lt;/span&gt;
8956 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;username&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;jdoe&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;password&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;s3cret&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
8957 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ugly&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dumps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8958 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pretty&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dumps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;indent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sort_keys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8959 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ugly&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8960 &lt;span class=&quot;go&quot;&gt;{&amp;quot;username&amp;quot;: &amp;quot;jdoe&amp;quot;, &amp;quot;password&amp;quot;: &amp;quot;s3cret&amp;quot;}&lt;/span&gt;
8961 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pretty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
8962 &lt;span class=&quot;go&quot;&gt;{&lt;/span&gt;
8963 &lt;span class=&quot;go&quot;&gt;    &amp;quot;password&amp;quot;: &amp;quot;s3cret&amp;quot;,&lt;/span&gt;
8964 &lt;span class=&quot;go&quot;&gt;    &amp;quot;username&amp;quot;: &amp;quot;jdoe&amp;quot;&lt;/span&gt;
8965 &lt;span class=&quot;go&quot;&gt;}&lt;/span&gt;
8966 &lt;/pre&gt;&lt;/div&gt;
8967 
8968 &lt;p&gt;Notice, however, that you need to handle printing yourself, because it&amp;rsquo;s not something you&amp;rsquo;d typically want to do. Similarly, the &lt;code&gt;pprint&lt;/code&gt; module has an additional &lt;code&gt;pformat()&lt;/code&gt; function that returns a string, in case you had to do something other than printing it.&lt;/p&gt;
8969 &lt;p&gt;Surprisingly, the signature of &lt;code&gt;pprint()&lt;/code&gt; is nothing like the &lt;code&gt;print()&lt;/code&gt; function&amp;rsquo;s one. You can&amp;rsquo;t even pass more than one positional argument, which shows how much it focuses on printing data structures.&lt;/p&gt;
8970 &lt;h3 id=&quot;adding-colors-with-ansi-escape-sequences&quot;&gt;Adding Colors With ANSI Escape Sequences&lt;/h3&gt;
8971 &lt;p&gt;As personal computers got more sophisticated, they had better graphics and could display more colors. However, different vendors had their own idea about the API design for controlling it. That changed a few decades ago when people at the American National Standards Institute decided to unify it by defining &lt;a href=&quot;https://en.wikipedia.org/wiki/ANSI_escape_code&quot;&gt;ANSI escape codes&lt;/a&gt;.&lt;/p&gt;
8972 &lt;p&gt;Most of today&amp;rsquo;s terminal emulators support this standard to some degree. Until recently, the Windows operating system was a notable exception. Therefore, if you want the best portability, use the &lt;a href=&quot;https://pypi.org/project/colorama/&quot;&gt;&lt;code&gt;colorama&lt;/code&gt;&lt;/a&gt; library in Python. It translates ANSI codes to their appropriate counterparts in Windows while keeping them intact in other operating systems.&lt;/p&gt;
8973 &lt;p&gt;To check if your terminal understands a subset of the ANSI escape sequences, for example, related to colors, you can try using the following command:&lt;/p&gt;
8974 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; tput colors
8975 &lt;/pre&gt;&lt;/div&gt;
8976 
8977 &lt;p&gt;My default terminal on Linux says it can display 256 distinct colors, while xterm gives me only 8. The command would return a negative number if colors were unsupported.&lt;/p&gt;
8978 &lt;p&gt;ANSI escape sequences are like a markup language for the terminal. In HTML you work with tags, such as &lt;code&gt;&amp;lt;b&amp;gt;&lt;/code&gt; or &lt;code&gt;&amp;lt;i&amp;gt;&lt;/code&gt;, to change how elements look in the document. These tags are mixed with your content, but they&amp;rsquo;re not visible themselves. Similarly, escape codes won&amp;rsquo;t show up in the terminal as long as it recognizes them. Otherwise, they&amp;rsquo;ll appear in the literal form as if you were viewing the source of a website.&lt;/p&gt;
8979 &lt;p&gt;As its name implies, a sequence must begin with the non-printable &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-escape&quot;&gt;Esc&lt;/kbd&gt;&lt;/span&gt; character, whose ASCII value is 27, sometimes denoted as &lt;code&gt;0x1b&lt;/code&gt; in hexadecimal or &lt;code&gt;033&lt;/code&gt; in octal. You may use Python number literals to quickly verify it&amp;rsquo;s indeed the same number:&lt;/p&gt;
8980 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;27&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x1b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mo&quot;&gt;0o33&lt;/span&gt;
8981 &lt;span class=&quot;go&quot;&gt;True&lt;/span&gt;
8982 &lt;/pre&gt;&lt;/div&gt;
8983 
8984 &lt;p&gt;Additionally, you can obtain it with the &lt;code&gt;\e&lt;/code&gt; escape sequence in the shell:&lt;/p&gt;
8985 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; -e &lt;span class=&quot;s2&quot;&gt;&amp;quot;\e&amp;quot;&lt;/span&gt;
8986 &lt;/pre&gt;&lt;/div&gt;
8987 
8988 &lt;p&gt;The most common ANSI escape sequences take the following form:&lt;/p&gt;
8989 &lt;div class=&quot;table-responsive&quot;&gt;
8990 &lt;table class=&quot;table table-hover&quot;&gt;
8991 &lt;thead&gt;
8992 &lt;tr&gt;
8993 &lt;th&gt;Element&lt;/th&gt;
8994 &lt;th&gt;Description&lt;/th&gt;
8995 &lt;th&gt;Example&lt;/th&gt;
8996 &lt;/tr&gt;
8997 &lt;/thead&gt;
8998 &lt;tbody&gt;
8999 &lt;tr&gt;
9000 &lt;td&gt;&lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-escape&quot;&gt;Esc&lt;/kbd&gt;&lt;/span&gt;&lt;/td&gt;
9001 &lt;td&gt;non-printable escape character&lt;/td&gt;
9002 &lt;td&gt;&lt;code&gt;\033&lt;/code&gt;&lt;/td&gt;
9003 &lt;/tr&gt;
9004 &lt;tr&gt;
9005 &lt;td&gt;&lt;code&gt;[&lt;/code&gt;&lt;/td&gt;
9006 &lt;td&gt;opening square bracket&lt;/td&gt;
9007 &lt;td&gt;&lt;code&gt;[&lt;/code&gt;&lt;/td&gt;
9008 &lt;/tr&gt;
9009 &lt;tr&gt;
9010 &lt;td&gt;numeric code&lt;/td&gt;
9011 &lt;td&gt;one or more numbers separated with &lt;code&gt;;&lt;/code&gt;&lt;/td&gt;
9012 &lt;td&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
9013 &lt;/tr&gt;
9014 &lt;tr&gt;
9015 &lt;td&gt;character code&lt;/td&gt;
9016 &lt;td&gt;uppercase or lowercase letter&lt;/td&gt;
9017 &lt;td&gt;&lt;code&gt;m&lt;/code&gt;&lt;/td&gt;
9018 &lt;/tr&gt;
9019 &lt;/tbody&gt;
9020 &lt;/table&gt;
9021 &lt;/div&gt;
9022 &lt;p&gt;The &lt;strong&gt;numeric code&lt;/strong&gt; can be one or more numbers separated with a semicolon, while the &lt;strong&gt;character code&lt;/strong&gt; is just one letter. Their specific meaning is defined by the ANSI standard. For example, to reset all formatting, you would type one of the following commands, which use the code zero and the letter &lt;code&gt;m&lt;/code&gt;:&lt;/p&gt;
9023 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; -e &lt;span class=&quot;s2&quot;&gt;&amp;quot;\e[0m&amp;quot;&lt;/span&gt;
9024 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; -e &lt;span class=&quot;s2&quot;&gt;&amp;quot;\x1b[0m&amp;quot;&lt;/span&gt;
9025 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; -e &lt;span class=&quot;s2&quot;&gt;&amp;quot;\033[0m&amp;quot;&lt;/span&gt;
9026 &lt;/pre&gt;&lt;/div&gt;
9027 
9028 &lt;p&gt;At the other end of the spectrum, you have compound code values. To set foreground and background with RGB channels, given that your terminal supports 24-bit depth, you could provide multiple numbers:&lt;/p&gt;
9029 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; -e &lt;span class=&quot;s2&quot;&gt;&amp;quot;\e[38;2;0;0;0m\e[48;2;255;255;255mBlack on white\e[0m&amp;quot;&lt;/span&gt;
9030 &lt;/pre&gt;&lt;/div&gt;
9031 
9032 &lt;p&gt;It&amp;rsquo;s not just text color that you can set with the ANSI escape codes. You can, for example, clear and scroll the terminal window, change its background, move the cursor around, make the text blink or decorate it with an underline.&lt;/p&gt;
9033 &lt;p&gt;In Python, you&amp;rsquo;d probably write a helper function to allow for wrapping arbitrary codes into a sequence:&lt;/p&gt;
9034 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;esc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9035 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\033&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{code}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;m&amp;#39;&lt;/span&gt;
9036 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
9037 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;esc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;31;1;4&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;really&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;esc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39; important&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9038 &lt;/pre&gt;&lt;/div&gt;
9039 
9040 &lt;p&gt;This would make the word &lt;code&gt;really&lt;/code&gt; appear in red, bold, and underlined font:&lt;/p&gt;
9041 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/ansi.21ed85878eb9.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/ansi.21ed85878eb9.png&quot; width=&quot;576&quot; height=&quot;236&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/ansi.21ed85878eb9.png&amp;amp;w=144&amp;amp;sig=1b3e337786114c9e099a662d5e096ce1d41ba6a5 144w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/ansi.21ed85878eb9.png&amp;amp;w=288&amp;amp;sig=cb8720ce7fb4db092d674380ef3bf3543503546a 288w, https://files.realpython.com/media/ansi.21ed85878eb9.png 576w&quot; sizes=&quot;75vw&quot; alt=&quot;Text formatted with ANSI escape codes&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
9042 &lt;p&gt;However, there are higher-level abstractions over ANSI escape codes, such as the mentioned &lt;code&gt;colorama&lt;/code&gt; library, as well as tools for building user interfaces in the console.&lt;/p&gt;
9043 &lt;h3 id=&quot;building-console-user-interfaces&quot;&gt;Building Console User Interfaces&lt;/h3&gt;
9044 &lt;p&gt;While playing with ANSI escape codes is undeniably a ton of fun, in the real world you&amp;rsquo;d rather have more abstract building blocks to put together a user interface. There are a few libraries that provide such a high level of control over the terminal, but &lt;a href=&quot;https://docs.python.org/3/howto/curses.html&quot;&gt;&lt;code&gt;curses&lt;/code&gt;&lt;/a&gt; seems to be the most popular choice.&lt;/p&gt;
9045 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
9046 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; To use the &lt;code&gt;curses&lt;/code&gt; library in Windows, you need to install a third-party package:&lt;/p&gt;
9047 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;C:\&amp;gt; pip install windows-curses&lt;/span&gt;
9048 &lt;/pre&gt;&lt;/div&gt;
9049 
9050 &lt;p&gt;That&amp;rsquo;s because &lt;code&gt;curses&lt;/code&gt; isn&amp;rsquo;t available in the standard library of the Python distribution for Windows.&lt;/p&gt;
9051 &lt;/div&gt;
9052 &lt;p&gt;Primarily, it allows you to think in terms of independent graphical widgets instead of a blob of text. Besides, you get a lot of freedom in expressing your inner artist, because it&amp;rsquo;s really like painting a blank canvas. The library hides the complexities of having to deal with different terminals. Other than that, it has great support for keyboard events, which might be useful for writing video games.&lt;/p&gt;
9053 &lt;p&gt;How about making a retro snake game? Let&amp;rsquo;s create a Python snake simulator:&lt;/p&gt;
9054 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/snake.a9589582b58a.gif&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/snake.a9589582b58a.gif&quot; width=&quot;576&quot; height=&quot;392&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/snake.a9589582b58a.gif&amp;amp;w=144&amp;amp;sig=a02bac6a4123b5c1a10112973045e6deec2895c3 144w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/snake.a9589582b58a.gif&amp;amp;w=288&amp;amp;sig=0019bb69f00b55d6201acf7661b870f865758f61 288w, https://files.realpython.com/media/snake.a9589582b58a.gif 576w&quot; sizes=&quot;75vw&quot; alt=&quot;The retro snake game built with curses library&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
9055 &lt;p&gt;First, you need to import the &lt;code&gt;curses&lt;/code&gt; module. Since it modifies the state of a running terminal, it&amp;rsquo;s important to handle errors and gracefully restore the previous state. You can do this manually, but the library comes with a convenient wrapper for your main function:&lt;/p&gt;
9056 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;curses&lt;/span&gt;
9057 
9058 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9059     &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
9060 
9061 &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vm&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;__main__&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9062     &lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wrapper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9063 &lt;/pre&gt;&lt;/div&gt;
9064 
9065 &lt;p&gt;Note, the function must accept a reference to the screen object, also known as &lt;code&gt;stdscr&lt;/code&gt;, that you&amp;rsquo;ll use later for additional setup.&lt;/p&gt;
9066 &lt;p&gt;If you run this program now, you won&amp;rsquo;t see any effects, because it terminates immediately. However, you can add a small delay to have a sneak peek:&lt;/p&gt;
9067 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;curses&lt;/span&gt;
9068 
9069 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9070     &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9071 
9072 &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vm&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;__main__&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9073     &lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wrapper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9074 &lt;/pre&gt;&lt;/div&gt;
9075 
9076 &lt;p&gt;This time the screen went completely blank for a second, but the cursor was still blinking. To hide it, just call one of the configuration functions defined in the module:&lt;/p&gt;
9077 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;curses&lt;/span&gt;
9078 
9079 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9080 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;curs_set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Hide the cursor&lt;/span&gt;
9081 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9082 
9083 &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vm&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;__main__&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9084     &lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wrapper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9085 &lt;/pre&gt;&lt;/div&gt;
9086 
9087 &lt;p&gt;Let&amp;rsquo;s define the snake as a list of points in screen coordinates:&lt;/p&gt;
9088 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;snake&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;reversed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]&lt;/span&gt;
9089 &lt;/pre&gt;&lt;/div&gt;
9090 
9091 &lt;p&gt;The head of the snake is always the first element in the list, whereas the tail is the last one. The initial shape of the snake is horizontal, starting from the top-left corner of the screen and facing to the right. While its y-coordinate stays at zero, its x-coordinate decreases from head to tail.&lt;/p&gt;
9092 &lt;p&gt;To draw the snake, you&amp;rsquo;ll start with the head and then follow with the remaining segments. Each segment carries &lt;code&gt;(y, x)&lt;/code&gt; coordinates, so you can unpack them:&lt;/p&gt;
9093 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Draw the snake&lt;/span&gt;
9094 &lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addstr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;snake&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;@&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9095 &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;segment&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;snake&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]:&lt;/span&gt;
9096     &lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addstr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;segment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;*&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9097 &lt;/pre&gt;&lt;/div&gt;
9098 
9099 &lt;p&gt;Again, if you run this code now, it won&amp;rsquo;t display anything, because you must explicitly refresh the screen afterward:&lt;/p&gt;
9100 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;curses&lt;/span&gt;
9101 
9102 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9103     &lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;curs_set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Hide the cursor&lt;/span&gt;
9104 
9105     &lt;span class=&quot;n&quot;&gt;snake&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;reversed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]&lt;/span&gt;
9106 
9107     &lt;span class=&quot;c1&quot;&gt;# Draw the snake&lt;/span&gt;
9108     &lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addstr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;snake&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;@&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9109     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;segment&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;snake&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]:&lt;/span&gt;
9110         &lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addstr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;segment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;*&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9111 
9112 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;refresh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9113 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9114 
9115 &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vm&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;__main__&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9116     &lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wrapper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9117 &lt;/pre&gt;&lt;/div&gt;
9118 
9119 &lt;p&gt;You want to move the snake in one of four directions, which can be defined as vectors. Eventually, the direction will change in response to an arrow keystroke, so you may hook it up to the library&amp;rsquo;s key codes:&lt;/p&gt;
9120 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;directions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
9121     &lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;KEY_UP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
9122     &lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;KEY_DOWN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
9123     &lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;KEY_LEFT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
9124     &lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;KEY_RIGHT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
9125 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
9126 
9127 &lt;span class=&quot;n&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;directions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;KEY_RIGHT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
9128 &lt;/pre&gt;&lt;/div&gt;
9129 
9130 &lt;p&gt;How does a snake move? It turns out that only its head really moves to a new location, while all other segments shift towards it. In each step, almost all segments remain the same, except for the head and the tail. Assuming the snake isn&amp;rsquo;t growing, you can remove the tail and insert a new head at the beginning of the list:&lt;/p&gt;
9131 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Move the snake&lt;/span&gt;
9132 &lt;span class=&quot;n&quot;&gt;snake&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9133 &lt;span class=&quot;n&quot;&gt;snake&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;snake&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;direction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;
9134 &lt;/pre&gt;&lt;/div&gt;
9135 
9136 &lt;p&gt;To get the new coordinates of the head, you need to add the direction vector to it. However, adding tuples in Python results in a bigger tuple instead of the algebraic sum of the corresponding vector components. One way to fix this is by using the built-in &lt;code&gt;zip()&lt;/code&gt;, &lt;code&gt;sum()&lt;/code&gt;, and &lt;code&gt;map()&lt;/code&gt; functions.&lt;/p&gt;
9137 &lt;p&gt;The direction will change on a keystroke, so you need to call &lt;code&gt;.getch()&lt;/code&gt; to obtain the pressed key code. However, if the pressed key doesn&amp;rsquo;t correspond to the arrow keys defined earlier as dictionary keys, the direction won&amp;rsquo;t change:&lt;/p&gt;
9138 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Change direction on arrow keystroke&lt;/span&gt;
9139 &lt;span class=&quot;n&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;directions&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;direction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9140 &lt;/pre&gt;&lt;/div&gt;
9141 
9142 &lt;p&gt;By default, however, &lt;code&gt;.getch()&lt;/code&gt; is a blocking call that would prevent the snake from moving unless there was a keystroke. Therefore, you need to make the call non-blocking by adding yet another configuration:&lt;/p&gt;
9143 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9144     &lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;curs_set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Hide the cursor&lt;/span&gt;
9145 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nodelay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Don&amp;#39;t block I/O calls&lt;/span&gt;
9146 &lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
9147 
9148 &lt;p&gt;You&amp;rsquo;re almost done, but there&amp;rsquo;s just one last thing left. If you now loop this code, the snake will appear to be growing instead of moving. That&amp;rsquo;s because you have to erase the screen explicitly before each iteration.&lt;/p&gt;
9149 &lt;p&gt;Finally, this is all you need to play the snake game in Python:&lt;/p&gt;
9150 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;curses&lt;/span&gt;
9151 
9152 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9153     &lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;curs_set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Hide the cursor&lt;/span&gt;
9154     &lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nodelay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Don&amp;#39;t block I/O calls&lt;/span&gt;
9155 
9156     &lt;span class=&quot;n&quot;&gt;directions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
9157         &lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;KEY_UP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
9158         &lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;KEY_DOWN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
9159         &lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;KEY_LEFT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
9160         &lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;KEY_RIGHT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
9161     &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
9162 
9163     &lt;span class=&quot;n&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;directions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;KEY_RIGHT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
9164     &lt;span class=&quot;n&quot;&gt;snake&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;reversed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]&lt;/span&gt;
9165 
9166     &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9167 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;erase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9168 &lt;/span&gt;
9169         &lt;span class=&quot;c1&quot;&gt;# Draw the snake&lt;/span&gt;
9170         &lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addstr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;snake&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;@&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9171         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;segment&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;snake&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]:&lt;/span&gt;
9172             &lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addstr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;segment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;*&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9173 
9174         &lt;span class=&quot;c1&quot;&gt;# Move the snake&lt;/span&gt;
9175         &lt;span class=&quot;n&quot;&gt;snake&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9176         &lt;span class=&quot;n&quot;&gt;snake&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;snake&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;direction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;
9177 
9178         &lt;span class=&quot;c1&quot;&gt;# Change direction on arrow keystroke&lt;/span&gt;
9179         &lt;span class=&quot;n&quot;&gt;direction&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;directions&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;direction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9180 
9181         &lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;refresh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9182         &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9183 
9184 &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vm&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;__main__&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9185     &lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wrapper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9186 &lt;/pre&gt;&lt;/div&gt;
9187 
9188 &lt;p&gt;This is merely scratching the surface of the possibilities that the &lt;code&gt;curses&lt;/code&gt; module opens up. You may use it for game development like this or more business-oriented applications.&lt;/p&gt;
9189 &lt;h3 id=&quot;living-it-up-with-cool-animations&quot;&gt;Living It Up With Cool Animations&lt;/h3&gt;
9190 &lt;p&gt;Not only can animations make the user interface more appealing to the eye, but they also improve the overall user experience. When you provide early feedback to the user, for example, they&amp;rsquo;ll know if your program&amp;rsquo;s still working or if it&amp;rsquo;s time to kill it.&lt;/p&gt;
9191 &lt;p&gt;To animate text in the terminal, you have to be able to freely move the cursor around. You can do this with one of the tools mentioned previously, that is ANSI escape codes or the &lt;code&gt;curses&lt;/code&gt; library. However, I&amp;rsquo;d like to show you an even simpler way.&lt;/p&gt;
9192 &lt;p&gt;If the animation can be constrained to a single line of text, then you might be interested in two special escape character sequences:&lt;/p&gt;
9193 &lt;ul&gt;
9194 &lt;li&gt;&lt;strong&gt;Carriage return:&lt;/strong&gt; &lt;code&gt;\r&lt;/code&gt;&lt;/li&gt;
9195 &lt;li&gt;&lt;strong&gt;Backspace:&lt;/strong&gt; &lt;code&gt;\b&lt;/code&gt;&lt;/li&gt;
9196 &lt;/ul&gt;
9197 &lt;p&gt;The first one moves the cursor to the beginning of the line, whereas the second one moves it only one character to the left. They both work in a non-destructive way without overwriting text that&amp;rsquo;s already been written.&lt;/p&gt;
9198 &lt;p&gt;Let&amp;rsquo;s take a look at a few examples.&lt;/p&gt;
9199 &lt;p&gt;You&amp;rsquo;ll often want to display some kind of a &lt;strong&gt;spinning wheel&lt;/strong&gt; to indicate a work in progress without knowing exactly how much time&amp;rsquo;s left to finish:&lt;/p&gt;
9200 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/spinning_wheel.c595af6f83ea.gif&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/spinning_wheel.c595af6f83ea.gif&quot; width=&quot;576&quot; height=&quot;236&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/spinning_wheel.c595af6f83ea.gif&amp;amp;w=144&amp;amp;sig=6668f7484a9d15711f5a9f149647b38baf8683cb 144w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/spinning_wheel.c595af6f83ea.gif&amp;amp;w=288&amp;amp;sig=267bb31016ff764a987006dcecafcf80233af3ad 288w, https://files.realpython.com/media/spinning_wheel.c595af6f83ea.gif 576w&quot; sizes=&quot;75vw&quot; alt=&quot;Indefinite animation in the terminal&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
9201 &lt;p&gt;Many command line tools use this trick while downloading data over the network. You can make a really simple stop motion animation from a sequence of characters that will cycle in a round-robin fashion:&lt;/p&gt;
9202 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;itertools&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cycle&lt;/span&gt;
9203 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;
9204 
9205 &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;frame&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cycle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;-\|/-\|/&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9206     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flush&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9207     &lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9208 &lt;/pre&gt;&lt;/div&gt;
9209 
9210 &lt;p&gt;The loop gets the next character to print, then moves the cursor to the beginning of the line, and overwrites whatever there was before without adding a newline. You don&amp;rsquo;t want extra space between positional arguments, so separator argument must be blank. Also, notice the use of Python&amp;rsquo;s raw strings due to backslash characters present in the literal.&lt;/p&gt;
9211 &lt;p&gt;When you know the remaining time or task completion percentage, then you&amp;rsquo;re able to show an animated progress bar:&lt;/p&gt;
9212 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/progress.6bd055d8dcc4.gif&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/progress.6bd055d8dcc4.gif&quot; width=&quot;576&quot; height=&quot;236&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/progress.6bd055d8dcc4.gif&amp;amp;w=144&amp;amp;sig=b77d52a79d28329d6ec8d5e43fbffd8bc112896e 144w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/progress.6bd055d8dcc4.gif&amp;amp;w=288&amp;amp;sig=57b015e2b6a7534b9a9d027d81a71b52967c20e0 288w, https://files.realpython.com/media/progress.6bd055d8dcc4.gif 576w&quot; sizes=&quot;75vw&quot; alt=&quot;Progress bar animation in the terminal&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
9213 &lt;p&gt;First, you need to calculate how many hashtags to display and how many blank spaces to insert. Next, you erase the line and build the bar from scratch:&lt;/p&gt;
9214 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;
9215 
9216 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;progress&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;percent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9217     &lt;span class=&quot;n&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;percent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;
9218     &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;
9219     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\r&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;[&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;#&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39; &amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;]&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
9220           &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39; &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{percent:.0f}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;%&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
9221           &lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flush&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9222 
9223 &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;101&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9224     &lt;span class=&quot;n&quot;&gt;progress&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9225     &lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9226 &lt;/pre&gt;&lt;/div&gt;
9227 
9228 &lt;p&gt;As before, each request for update repaints the entire line.&lt;/p&gt;
9229 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
9230 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; There&amp;rsquo;s a feature-rich &lt;a href=&quot;https://pypi.org/project/progressbar2/&quot;&gt;&lt;code&gt;progressbar2&lt;/code&gt;&lt;/a&gt; library, along with a few other similar tools, that can show progress in a much more comprehensive way.&lt;/p&gt;
9231 &lt;/div&gt;
9232 &lt;h3 id=&quot;making-sounds-with-print&quot;&gt;Making Sounds With Print&lt;/h3&gt;
9233 &lt;p&gt;If you&amp;rsquo;re old enough to remember computers with a PC speaker, then you must also remember their distinctive &lt;em&gt;beep&lt;/em&gt; sound, often used to indicate hardware problems. They could barely make any more noises than that, yet video games seemed so much better with it.&lt;/p&gt;
9234 &lt;p&gt;Today you can still take advantage of this small loudspeaker, but chances are your laptop didn&amp;rsquo;t come with one. In such a case, you can enable &lt;strong&gt;terminal bell&lt;/strong&gt; emulation in your shell, so that a system warning sound is played instead.&lt;/p&gt;
9235 &lt;p&gt;Go ahead and type this command to see if your terminal can play a sound:&lt;/p&gt;
9236 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; -e &lt;span class=&quot;s2&quot;&gt;&amp;quot;\a&amp;quot;&lt;/span&gt;
9237 &lt;/pre&gt;&lt;/div&gt;
9238 
9239 &lt;p&gt;This would normally print text, but the &lt;code&gt;-e&lt;/code&gt; flag enables the interpretation of backslash escapes. As you can see, there&amp;rsquo;s a dedicated escape sequence &lt;code&gt;\a&lt;/code&gt;, which stands for &amp;ldquo;alert&amp;rdquo;, that outputs a special &lt;a href=&quot;https://en.wikipedia.org/wiki/Bell_character&quot;&gt;bell character&lt;/a&gt;. Some terminals make a sound whenever they see it.&lt;/p&gt;
9240 &lt;p&gt;Similarly, you can print this character in Python. Perhaps in a loop to form some kind of melody. While it&amp;rsquo;s only a single note, you can still vary the length of pauses between consecutive instances. That seems like a perfect toy for Morse code playback!&lt;/p&gt;
9241 &lt;p&gt;The rules are the following:&lt;/p&gt;
9242 &lt;ul&gt;
9243 &lt;li&gt;Letters are encoded with a sequence of &lt;strong&gt;dot&lt;/strong&gt; (&amp;middot;) and &lt;strong&gt;dash&lt;/strong&gt; (&amp;ndash;) symbols.&lt;/li&gt;
9244 &lt;li&gt;A &lt;strong&gt;dot&lt;/strong&gt; is one unit of time.&lt;/li&gt;
9245 &lt;li&gt;A &lt;strong&gt;dash&lt;/strong&gt; is three units of time.&lt;/li&gt;
9246 &lt;li&gt;Individual &lt;strong&gt;symbols&lt;/strong&gt; in a letter are spaced one unit of time apart.&lt;/li&gt;
9247 &lt;li&gt;Symbols of two adjacent &lt;strong&gt;letters&lt;/strong&gt; are spaced three units of time apart.&lt;/li&gt;
9248 &lt;li&gt;Symbols of two adjacent &lt;strong&gt;words&lt;/strong&gt; are spaced seven units of time apart.&lt;/li&gt;
9249 &lt;/ul&gt;
9250 &lt;p&gt;According to those rules, you could be &amp;ldquo;printing&amp;rdquo; an SOS signal indefinitely in the following way:&lt;/p&gt;
9251 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9252     &lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9253     &lt;span class=&quot;n&quot;&gt;symbol_space&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9254     &lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9255     &lt;span class=&quot;n&quot;&gt;symbol_space&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9256     &lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9257     &lt;span class=&quot;n&quot;&gt;letter_space&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9258     &lt;span class=&quot;n&quot;&gt;dash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9259     &lt;span class=&quot;n&quot;&gt;symbol_space&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9260     &lt;span class=&quot;n&quot;&gt;dash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9261     &lt;span class=&quot;n&quot;&gt;symbol_space&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9262     &lt;span class=&quot;n&quot;&gt;dash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9263     &lt;span class=&quot;n&quot;&gt;letter_space&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9264     &lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9265     &lt;span class=&quot;n&quot;&gt;symbol_space&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9266     &lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9267     &lt;span class=&quot;n&quot;&gt;symbol_space&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9268     &lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9269     &lt;span class=&quot;n&quot;&gt;word_space&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9270 &lt;/pre&gt;&lt;/div&gt;
9271 
9272 &lt;p&gt;In Python, you can implement it in merely ten lines of code:&lt;/p&gt;
9273 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;
9274 
9275 &lt;span class=&quot;n&quot;&gt;speed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;
9276 
9277 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;signal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;duration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;symbol&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9278     &lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;duration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9279     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;symbol&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flush&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9280 
9281 &lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;signal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;speed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;ยท&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\a&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9282 &lt;span class=&quot;n&quot;&gt;dash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;signal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;speed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;โˆ’&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\a&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9283 &lt;span class=&quot;n&quot;&gt;symbol_space&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;signal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;speed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9284 &lt;span class=&quot;n&quot;&gt;letter_space&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;signal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;speed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9285 &lt;span class=&quot;n&quot;&gt;word_space&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;signal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;speed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39; &amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9286 &lt;/pre&gt;&lt;/div&gt;
9287 
9288 &lt;p&gt;Maybe you could even take it one step further and make a command line tool for translating text into Morse code? Either way, I hope you&amp;rsquo;re having fun with this!&lt;/p&gt;
9289 &lt;h2 id=&quot;mocking-python-print-in-unit-tests&quot;&gt;Mocking Python Print in Unit Tests&lt;/h2&gt;
9290 &lt;p&gt;Nowadays, it&amp;rsquo;s expected that you ship code that meets high quality standards. If you aspire to become a professional, you must learn &lt;a href=&quot;https://realpython.com/python-testing/&quot;&gt;how to test&lt;/a&gt; your code.&lt;/p&gt;
9291 &lt;p&gt;Software testing is especially important in dynamically typed languages, such as  Python, which don&amp;rsquo;t have a compiler to warn you about obvious mistakes. Defects can make their way to the production environment and remain dormant for a long time, until that one day when a branch of code finally gets executed.&lt;/p&gt;
9292 &lt;p&gt;Sure, you have &lt;a href=&quot;https://realpython.com/python-code-quality/#linters&quot;&gt;linters&lt;/a&gt;, &lt;a href=&quot;https://realpython.com/python-type-checking/&quot;&gt;type checkers&lt;/a&gt;, and other tools for static code analysis to assist you. But they won&amp;rsquo;t tell you whether your program does what it&amp;rsquo;s supposed to do on the business level.&lt;/p&gt;
9293 &lt;p&gt;So, should you be testing &lt;code&gt;print()&lt;/code&gt;? No. After all, it&amp;rsquo;s a built-in function that must have already gone through a comprehensive suite of tests.  What you want to test, though, is whether your code is calling &lt;code&gt;print()&lt;/code&gt; at the right time with the expected parameters. That&amp;rsquo;s known as a &lt;strong&gt;behavior&lt;/strong&gt;.&lt;/p&gt;
9294 &lt;p&gt;You can test behaviors by &lt;a href=&quot;https://realpython.com/python-mock-library/&quot;&gt;mocking&lt;/a&gt; real objects or functions. In this case, you want to mock &lt;code&gt;print()&lt;/code&gt; to record and verify its invocations.&lt;/p&gt;
9295 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
9296 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You might have heard the terms: &lt;strong&gt;dummy&lt;/strong&gt;, &lt;strong&gt;fake&lt;/strong&gt;, &lt;strong&gt;stub&lt;/strong&gt;, &lt;strong&gt;spy&lt;/strong&gt;, or &lt;strong&gt;mock&lt;/strong&gt; used interchangeably. Some people make a distinction between them, while others don&amp;rsquo;t.&lt;/p&gt;
9297 &lt;p&gt;Martin Fowler explains their differences in a &lt;a href=&quot;https://martinfowler.com/bliki/TestDouble.html&quot;&gt;short glossary&lt;/a&gt; and collectively calls them &lt;strong&gt;test doubles&lt;/strong&gt;.&lt;/p&gt;
9298 &lt;/div&gt;
9299 &lt;p&gt;Mocking in Python can be done twofold. First, you can take the traditional path of statically-typed languages by employing dependency injection. This may sometimes require you to change the code under test, which isn&amp;rsquo;t always possible if the code is defined in an external library:&lt;/p&gt;
9300 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;download&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9301     &lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Downloading &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{url}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9302     &lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;
9303 &lt;/pre&gt;&lt;/div&gt;
9304 
9305 &lt;p&gt;This is the same example I used in an earlier section to talk about function composition. It basically allows for substituting &lt;code&gt;print()&lt;/code&gt; with a custom function of the same interface. To check if it prints the right message, you have to intercept it by injecting a mocked function:&lt;/p&gt;
9306 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mock_print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9307 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;mock_print&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_message&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;
9308 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
9309 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;download&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;resource&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mock_print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9310 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Downloading resource&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mock_print&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_message&lt;/span&gt;
9311 &lt;/pre&gt;&lt;/div&gt;
9312 
9313 &lt;p&gt;Calling this mock makes it save the last message in an attribute, which you can inspect later, for example in an &lt;code&gt;assert&lt;/code&gt; statement.&lt;/p&gt;
9314 &lt;p&gt;In a slightly alternative solution, instead of replacing the entire &lt;code&gt;print()&lt;/code&gt; function with a custom wrapper, you could redirect the standard output to an in-memory file-like stream of characters:&lt;/p&gt;
9315 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;download&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9316 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Downloading &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{url}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9317 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;
9318 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
9319 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;io&lt;/span&gt;
9320 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;memory_buffer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StringIO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9321 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;download&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;app.js&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;memory_buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9322 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;download&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;style.css&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;memory_buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9323 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;memory_buffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getvalue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9324 &lt;span class=&quot;go&quot;&gt;&amp;#39;Downloading app.js\nDownloading style.css\n&amp;#39;&lt;/span&gt;
9325 &lt;/pre&gt;&lt;/div&gt;
9326 
9327 &lt;p&gt;This time the function explicitly calls &lt;code&gt;print()&lt;/code&gt;, but it exposes its &lt;code&gt;file&lt;/code&gt; parameter to the outside world.&lt;/p&gt;
9328 &lt;p&gt;However, a more Pythonic way of mocking objects takes advantage of the built-in &lt;code&gt;mock&lt;/code&gt; module, which uses a technique called &lt;a href=&quot;https://en.wikipedia.org/wiki/Monkey_patch&quot;&gt;monkey patching&lt;/a&gt;. This derogatory name stems from it being a &amp;ldquo;dirty hack&amp;rdquo; that you can easily shoot yourself in the foot with. It&amp;rsquo;s less elegant than dependency injection but definitely quick and convenient.&lt;/p&gt;
9329 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
9330 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The &lt;code&gt;mock&lt;/code&gt; module got absorbed by the standard library in Python 3, but before that, it was a third-party package. You had to install it separately:&lt;/p&gt;
9331 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pip2 install mock
9332 &lt;/pre&gt;&lt;/div&gt;
9333 
9334 &lt;p&gt;Other than that, you referred to it as &lt;code&gt;mock&lt;/code&gt;, whereas in Python 3 it&amp;rsquo;s part of the unit testing module, so you must import from &lt;code&gt;unittest.mock&lt;/code&gt;.&lt;/p&gt;
9335 &lt;/div&gt;
9336 &lt;p&gt;What monkey patching does is alter implementation dynamically at runtime. Such a change is visible globally, so it may have unwanted consequences. In practice, however, patching only affects the code for the duration of test execution.&lt;/p&gt;
9337 &lt;p&gt;To mock &lt;code&gt;print()&lt;/code&gt; in a test case, you&amp;rsquo;ll typically use the &lt;code&gt;@patch&lt;/code&gt; &lt;a href=&quot;https://realpython.com/primer-on-python-decorators/&quot;&gt;decorator&lt;/a&gt; and specify a target for patching by referring to it with a fully qualified name, that is including the module name:&lt;/p&gt;
9338 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;unittest.mock&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;patch&lt;/span&gt;
9339 
9340 &lt;span class=&quot;nd&quot;&gt;@patch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;builtins.print&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9341 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mock_print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9342     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;not a real print&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9343     &lt;span class=&quot;n&quot;&gt;mock_print&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assert_called_with&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;not a real print&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9344 &lt;/pre&gt;&lt;/div&gt;
9345 
9346 &lt;p&gt;This will automatically create the mock for you and inject it to the test function.  However, you need to declare that your test function accepts a mock now. The underlying mock object has lots of useful methods and attributes for verifying behavior.&lt;/p&gt;
9347 &lt;p&gt;Did you notice anything peculiar about that code snippet?&lt;/p&gt;
9348 &lt;p&gt;Despite injecting a mock to the function, you&amp;rsquo;re not calling it directly, although you could. That injected mock is only used to make assertions afterward and maybe to prepare the context before running the test.&lt;/p&gt;
9349 &lt;p&gt;In real life, mocking helps to isolate the code under test by removing dependencies such as a database connection. You rarely call mocks in a test, because that doesn&amp;rsquo;t make much sense. Rather, it&amp;rsquo;s other pieces of code that call your mock indirectly without knowing it.&lt;/p&gt;
9350 &lt;p&gt;Here&amp;rsquo;s what that means:&lt;/p&gt;
9351 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;unittest.mock&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;patch&lt;/span&gt;
9352 
9353 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9354     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9355 
9356 &lt;span class=&quot;nd&quot;&gt;@patch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;builtins.print&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9357 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_greet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mock_print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9358     &lt;span class=&quot;n&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;John&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9359     &lt;span class=&quot;n&quot;&gt;mock_print&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assert_called_with&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello, John!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9360 &lt;/pre&gt;&lt;/div&gt;
9361 
9362 &lt;p&gt;The code under test is a function that prints a greeting. Even though it&amp;rsquo;s a fairly simple function, you can&amp;rsquo;t test it easily because it doesn&amp;rsquo;t return a value. It has a side-effect.&lt;/p&gt;
9363 &lt;p&gt;To eliminate that side-effect, you need to mock the dependency out. Patching lets you avoid making changes to the original function, which can remain agnostic about &lt;code&gt;print()&lt;/code&gt;. It thinks it&amp;rsquo;s calling &lt;code&gt;print()&lt;/code&gt;, but in reality, it&amp;rsquo;s calling a mock you&amp;rsquo;re in total control of.&lt;/p&gt;
9364 &lt;p&gt;There are many reasons for testing software. One of them is looking for bugs. When you write tests, you often want to get rid of the &lt;code&gt;print()&lt;/code&gt; function, for example, by mocking it away. Paradoxically, however, that same function can help you find bugs during a related process of debugging you&amp;rsquo;ll read about in the next section.&lt;/p&gt;
9365 &lt;div class=&quot;card mb-3&quot; id=&quot;collapse_card841486&quot;&gt;
9366 &lt;div class=&quot;card-header border-0&quot;&gt;&lt;p class=&quot;m-0&quot;&gt;&lt;button class=&quot;btn&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapse841486&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapse841486&quot;&gt;Syntax in Python 2&lt;/button&gt; &lt;button class=&quot;btn btn-link float-right&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapse841486&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapse841486&quot;&gt;Show/Hide&lt;/button&gt;&lt;/p&gt;&lt;/div&gt;
9367 &lt;div id=&quot;collapse841486&quot; class=&quot;collapse&quot; data-parent=&quot;#collapse_card841486&quot;&gt;&lt;div class=&quot;card-body&quot; markdown=&quot;1&quot;&gt;
9368 
9369 &lt;p&gt;You can&amp;rsquo;t monkey patch the &lt;code&gt;print&lt;/code&gt; statement in Python 2, nor can you inject it as a dependency. However, you have a few other options:&lt;/p&gt;
9370 &lt;ul&gt;
9371 &lt;li&gt;Use stream redirection.&lt;/li&gt;
9372 &lt;li&gt;Patch the standard output defined in the &lt;code&gt;sys&lt;/code&gt; module.&lt;/li&gt;
9373 &lt;li&gt;Import &lt;code&gt;print()&lt;/code&gt; from the &lt;code&gt;__future__&lt;/code&gt; module.&lt;/li&gt;
9374 &lt;/ul&gt;
9375 &lt;p&gt;Let&amp;rsquo;s examine them one by one.&lt;/p&gt;
9376 &lt;p&gt;Stream redirection is almost identical to the example you saw earlier:&lt;/p&gt;
9377 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;download&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9378 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Downloading &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;
9379 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;
9380 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
9381 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;StringIO&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StringIO&lt;/span&gt;
9382 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;memory_buffer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StringIO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9383 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;download&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;app.js&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;memory_buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9384 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;download&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;style.css&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;memory_buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9385 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;memory_buffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getvalue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9386 &lt;span class=&quot;go&quot;&gt;&amp;#39;Downloading app.js\nDownloading style.css\n&amp;#39;&lt;/span&gt;
9387 &lt;/pre&gt;&lt;/div&gt;
9388 
9389 &lt;p&gt;There are only two differences. First, the syntax for stream redirection uses chevron (&lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt;) instead of the &lt;code&gt;file&lt;/code&gt; argument. The other difference is where &lt;code&gt;StringIO&lt;/code&gt; is defined. You can import it from a similarly named &lt;code&gt;StringIO&lt;/code&gt; module, or &lt;code&gt;cStringIO&lt;/code&gt; for a faster implementation.&lt;/p&gt;
9390 &lt;p&gt;Patching the standard output from the &lt;code&gt;sys&lt;/code&gt; module is exactly what it sounds like, but you need to be aware of a few gotchas:&lt;/p&gt;
9391 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;mock&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;patch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;
9392 
9393 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9394     &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;!&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
9395 
9396 &lt;span class=&quot;nd&quot;&gt;@patch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;sys.stdout&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9397 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_greet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mock_stdout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9398     &lt;span class=&quot;n&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;John&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9399     &lt;span class=&quot;n&quot;&gt;mock_stdout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assert_has_calls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
9400        &lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello, John!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
9401        &lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9402     &lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
9403 &lt;/pre&gt;&lt;/div&gt;
9404 
9405 &lt;p&gt;First of all, remember to install the &lt;code&gt;mock&lt;/code&gt; module as it wasn&amp;rsquo;t available in the standard library in Python 2.&lt;/p&gt;
9406 &lt;p&gt;Secondly, the &lt;code&gt;print&lt;/code&gt; statement calls the underlying &lt;code&gt;.write()&lt;/code&gt; method on the mocked object instead of calling the object itself. That&amp;rsquo;s why you&amp;rsquo;ll run assertions against &lt;code&gt;mock_stdout.write&lt;/code&gt;. &lt;/p&gt;
9407 &lt;p&gt;Finally, a single &lt;code&gt;print&lt;/code&gt; statement doesn&amp;rsquo;t always correspond to a single call to &lt;code&gt;sys.stdout.write()&lt;/code&gt;. In fact, you&amp;rsquo;ll see the newline character written separately.&lt;/p&gt;
9408 &lt;p&gt;The last option you have is importing  &lt;code&gt;print()&lt;/code&gt; from &lt;code&gt;future&lt;/code&gt; and patching it:&lt;/p&gt;
9409 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;__future__&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print_function&lt;/span&gt;
9410 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;mock&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;patch&lt;/span&gt;
9411 
9412 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9413     &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;!&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9414 
9415 &lt;span class=&quot;nd&quot;&gt;@patch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;__builtin__.print&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9416 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_greet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mock_print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9417     &lt;span class=&quot;n&quot;&gt;greet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;John&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9418     &lt;span class=&quot;n&quot;&gt;mock_print&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assert_called_with&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello, John!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9419 &lt;/pre&gt;&lt;/div&gt;
9420 
9421 &lt;p&gt;Again, it&amp;rsquo;s nearly identical to Python 3, but the &lt;code&gt;print()&lt;/code&gt; function is defined in the &lt;code&gt;__builtin__&lt;/code&gt; module rather than &lt;code&gt;builtins&lt;/code&gt;.&lt;/p&gt;
9422 &lt;/div&gt;&lt;/div&gt;
9423 
9424 &lt;/div&gt;
9425 &lt;h2 id=&quot;print-debugging&quot;&gt;Print Debugging&lt;/h2&gt;
9426 &lt;p&gt;In this section, you&amp;rsquo;ll take a look at the available tools for debugging in Python, starting from a humble &lt;code&gt;print()&lt;/code&gt; function, through the &lt;code&gt;logging&lt;/code&gt; module, to a fully fledged debugger. After reading it, you&amp;rsquo;ll be able to make an educated decision about which of them is the most suitable in a given situation.&lt;/p&gt;
9427 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
9428 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Debugging is the process of looking for the root causes of &lt;strong&gt;bugs&lt;/strong&gt; or defects in software after they&amp;rsquo;ve been discovered, as well as taking steps to fix them.&lt;/p&gt;
9429 &lt;p&gt;The term &lt;strong&gt;bug&lt;/strong&gt; has an &lt;a href=&quot;https://en.wikipedia.org/wiki/Debugging#Origin_of_the_term&quot;&gt;amusing story&lt;/a&gt; about the origin of its name.&lt;/p&gt;
9430 &lt;/div&gt;
9431 &lt;h3 id=&quot;tracing&quot;&gt;Tracing&lt;/h3&gt;
9432 &lt;p&gt;Also known as &lt;strong&gt;print debugging&lt;/strong&gt; or &lt;strong&gt;caveman debugging&lt;/strong&gt;, it&amp;rsquo;s the most basic form of debugging. While a little bit old-fashioned, it&amp;rsquo;s still powerful and has its uses.&lt;/p&gt;
9433 &lt;p&gt;The idea is to follow the path of program execution until it stops abruptly, or gives incorrect results, to identify the exact instruction with a problem. You do that by inserting print statements with words that stand out in carefully chosen places.&lt;/p&gt;
9434 &lt;p&gt;Take a look at this example, which manifests a rounding error:&lt;/p&gt;
9435 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;average&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9436 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;debug1:&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9437 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9438 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;debug2:&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
9439 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9440 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
9441 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;average&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
9442 &lt;span class=&quot;go&quot;&gt;debug1: [0.1, 0.1, 0.1]&lt;/span&gt;
9443 &lt;span class=&quot;go&quot;&gt;debug2: 0.30000000000000004&lt;/span&gt;
9444 &lt;span class=&quot;go&quot;&gt;False&lt;/span&gt;
9445 &lt;/pre&gt;&lt;/div&gt;
9446 
9447 &lt;p&gt;As you can see, the function doesn&amp;rsquo;t return the expected value of &lt;code&gt;0.1&lt;/code&gt;, but now you know it&amp;rsquo;s because the sum is a little off. Tracing the state of variables at different steps of the algorithm can give you a hint where the issue is.&lt;/p&gt;
9448 &lt;div class=&quot;card mb-3&quot; id=&quot;collapse_cardab3ddc&quot;&gt;
9449 &lt;div class=&quot;card-header border-0&quot;&gt;&lt;p class=&quot;m-0&quot;&gt;&lt;button class=&quot;btn&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapseab3ddc&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapseab3ddc&quot;&gt;Rounding Error&lt;/button&gt; &lt;button class=&quot;btn btn-link float-right&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapseab3ddc&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapseab3ddc&quot;&gt;Show/Hide&lt;/button&gt;&lt;/p&gt;&lt;/div&gt;
9450 &lt;div id=&quot;collapseab3ddc&quot; class=&quot;collapse&quot; data-parent=&quot;#collapse_cardab3ddc&quot;&gt;&lt;div class=&quot;card-body&quot; markdown=&quot;1&quot;&gt;
9451 
9452 &lt;p&gt;In this case, the problem lies in how &lt;strong&gt;floating point&lt;/strong&gt; numbers are represented in computer memory. Remember that numbers are stored in binary form. Decimal value of &lt;code&gt;0.1&lt;/code&gt; turns out to have an infinite binary representation, which gets rounded.&lt;/p&gt;
9453 &lt;p&gt;For more information on rounding numbers in Python, you can check out &lt;a href=&quot;https://realpython.com/python-rounding/&quot;&gt;How to Round Numbers in Python&lt;/a&gt;.&lt;/p&gt;
9454 &lt;/div&gt;&lt;/div&gt;
9455 
9456 &lt;/div&gt;
9457 &lt;p&gt;This method is simple and intuitive and will work in pretty much every programming language out there. Not to mention, it&amp;rsquo;s a great exercise in the learning process.&lt;/p&gt;
9458 &lt;p&gt;On the other hand, once you master more advanced techniques, it&amp;rsquo;s hard to go back, because they allow you to find bugs much quicker. Tracing is a laborious manual process, which can let even more errors slip through. The build and deploy cycle takes time. Afterward, you need to remember to meticulously remove all the &lt;code&gt;print()&lt;/code&gt; calls you made without accidentally touching the genuine ones.&lt;/p&gt;
9459 &lt;p&gt;Besides, it requires you to make changes in the code, which isn&amp;rsquo;t always possible. Maybe you&amp;rsquo;re debugging an application running in a remote web server or want to diagnose a problem in a &lt;strong&gt;post-mortem&lt;/strong&gt; fashion. Sometimes you simply don&amp;rsquo;t have access to the standard output.&lt;/p&gt;
9460 &lt;p&gt;That&amp;rsquo;s precisely where &lt;a href=&quot;https://realpython.com/courses/logging-python/&quot;&gt;logging&lt;/a&gt; shines.&lt;/p&gt;
9461 &lt;h3 id=&quot;logging&quot;&gt;Logging&lt;/h3&gt;
9462 &lt;p&gt;Let&amp;rsquo;s pretend for a minute that you&amp;rsquo;re running an e-commerce website. One day, an angry customer makes a phone call complaining about a failed transaction and saying he lost his money. He claims to have tried purchasing a few items, but in the end, there was some cryptic error that prevented him from finishing that order. Yet, when he checked his bank account, the money was gone.&lt;/p&gt;
9463 &lt;p&gt;You apologize sincerely and make a refund, but also don&amp;rsquo;t want this to happen again in the future. How do you debug that? If only you had some trace of what happened, ideally in the form of a chronological list of events with their context.&lt;/p&gt;
9464 &lt;p&gt;Whenever you find yourself doing print debugging, consider turning it into permanent log messages. This may help in situations like this, when you need to analyze a problem after it happened, in an environment that you don&amp;rsquo;t have access to.&lt;/p&gt;
9465 &lt;p&gt;There are sophisticated tools for log aggregation and searching, but at the most basic level, you can think of logs as text files. Each line conveys detailed information about an event in your system. Usually, it won&amp;rsquo;t contain personally identifying information, though, in some cases, it may be mandated by law.&lt;/p&gt;
9466 &lt;p&gt;Here&amp;rsquo;s a breakdown of a typical log record:&lt;/p&gt;
9467 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;[2019-06-14 15:18:34,517][DEBUG][root][MainThread] Customer(id=123) logged out
9468 &lt;/pre&gt;&lt;/div&gt;
9469 
9470 &lt;p&gt;As you can see, it has a structured form. Apart from a descriptive message, there are a few customizable fields, which provide the context of an event. Here, you have the exact date and time, the log level, the logger name, and the thread name.&lt;/p&gt;
9471 &lt;p&gt;Log levels allow you to filter messages quickly to reduce noise. If you&amp;rsquo;re looking for an error, you don&amp;rsquo;t want to see all the warnings or debug messages, for example. It&amp;rsquo;s trivial to disable or enable messages at certain log levels through the configuration, without even touching the code.&lt;/p&gt;
9472 &lt;p&gt;With logging, you can keep your debug messages separate from the standard output. All the log messages go to the standard error stream by default, which can conveniently show up in different colors. However, you can redirect log messages to separate files, even for individual modules!&lt;/p&gt;
9473 &lt;p&gt;Quite commonly, misconfigured logging can lead to running out of space on the server&amp;rsquo;s disk. To prevent that, you may set up &lt;strong&gt;log rotation&lt;/strong&gt;, which will keep the log files for a specified duration, such as one week, or once they hit a certain size. Nevertheless, it&amp;rsquo;s always a good practice to archive older logs. Some regulations enforce that customer data be kept for as long as five years!&lt;/p&gt;
9474 &lt;p&gt;Compared to other programming languages, &lt;a href=&quot;https://realpython.com/python-logging/&quot;&gt;logging in Python&lt;/a&gt; is simpler, because the &lt;code&gt;logging&lt;/code&gt; module is bundled with the standard library. You just import and configure it in as little as two lines of code:&lt;/p&gt;
9475 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;logging&lt;/span&gt;
9476 &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;basicConfig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEBUG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9477 &lt;/pre&gt;&lt;/div&gt;
9478 
9479 &lt;p&gt;You can call functions defined at the module level, which are hooked to the &lt;strong&gt;root logger&lt;/strong&gt;, but more the common practice is to obtain a dedicated logger for each of your source files:&lt;/p&gt;
9480 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hello&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Module-level function&lt;/span&gt;
9481 
9482 &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getLogger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;vm&quot;&gt;__name__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9483 &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hello&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# Logger&amp;#39;s method&lt;/span&gt;
9484 &lt;/pre&gt;&lt;/div&gt;
9485 
9486 &lt;p&gt;The advantage of using custom loggers is more fine-grain control. They&amp;rsquo;re usually named after the module they were defined in through the &lt;code&gt;__name__&lt;/code&gt; variable.&lt;/p&gt;
9487 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
9488 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; There&amp;rsquo;s a somewhat related &lt;code&gt;warnings&lt;/code&gt; module in Python, which can also log messages to the standard error stream. However, it has a narrower spectrum of applications, mostly in library code, whereas client applications should use the &lt;code&gt;logging&lt;/code&gt; module.&lt;/p&gt;
9489 &lt;p&gt;That said, you can make them work together by calling &lt;code&gt;logging.captureWarnings(True)&lt;/code&gt;.&lt;/p&gt;
9490 &lt;/div&gt;
9491 &lt;p&gt;One last reason to switch from the &lt;code&gt;print()&lt;/code&gt; function to logging is thread safety. In the upcoming section, you&amp;rsquo;ll see that the former doesn&amp;rsquo;t play well with multiple threads of execution.&lt;/p&gt;
9492 &lt;h3 id=&quot;debugging&quot;&gt;Debugging&lt;/h3&gt;
9493 &lt;p&gt;The truth is that neither tracing nor logging can be considered real debugging. To do actual debugging, you need a debugger tool, which allows you to do the following:&lt;/p&gt;
9494 &lt;ul&gt;
9495 &lt;li&gt;Step through the code interactively.&lt;/li&gt;
9496 &lt;li&gt;Set breakpoints, including conditional breakpoints.&lt;/li&gt;
9497 &lt;li&gt;Introspect variables in memory.&lt;/li&gt;
9498 &lt;li&gt;Evaluate custom expressions at runtime.&lt;/li&gt;
9499 &lt;/ul&gt;
9500 &lt;p&gt;A crude debugger that runs in the terminal, unsurprisingly named &lt;strong&gt;&lt;code&gt;pdb&lt;/code&gt;&lt;/strong&gt; for &amp;ldquo;The Python Debugger,&amp;rdquo; is distributed as part of the standard library. This makes it always available, so it may be your only choice for performing remote debugging. Perhaps that&amp;rsquo;s a good reason to get familiar with it.&lt;/p&gt;
9501 &lt;p&gt;However, it doesn&amp;rsquo;t come with a graphical interface, so &lt;a href=&quot;https://realpython.com/python-debugging-pdb/&quot;&gt;using &lt;code&gt;pdb&lt;/code&gt;&lt;/a&gt; may be a bit tricky. If you can&amp;rsquo;t edit the code, you have to run it as a module and pass your script&amp;rsquo;s location:&lt;/p&gt;
9502 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python -m pdb my_script.py
9503 &lt;/pre&gt;&lt;/div&gt;
9504 
9505 &lt;p&gt;Otherwise, you can set up a breakpoint directly in the code, which will pause the execution of your script and drop you into the debugger. The old way of doing this required two steps:&lt;/p&gt;
9506 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pdb&lt;/span&gt;
9507 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pdb&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9508 &lt;span class=&quot;go&quot;&gt;--Return--&lt;/span&gt;
9509 &lt;span class=&quot;go&quot;&gt;&amp;gt; &amp;lt;stdin&amp;gt;(1)&amp;lt;module&amp;gt;()-&amp;gt;None&lt;/span&gt;
9510 &lt;span class=&quot;go&quot;&gt;(Pdb)&lt;/span&gt;
9511 &lt;/pre&gt;&lt;/div&gt;
9512 
9513 &lt;p&gt;This shows up an interactive prompt, which might look intimidating at first. However, you can still type native Python at this point to examine or modify the state of local variables. Apart from that, there&amp;rsquo;s really only a handful of debugger-specific commands that you want to use for stepping through the code.&lt;/p&gt;
9514 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
9515 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; It&amp;rsquo;s customary to put the two instructions for spinning up a debugger on a single line. This requires the use of a semicolon, which is rarely found in Python programs:&lt;/p&gt;
9516 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pdb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdb&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9517 &lt;/pre&gt;&lt;/div&gt;
9518 
9519 &lt;p&gt;While certainly not Pythonic, it stands out as a reminder to remove it after you&amp;rsquo;re done with debugging.&lt;/p&gt;
9520 &lt;/div&gt;
9521 &lt;p&gt;Since Python 3.7, you can also call the built-in &lt;code&gt;breakpoint()&lt;/code&gt; function, which does the same thing, but in a more compact way and with some additional &lt;a href=&quot;https://realpython.com/python37-new-features/#the-breakpoint-built-in&quot;&gt;bells and whistles&lt;/a&gt;:&lt;/p&gt;
9522 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;average&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9523     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9524         &lt;span class=&quot;n&quot;&gt;breakpoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Python 3.7+&lt;/span&gt;
9525         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9526 &lt;/pre&gt;&lt;/div&gt;
9527 
9528 &lt;p&gt;You&amp;rsquo;re probably going to use a visual debugger integrated with a code editor for the most part. &lt;a href=&quot;https://www.jetbrains.com/pycharm/&quot;&gt;PyCharm&lt;/a&gt; has an excellent debugger, which boasts high performance, but you&amp;rsquo;ll find &lt;a href=&quot;https://realpython.com/python-ides-code-editors-guide/&quot;&gt;plenty of alternative IDEs&lt;/a&gt; with debuggers, both paid and free of charge.&lt;/p&gt;
9529 &lt;p&gt;Debugging isn&amp;rsquo;t the proverbial silver bullet. Sometimes logging or tracing will be a better solution. For example, defects that are hard to reproduce, such as &lt;a href=&quot;https://en.wikipedia.org/wiki/Race_condition&quot;&gt;race conditions&lt;/a&gt;, often result from temporal coupling. When you stop at a breakpoint, that little pause in program execution may mask the problem. It&amp;rsquo;s kind of like the &lt;a href=&quot;https://en.wikipedia.org/wiki/Uncertainty_principle&quot;&gt;Heisenberg principle&lt;/a&gt;: you can&amp;rsquo;t measure and observe a bug at the same time.&lt;/p&gt;
9530 &lt;p&gt;These methods aren&amp;rsquo;t mutually exclusive. They complement each other.&lt;/p&gt;
9531 &lt;h2 id=&quot;thread-safe-printing&quot;&gt;Thread-Safe Printing&lt;/h2&gt;
9532 &lt;p&gt;I briefly touched upon the thread safety issue before, recommending &lt;code&gt;logging&lt;/code&gt; over the &lt;code&gt;print()&lt;/code&gt; function. If you&amp;rsquo;re still reading this, then you must be comfortable with &lt;a href=&quot;https://realpython.com/intro-to-python-threading/&quot;&gt;the concept of threads&lt;/a&gt;.&lt;/p&gt;
9533 &lt;p&gt;Thread safety means that a piece of code can be safely shared between multiple threads of execution. The simplest strategy for ensuring thread-safety is by sharing &lt;strong&gt;immutable&lt;/strong&gt; objects only. If threads can&amp;rsquo;t modify an object&amp;rsquo;s state, then there&amp;rsquo;s no risk of breaking its consistency.&lt;/p&gt;
9534 &lt;p&gt;Another method takes advantage of &lt;strong&gt;local memory&lt;/strong&gt;, which makes each thread receive its own copy of the same object. That way, other threads can&amp;rsquo;t see the changes made to it in the current thread.&lt;/p&gt;
9535 &lt;p&gt;But that doesn&amp;rsquo;t solve the problem, does it? You often want your threads to cooperate by being able to mutate a shared resource. The most common way of synchronizing concurrent access to such a resource is by &lt;strong&gt;locking&lt;/strong&gt; it. This gives exclusive write access to one or sometimes a few threads at a time.&lt;/p&gt;
9536 &lt;p&gt;However, locking is expensive and reduces concurrent throughput, so other means for controlling access have been invented, such as &lt;strong&gt;atomic variables&lt;/strong&gt; or the &lt;strong&gt;compare-and-swap&lt;/strong&gt; algorithm.&lt;/p&gt;
9537 &lt;p&gt;Printing isn&amp;rsquo;t thread-safe in Python. The &lt;code&gt;print()&lt;/code&gt; function holds a reference to the standard output, which is a shared global variable. In theory, because there&amp;rsquo;s no locking, a context switch could happen during a call to &lt;code&gt;sys.stdout.write()&lt;/code&gt;, intertwining bits of text from multiple &lt;code&gt;print()&lt;/code&gt; calls.&lt;/p&gt;
9538 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
9539 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; A context switch means that one thread halts its execution, either voluntarily or not, so that another one can take over. This might happen at any moment, even in the middle of a function call.&lt;/p&gt;
9540 &lt;/div&gt;
9541 &lt;p&gt;In practice, however, that doesn&amp;rsquo;t happen. No matter how hard you try, writing to the standard output seems to be atomic. The only problem that you may sometimes observe is with messed up line breaks:&lt;/p&gt;
9542 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;[Thread-3 A][Thread-2 A][Thread-1 A]
9543 
9544 [Thread-3 B][Thread-1 B]
9545 
9546 
9547 [Thread-1 C][Thread-3 C]
9548 
9549 [Thread-2 B]
9550 [Thread-2 C]
9551 &lt;/pre&gt;&lt;/div&gt;
9552 
9553 &lt;p&gt;To simulate this, you can increase the likelihood of a context switch by making the underlying &lt;code&gt;.write()&lt;/code&gt; method go to sleep for a random amount of time. How? By mocking it, which you already know about from an earlier section:&lt;/p&gt;
9554 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
9555 
9556 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;
9557 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;
9558 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;threading&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current_thread&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Thread&lt;/span&gt;
9559 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;unittest.mock&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;patch&lt;/span&gt;
9560 
9561 &lt;span class=&quot;n&quot;&gt;write&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stdout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;
9562 
9563 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;slow_write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9564     &lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
9565     &lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9566 
9567 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
9568     &lt;span class=&quot;n&quot;&gt;thread_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current_thread&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
9569     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;letter&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;ABC&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9570         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;[&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{thread_name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{letter}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;]&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9571 
9572 &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;patch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;sys.stdout&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mock_stdout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9573     &lt;span class=&quot;n&quot;&gt;mock_stdout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;slow_write&lt;/span&gt;
9574     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9575         &lt;span class=&quot;n&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9576 &lt;/pre&gt;&lt;/div&gt;
9577 
9578 &lt;p&gt;First, you need to store the original &lt;code&gt;.write()&lt;/code&gt; method in a variable, which you&amp;rsquo;ll delegate to later. Then you provide your fake implementation, which will take up to one second to execute. Each thread will make a few &lt;code&gt;print()&lt;/code&gt; calls with its name and a letter: A, B, and C.&lt;/p&gt;
9579 &lt;p&gt;If you read the mocking section before, then you may already have an idea of why printing misbehaves like that. Nonetheless, to make it crystal clear, you can capture values fed into your &lt;code&gt;slow_write()&lt;/code&gt; function. You&amp;rsquo;ll notice that you get a slightly different sequence each time:&lt;/p&gt;
9580 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
9581     &lt;span class=&quot;s1&quot;&gt;&amp;#39;[Thread-3 A]&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
9582     &lt;span class=&quot;s1&quot;&gt;&amp;#39;[Thread-2 A]&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
9583     &lt;span class=&quot;s1&quot;&gt;&amp;#39;[Thread-1 A]&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
9584     &lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
9585     &lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
9586     &lt;span class=&quot;s1&quot;&gt;&amp;#39;[Thread-3 B]&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
9587     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9588 &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
9589 &lt;/pre&gt;&lt;/div&gt;
9590 
9591 &lt;p&gt;Even though &lt;code&gt;sys.stdout.write()&lt;/code&gt; itself is an atomic operation, a single call to the &lt;code&gt;print()&lt;/code&gt; function can yield more than one write. For example, line breaks are written separately from the rest of the text, and context switching takes place between those writes.&lt;/p&gt;
9592 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
9593 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The atomic nature of the standard output in Python is a byproduct of the &lt;a href=&quot;https://realpython.com/python-gil/&quot;&gt;Global Interpreter Lock&lt;/a&gt;, which applies locking around bytecode instructions. Be aware, however, that many interpreter flavors don&amp;rsquo;t have the GIL, where multi-threaded printing requires explicit locking.&lt;/p&gt;
9594 &lt;/div&gt;
9595 &lt;p&gt;You can make the newline character become an integral part of the message by handling it manually:&lt;/p&gt;
9596 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;[&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{thread_name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{letter}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9597 &lt;/pre&gt;&lt;/div&gt;
9598 
9599 &lt;p&gt;This will fix the output:&lt;/p&gt;
9600 &lt;div class=&quot;highlight text&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;[Thread-2 A]
9601 [Thread-1 A]
9602 [Thread-3 A]
9603 [Thread-1 B]
9604 [Thread-3 B]
9605 [Thread-2 B]
9606 [Thread-1 C]
9607 [Thread-2 C]
9608 [Thread-3 C]
9609 &lt;/pre&gt;&lt;/div&gt;
9610 
9611 &lt;p&gt;Notice, however, that the &lt;code&gt;print()&lt;/code&gt; function still keeps making a separate call for the empty suffix, which translates to useless &lt;code&gt;sys.stdout.write(&#39;&#39;)&lt;/code&gt; instruction:&lt;/p&gt;
9612 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
9613     &lt;span class=&quot;s1&quot;&gt;&amp;#39;[Thread-2 A]&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
9614     &lt;span class=&quot;s1&quot;&gt;&amp;#39;[Thread-1 A]&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
9615     &lt;span class=&quot;s1&quot;&gt;&amp;#39;[Thread-3 A]&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
9616     &lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
9617     &lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
9618     &lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
9619     &lt;span class=&quot;s1&quot;&gt;&amp;#39;[Thread-1 B]&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
9620     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9621 &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
9622 &lt;/pre&gt;&lt;/div&gt;
9623 
9624 &lt;p&gt;A truly thread-safe version of the &lt;code&gt;print()&lt;/code&gt; function could look like this:&lt;/p&gt;
9625 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;threading&lt;/span&gt;
9626 
9627 &lt;span class=&quot;n&quot;&gt;lock&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;threading&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Lock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9628 
9629 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;thread_safe_print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9630     &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9631         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9632 &lt;/pre&gt;&lt;/div&gt;
9633 
9634 &lt;p&gt;You can put that function in a module and import it elsewhere:&lt;/p&gt;
9635 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;thread_safe_print&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;thread_safe_print&lt;/span&gt;
9636 
9637 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
9638     &lt;span class=&quot;n&quot;&gt;thread_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current_thread&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
9639     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;letter&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;ABC&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9640         &lt;span class=&quot;n&quot;&gt;thread_safe_print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;[&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{thread_name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{letter}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;]&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9641 &lt;/pre&gt;&lt;/div&gt;
9642 
9643 &lt;p&gt;Now, despite making two writes per each &lt;code&gt;print()&lt;/code&gt; request, only one thread is allowed to interact with the stream, while the rest must wait:&lt;/p&gt;
9644 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
9645     &lt;span class=&quot;c1&quot;&gt;# Lock acquired by Thread-3 &lt;/span&gt;
9646     &lt;span class=&quot;s1&quot;&gt;&amp;#39;[Thread-3 A]&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
9647     &lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
9648     &lt;span class=&quot;c1&quot;&gt;# Lock released by Thread-3&lt;/span&gt;
9649     &lt;span class=&quot;c1&quot;&gt;# Lock acquired by Thread-1&lt;/span&gt;
9650     &lt;span class=&quot;s1&quot;&gt;&amp;#39;[Thread-1 B]&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
9651     &lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
9652     &lt;span class=&quot;c1&quot;&gt;# Lock released by Thread-1&lt;/span&gt;
9653     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9654 &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
9655 &lt;/pre&gt;&lt;/div&gt;
9656 
9657 &lt;p&gt;I added comments to indicate how the lock is limiting access to the shared resource.&lt;/p&gt;
9658 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
9659 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Even in single-threaded code, you might get caught up in a similar situation. Specifically, when you&amp;rsquo;re printing to the standard output and the standard error streams at the same time. Unless you redirect one or both of them to separate files, they&amp;rsquo;ll both share a single terminal window.&lt;/p&gt;
9660 &lt;/div&gt;
9661 &lt;p&gt;Conversely, the &lt;code&gt;logging&lt;/code&gt; module is thread-safe by design, which is reflected by its ability to display thread names in the formatted message:&lt;/p&gt;
9662 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;logging&lt;/span&gt;
9663 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;basicConfig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%(threadName)s&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%(message)s&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9664 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hello&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9665 &lt;span class=&quot;go&quot;&gt;MainThread hello&lt;/span&gt;
9666 &lt;/pre&gt;&lt;/div&gt;
9667 
9668 &lt;p&gt;It&amp;rsquo;s another reason why you might not want to use the &lt;code&gt;print()&lt;/code&gt; function all the time.&lt;/p&gt;
9669 &lt;h2 id=&quot;python-print-counterparts&quot;&gt;Python Print Counterparts&lt;/h2&gt;
9670 &lt;p&gt;By now, you know a lot of what there is to know about &lt;code&gt;print()&lt;/code&gt;! The subject, however, wouldn&amp;rsquo;t be complete without talking about its counterparts a little bit. While &lt;code&gt;print()&lt;/code&gt; is about the output, there are functions and libraries for the input.&lt;/p&gt;
9671 &lt;h3 id=&quot;built-in&quot;&gt;Built-In&lt;/h3&gt;
9672 &lt;p&gt;Python comes with a built-in function for accepting input from the user, predictably called &lt;code&gt;input()&lt;/code&gt;. It accepts data from the standard input stream, which is usually the keyboard:&lt;/p&gt;
9673 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Enter your name: &amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9674 &lt;span class=&quot;go&quot;&gt;Enter your name: jdoe&lt;/span&gt;
9675 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9676 &lt;span class=&quot;go&quot;&gt;jdoe&lt;/span&gt;
9677 &lt;/pre&gt;&lt;/div&gt;
9678 
9679 &lt;p&gt;The function always returns a string, so you might need to parse it accordingly:&lt;/p&gt;
9680 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9681     &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;How old are you? &amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
9682 &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9683     &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
9684 &lt;/pre&gt;&lt;/div&gt;
9685 
9686 &lt;p&gt;The prompt parameter is completely optional, so nothing will show if you skip it, but the function will still work:&lt;/p&gt;
9687 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9688 &lt;span class=&quot;go&quot;&gt;hello world&lt;/span&gt;
9689 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9690 &lt;span class=&quot;go&quot;&gt;hello world&lt;/span&gt;
9691 &lt;/pre&gt;&lt;/div&gt;
9692 
9693 &lt;p&gt;Nevertheless, throwing in a descriptive call to action makes the user experience so much better.&lt;/p&gt;
9694 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
9695 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; To read from the standard input in Python 2, you have to call &lt;code&gt;raw_input()&lt;/code&gt; instead, which is yet another built-in. Unfortunately, there&amp;rsquo;s also a misleadingly named &lt;code&gt;input()&lt;/code&gt; function, which does a slightly different thing.&lt;/p&gt;
9696 &lt;p&gt;In fact, it also takes the input from the standard stream, but then it tries to evaluate it as if it was Python code. Because that&amp;rsquo;s a potential &lt;strong&gt;security vulnerability&lt;/strong&gt;, this function was completely removed from Python 3, while &lt;code&gt;raw_input()&lt;/code&gt; got renamed to &lt;code&gt;input()&lt;/code&gt;.&lt;/p&gt;
9697 &lt;p&gt;Here&amp;rsquo;s a quick comparison of the available functions and what they do:&lt;/p&gt;
9698 &lt;div class=&quot;table-responsive&quot;&gt;
9699 &lt;table class=&quot;table table-hover&quot;&gt;
9700 &lt;thead&gt;
9701 &lt;tr&gt;
9702 &lt;th&gt;Python 2&lt;/th&gt;
9703 &lt;th&gt;Python 3&lt;/th&gt;
9704 &lt;/tr&gt;
9705 &lt;/thead&gt;
9706 &lt;tbody&gt;
9707 &lt;tr&gt;
9708 &lt;td&gt;&lt;code&gt;raw_input()&lt;/code&gt;&lt;/td&gt;
9709 &lt;td&gt;&lt;code&gt;input()&lt;/code&gt;&lt;/td&gt;
9710 &lt;/tr&gt;
9711 &lt;tr&gt;
9712 &lt;td&gt;&lt;code&gt;input()&lt;/code&gt;&lt;/td&gt;
9713 &lt;td&gt;&lt;code&gt;eval(input())&lt;/code&gt;&lt;/td&gt;
9714 &lt;/tr&gt;
9715 &lt;/tbody&gt;
9716 &lt;/table&gt;
9717 &lt;/div&gt;
9718 &lt;p&gt;As you can tell, it&amp;rsquo;s still possible to simulate the old behavior in Python 3.&lt;/p&gt;
9719 &lt;/div&gt;
9720 &lt;p&gt;Asking the user for a password with &lt;code&gt;input()&lt;/code&gt; is a bad idea because it&amp;rsquo;ll show up in plaintext as they&amp;rsquo;re typing it. In this case, you should be using the &lt;code&gt;getpass()&lt;/code&gt; function instead, which masks typed characters. This function is defined in a module under the same name, which is also available in the standard library:&lt;/p&gt;
9721 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;getpass&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getpass&lt;/span&gt;
9722 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getpass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9723 &lt;span class=&quot;go&quot;&gt;Password: &lt;/span&gt;
9724 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9725 &lt;span class=&quot;go&quot;&gt;s3cret&lt;/span&gt;
9726 &lt;/pre&gt;&lt;/div&gt;
9727 
9728 &lt;p&gt;The &lt;code&gt;getpass&lt;/code&gt; module has another function for getting the user&amp;rsquo;s name from an environment variable:&lt;/p&gt;
9729 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;getpass&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getuser&lt;/span&gt;
9730 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getuser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9731 &lt;span class=&quot;go&quot;&gt;&amp;#39;jdoe&amp;#39;&lt;/span&gt;
9732 &lt;/pre&gt;&lt;/div&gt;
9733 
9734 &lt;p&gt;Python&amp;rsquo;s built-in functions for handling the standard input are quite limited. At the same time, there are plenty of third-party packages, which offer much more sophisticated tools.&lt;/p&gt;
9735 &lt;h3 id=&quot;third-party&quot;&gt;Third-Party&lt;/h3&gt;
9736 &lt;p&gt;There are external Python packages out there that allow for building complex graphical interfaces specifically to collect data from the user. Some of their features include:&lt;/p&gt;
9737 &lt;ul&gt;
9738 &lt;li&gt;Advanced formatting and styling&lt;/li&gt;
9739 &lt;li&gt;Automated parsing, validation, and sanitization of user data&lt;/li&gt;
9740 &lt;li&gt;A declarative style of defining layouts&lt;/li&gt;
9741 &lt;li&gt;Interactive autocompletion&lt;/li&gt;
9742 &lt;li&gt;Mouse support&lt;/li&gt;
9743 &lt;li&gt;Predefined widgets such as checklists or menus&lt;/li&gt;
9744 &lt;li&gt;Searchable history of typed commands&lt;/li&gt;
9745 &lt;li&gt;Syntax highlighting&lt;/li&gt;
9746 &lt;/ul&gt;
9747 &lt;p&gt;Demonstrating such tools is outside of the scope of this article, but you may want to try them out. I personally got to know about some of those through the &lt;a href=&quot;https://pythonbytes.fm/&quot;&gt;Python Bytes Podcast&lt;/a&gt;. Here they are:&lt;/p&gt;
9748 &lt;ul&gt;
9749 &lt;li&gt;&lt;a href=&quot;https://github.com/Mckinsey666/bullet&quot;&gt;&lt;code&gt;bullet&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
9750 &lt;li&gt;&lt;a href=&quot;https://pypi.org/project/cooked-input/&quot;&gt;&lt;code&gt;cooked-input&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
9751 &lt;li&gt;&lt;a href=&quot;https://pypi.org/project/prompt_toolkit/&quot;&gt;&lt;code&gt;prompt_toolkit&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
9752 &lt;li&gt;&lt;a href=&quot;https://github.com/kylebebak/questionnaire&quot;&gt;&lt;code&gt;questionnaire&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
9753 &lt;/ul&gt;
9754 &lt;p&gt;Nonetheless, it&amp;rsquo;s worth mentioning a command line tool called &lt;code&gt;rlwrap&lt;/code&gt; that adds powerful line editing capabilities to your Python scripts for free. You don&amp;rsquo;t have to do anything for it to work!&lt;/p&gt;
9755 &lt;p&gt;Let&amp;rsquo;s assume you wrote a command-line interface that understands three instructions, including one for adding numbers:&lt;/p&gt;
9756 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Type &amp;quot;help&amp;quot;, &amp;quot;exit&amp;quot;, &amp;quot;add a [b [c ...]]&amp;quot;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9757 &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9758     &lt;span class=&quot;n&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;~ &amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39; &amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9759     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9760         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lower&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;exit&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9761             &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
9762         &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lower&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;help&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9763             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;This is help.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9764         &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lower&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;add&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9765             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
9766         &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9767             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Unknown command&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9768 &lt;/pre&gt;&lt;/div&gt;
9769 
9770 &lt;p&gt;At first glance, it seems like a typical prompt when you run it:&lt;/p&gt;
9771 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python calculator.py
9772 &lt;span class=&quot;go&quot;&gt;Type &amp;quot;help&amp;quot;, &amp;quot;exit&amp;quot;, &amp;quot;add a [b [c ...]]&amp;quot;&lt;/span&gt;
9773 &lt;span class=&quot;go&quot;&gt;~ add 1 2 3 4&lt;/span&gt;
9774 &lt;span class=&quot;go&quot;&gt;10&lt;/span&gt;
9775 &lt;span class=&quot;go&quot;&gt;~ aad 2 3&lt;/span&gt;
9776 &lt;span class=&quot;go&quot;&gt;Unknown command&lt;/span&gt;
9777 &lt;span class=&quot;go&quot;&gt;~ exit&lt;/span&gt;
9778 &lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt;
9779 &lt;/pre&gt;&lt;/div&gt;
9780 
9781 &lt;p&gt;But as soon as you make a mistake and want to fix it, you&amp;rsquo;ll see that none of the function keys work as expected. Hitting the &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-arrow-left&quot;&gt;Left&lt;/kbd&gt;&lt;/span&gt; arrow, for example, results in this instead of moving the cursor back:&lt;/p&gt;
9782 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python calculator.py
9783 &lt;span class=&quot;go&quot;&gt;Type &amp;quot;help&amp;quot;, &amp;quot;exit&amp;quot;, &amp;quot;add a [b [c ...]]&amp;quot;&lt;/span&gt;
9784 &lt;span class=&quot;go&quot;&gt;~ aad^[[D&lt;/span&gt;
9785 &lt;/pre&gt;&lt;/div&gt;
9786 
9787 &lt;p&gt;Now, you can wrap the same script with the &lt;code&gt;rlwrap&lt;/code&gt; command. Not only will you get the arrow keys working, but you&amp;rsquo;ll also be able to search through the persistent history of your custom commands, use autocompletion, and edit the line with shortcuts:&lt;/p&gt;
9788 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; rlwrap python calculator.py
9789 &lt;span class=&quot;go&quot;&gt;Type &amp;quot;help&amp;quot;, &amp;quot;exit&amp;quot;, &amp;quot;add a [b [c ...]]&amp;quot;&lt;/span&gt;
9790 &lt;span class=&quot;go&quot;&gt;(reverse-i-search)`a&amp;#39;: add 1 2 3 4&lt;/span&gt;
9791 &lt;/pre&gt;&lt;/div&gt;
9792 
9793 &lt;p&gt;Isn&amp;rsquo;t that great?&lt;/p&gt;
9794 &lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
9795 &lt;p&gt;You&amp;rsquo;re now armed with a body of knowledge about the &lt;code&gt;print()&lt;/code&gt; function in Python, as well as many surrounding topics. You have a deep understanding of what it is and how it works, involving all of its key elements. Numerous examples gave you insight into its evolution from Python 2.&lt;/p&gt;
9796 &lt;p&gt;Apart from that, you learned how to:&lt;/p&gt;
9797 &lt;ul&gt;
9798 &lt;li&gt;Avoid common mistakes with &lt;code&gt;print()&lt;/code&gt; in Python&lt;/li&gt;
9799 &lt;li&gt;Deal with newlines, character encodings and buffering&lt;/li&gt;
9800 &lt;li&gt;Write text to files&lt;/li&gt;
9801 &lt;li&gt;Mock the &lt;code&gt;print()&lt;/code&gt; function in unit tests&lt;/li&gt;
9802 &lt;li&gt;Build advanced user interfaces in the terminal&lt;/li&gt;
9803 &lt;/ul&gt;
9804 &lt;p&gt;Now that you know all this, you can make interactive programs that communicate with users or produce data in popular file formats. You&amp;rsquo;re able to quickly diagnose problems in your code and protect yourself from them. Last but not least, you know how to implement the classic snake game.&lt;/p&gt;
9805 &lt;p&gt;If you&amp;rsquo;re still thirsty for more information, have questions, or simply would like to share your thoughts, then feel free to reach out in the comments section below.&lt;/p&gt;
9806         &lt;hr /&gt;
9807         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
9808       </content>
9809     </entry>
9810   
9811     <entry>
9812       <title>Inheritance and Composition: A Python OOP Guide</title>
9813       <id>https://realpython.com/inheritance-composition-python/</id>
9814       <link href="https://realpython.com/inheritance-composition-python/"/>
9815       <updated>2019-08-07T14:00:00+00:00</updated>
9816       <summary>In this step-by-step tutorial, you&#39;ll learn about inheritance and composition in Python. You&#39;ll improve your object-oriented programming (OOP) skills by understanding how to use inheritance and composition and how to leverage them in their design.</summary>
9817       <content type="html">
9818         &lt;p&gt;In this article, you&amp;rsquo;ll explore &lt;strong&gt;inheritance&lt;/strong&gt; and &lt;strong&gt;composition&lt;/strong&gt; in Python. &lt;a href=&quot;https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)&quot;&gt;Inheritance&lt;/a&gt; and &lt;a href=&quot;https://en.wikipedia.org/wiki/Object_composition&quot;&gt;composition&lt;/a&gt; are two important concepts in object oriented programming that model the relationship between two classes. They are the building blocks of &lt;a href=&quot;https://realpython.com/python3-object-oriented-programming/&quot;&gt;object oriented design&lt;/a&gt;, and they help programmers to write reusable code.&lt;/p&gt;
9819 &lt;p&gt;&lt;strong&gt;By the end of this article, you&amp;rsquo;ll know how to&lt;/strong&gt;:&lt;/p&gt;
9820 &lt;ul&gt;
9821 &lt;li&gt;Use inheritance in Python&lt;/li&gt;
9822 &lt;li&gt;Model class hierarchies using inheritance&lt;/li&gt;
9823 &lt;li&gt;Use multiple inheritance in Python and understand its drawbacks&lt;/li&gt;
9824 &lt;li&gt;Use composition to create complex objects&lt;/li&gt;
9825 &lt;li&gt;Reuse existing code by applying composition&lt;/li&gt;
9826 &lt;li&gt;Change application behavior at run-time through composition&lt;/li&gt;
9827 &lt;/ul&gt;
9828 &lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Free Bonus:&lt;/strong&gt; &lt;a href=&quot;#&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-oop&quot; data-focus=&quot;false&quot;&gt;Click here to get access to a free Python OOP Cheat Sheet&lt;/a&gt; that points you to the best tutorials, videos, and books to learn more about Object-Oriented Programming with Python.&lt;/p&gt;&lt;/div&gt;
9829 
9830 &lt;h2 id=&quot;what-are-inheritance-and-composition&quot;&gt;What Are Inheritance and Composition?&lt;/h2&gt;
9831 &lt;p&gt;&lt;strong&gt;Inheritance&lt;/strong&gt; and &lt;strong&gt;composition&lt;/strong&gt; are two major concepts in object oriented programming that model the relationship between two classes. They drive the design of an application and determine how the application should evolve as new features are added or requirements change.&lt;/p&gt;
9832 &lt;p&gt;Both of them enable code reuse, but they do it in different ways.&lt;/p&gt;
9833 &lt;h3 id=&quot;whats-inheritance&quot;&gt;What&amp;rsquo;s Inheritance?&lt;/h3&gt;
9834 &lt;p&gt;&lt;strong&gt;Inheritance&lt;/strong&gt; models what is called an &lt;strong&gt;is a&lt;/strong&gt; relationship. This means that when you have a &lt;code&gt;Derived&lt;/code&gt; class that inherits from a &lt;code&gt;Base&lt;/code&gt; class, you created a relationship where &lt;code&gt;Derived&lt;/code&gt; &lt;strong&gt;is a&lt;/strong&gt; specialized version of &lt;code&gt;Base&lt;/code&gt;.&lt;/p&gt;
9835 &lt;p&gt;Inheritance is represented using the &lt;a href=&quot;https://www.uml.org/&quot;&gt;Unified Modeling Language&lt;/a&gt; or UML in the following way:&lt;/p&gt;
9836 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/ic-basic-inheritance.f8dc9ffee4d7.jpg&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-33&quot; src=&quot;https://files.realpython.com/media/ic-basic-inheritance.f8dc9ffee4d7.jpg&quot; width=&quot;242&quot; height=&quot;343&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/ic-basic-inheritance.f8dc9ffee4d7.jpg&amp;amp;w=60&amp;amp;sig=5b8ced824343b1ab983944639b080f1fb53c0ba4 60w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/ic-basic-inheritance.f8dc9ffee4d7.jpg&amp;amp;w=121&amp;amp;sig=e32f6166c127193b0856fcb61f9d2677ff7aa293 121w, https://files.realpython.com/media/ic-basic-inheritance.f8dc9ffee4d7.jpg 242w&quot; sizes=&quot;75vw&quot; alt=&quot;Basic inheritance between Base and Derived classes&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
9837 &lt;p&gt;Classes are represented as boxes with the class name on top. The inheritance relationship is represented by an arrow from the derived class pointing to the base class. The word &lt;strong&gt;extends&lt;/strong&gt; is usually added to the arrow.&lt;/p&gt;
9838 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
9839 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In an inheritance relationship:&lt;/p&gt;
9840 &lt;ul&gt;
9841 &lt;li&gt;Classes that inherit from another are called derived classes, subclasses, or subtypes. &lt;/li&gt;
9842 &lt;li&gt;Classes from which other classes are derived are called base classes or super classes. &lt;/li&gt;
9843 &lt;li&gt;A derived class is said to derive, inherit, or extend a base class.&lt;/li&gt;
9844 &lt;/ul&gt;
9845 &lt;/div&gt;
9846 &lt;p&gt;Let&amp;rsquo;s say you have a base class &lt;code&gt;Animal&lt;/code&gt; and you derive from it to create a &lt;code&gt;Horse&lt;/code&gt; class. The inheritance relationship states that a &lt;code&gt;Horse&lt;/code&gt; &lt;strong&gt;is an&lt;/strong&gt; &lt;code&gt;Animal&lt;/code&gt;. This means that &lt;code&gt;Horse&lt;/code&gt; inherits the interface and implementation of &lt;code&gt;Animal&lt;/code&gt;, and &lt;code&gt;Horse&lt;/code&gt; objects can be used to replace &lt;code&gt;Animal&lt;/code&gt; objects in the application.&lt;/p&gt;
9847 &lt;p&gt;This is known as the &lt;a href=&quot;https://en.wikipedia.org/wiki/Liskov_substitution_principle&quot;&gt;Liskov substitution principle&lt;/a&gt;. The principle states that &amp;ldquo;in a computer program, if &lt;code&gt;S&lt;/code&gt; is a subtype of &lt;code&gt;T&lt;/code&gt;, then objects of type &lt;code&gt;T&lt;/code&gt; may be replaced with objects of type &lt;code&gt;S&lt;/code&gt; without altering any of the desired properties of the program&amp;rdquo;.&lt;/p&gt;
9848 &lt;p&gt;You&amp;rsquo;ll see in this article why you should always follow the Liskov substitution principle when creating your class hierarchies, and the problems you&amp;rsquo;ll run into if you don&amp;rsquo;t.&lt;/p&gt;
9849 &lt;h3 id=&quot;whats-composition&quot;&gt;What&amp;rsquo;s Composition?&lt;/h3&gt;
9850 &lt;p&gt;&lt;strong&gt;Composition&lt;/strong&gt; is a concept that models a &lt;strong&gt;has a&lt;/strong&gt; relationship. It enables creating complex types by combining objects of other types. This means that a class &lt;code&gt;Composite&lt;/code&gt; can contain an object of another class &lt;code&gt;Component&lt;/code&gt;. This relationship means that a &lt;code&gt;Composite&lt;/code&gt; &lt;strong&gt;has a&lt;/strong&gt; &lt;code&gt;Component&lt;/code&gt;.&lt;/p&gt;
9851 &lt;p&gt;UML represents composition as follows:&lt;/p&gt;
9852 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/ic-basic-composition.8a15876f7db2.jpg&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-33&quot; src=&quot;https://files.realpython.com/media/ic-basic-composition.8a15876f7db2.jpg&quot; width=&quot;249&quot; height=&quot;348&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/ic-basic-composition.8a15876f7db2.jpg&amp;amp;w=62&amp;amp;sig=fe9cee54da335ac7d82f147d947f595d9063864f 62w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/ic-basic-composition.8a15876f7db2.jpg&amp;amp;w=124&amp;amp;sig=28fd4da7064494aa88f126b3b073aaab8a500b61 124w, https://files.realpython.com/media/ic-basic-composition.8a15876f7db2.jpg 249w&quot; sizes=&quot;75vw&quot; alt=&quot;Basic composition between Composite and Component classes&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
9853 &lt;p&gt;Composition is represented through a line with a diamond at the composite class pointing to the component class. The composite side can express the cardinality of the relationship. The cardinality indicates the number or valid range of &lt;code&gt;Component&lt;/code&gt; instances the &lt;code&gt;Composite&lt;/code&gt; class will contain.&lt;/p&gt;
9854 &lt;p&gt;In the diagram above, the &lt;code&gt;1&lt;/code&gt; represents that the &lt;code&gt;Composite&lt;/code&gt; class contains one object of type &lt;code&gt;Component&lt;/code&gt;. Cardinality can be expressed in the following ways:&lt;/p&gt;
9855 &lt;ul&gt;
9856 &lt;li&gt;&lt;strong&gt;A number&lt;/strong&gt; indicates the number of &lt;code&gt;Component&lt;/code&gt; instances that are contained in the &lt;code&gt;Composite&lt;/code&gt;.&lt;/li&gt;
9857 &lt;li&gt;&lt;strong&gt;The * symbol&lt;/strong&gt; indicates that the &lt;code&gt;Composite&lt;/code&gt; class can contain a variable number of &lt;code&gt;Component&lt;/code&gt; instances.&lt;/li&gt;
9858 &lt;li&gt;&lt;strong&gt;A range 1..4&lt;/strong&gt; indicates that the &lt;code&gt;Composite&lt;/code&gt; class can contain a range of &lt;code&gt;Component&lt;/code&gt; instances. The range is indicated with the minimum and maximum number of instances, or minimum and many instances like in &lt;strong&gt;1..*&lt;/strong&gt;.&lt;/li&gt;
9859 &lt;/ul&gt;
9860 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
9861 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Classes that contain objects of other classes are usually referred to as composites, where classes that are used to create more complex types are referred to as components.&lt;/p&gt;
9862 &lt;/div&gt;
9863 &lt;p&gt;For example, your &lt;code&gt;Horse&lt;/code&gt; class can be composed by another object of type &lt;code&gt;Tail&lt;/code&gt;. Composition allows you to express that relationship by saying a &lt;code&gt;Horse&lt;/code&gt; &lt;strong&gt;has a&lt;/strong&gt; &lt;code&gt;Tail&lt;/code&gt;.&lt;/p&gt;
9864 &lt;p&gt;Composition enables you to reuse code by adding objects to other objects, as opposed to inheriting the interface and implementation of other classes. Both &lt;code&gt;Horse&lt;/code&gt; and &lt;code&gt;Dog&lt;/code&gt; classes can leverage the functionality of &lt;code&gt;Tail&lt;/code&gt; through composition without deriving one class from the other.&lt;/p&gt;
9865 &lt;h2 id=&quot;an-overview-of-inheritance-in-python&quot;&gt;An Overview of Inheritance in Python&lt;/h2&gt;
9866 &lt;p&gt;Everything in Python is an object. Modules are objects, class definitions and functions are objects, and of course, objects created from classes are objects too.&lt;/p&gt;
9867 &lt;p&gt;Inheritance is a required feature of every object oriented programming language. This means that Python supports inheritance, and as you&amp;rsquo;ll see later, it&amp;rsquo;s one of the few languages that supports multiple inheritance.&lt;/p&gt;
9868 &lt;p&gt;When you write Python code using classes, you are using inheritance even if you don&amp;rsquo;t know you&amp;rsquo;re using it. Let&amp;rsquo;s take a look at what that means.&lt;/p&gt;
9869 &lt;h3 id=&quot;the-object-super-class&quot;&gt;The Object Super Class&lt;/h3&gt;
9870 &lt;p&gt;The easiest way to see inheritance in Python is to jump into the &lt;a href=&quot;https://realpython.com/interacting-with-python/#using-the-python-interpreter-interactively&quot;&gt;Python interactive shell&lt;/a&gt; and write a little bit of code. You&amp;rsquo;ll start by writing the simplest class possible:&lt;/p&gt;
9871 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyClass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9872 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
9873 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
9874 &lt;/pre&gt;&lt;/div&gt;
9875 
9876 &lt;p&gt;You declared a class &lt;code&gt;MyClass&lt;/code&gt; that doesn&amp;rsquo;t do much, but it will illustrate the most basic inheritance concepts. Now that you have the class declared, you can use the &lt;code&gt;dir()&lt;/code&gt; function to list its members:&lt;/p&gt;
9877 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyClass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9878 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9879 &lt;span class=&quot;go&quot;&gt;[&amp;#39;__class__&amp;#39;, &amp;#39;__delattr__&amp;#39;, &amp;#39;__dict__&amp;#39;, &amp;#39;__dir__&amp;#39;, &amp;#39;__doc__&amp;#39;, &amp;#39;__eq__&amp;#39;,&lt;/span&gt;
9880 &lt;span class=&quot;go&quot;&gt;&amp;#39;__format__&amp;#39;, &amp;#39;__ge__&amp;#39;, &amp;#39;__getattribute__&amp;#39;, &amp;#39;__gt__&amp;#39;, &amp;#39;__hash__&amp;#39;, &amp;#39;__init__&amp;#39;,&lt;/span&gt;
9881 &lt;span class=&quot;go&quot;&gt;&amp;#39;__init_subclass__&amp;#39;, &amp;#39;__le__&amp;#39;, &amp;#39;__lt__&amp;#39;, &amp;#39;__module__&amp;#39;, &amp;#39;__ne__&amp;#39;, &amp;#39;__new__&amp;#39;,&lt;/span&gt;
9882 &lt;span class=&quot;go&quot;&gt;&amp;#39;__reduce__&amp;#39;, &amp;#39;__reduce_ex__&amp;#39;, &amp;#39;__repr__&amp;#39;, &amp;#39;__setattr__&amp;#39;, &amp;#39;__sizeof__&amp;#39;,&lt;/span&gt;
9883 &lt;span class=&quot;go&quot;&gt;&amp;#39;__str__&amp;#39;, &amp;#39;__subclasshook__&amp;#39;, &amp;#39;__weakref__&amp;#39;]&lt;/span&gt;
9884 &lt;/pre&gt;&lt;/div&gt;
9885 
9886 &lt;p&gt;&lt;a href=&quot;https://docs.python.org/3/library/functions.html#dir&quot;&gt;&lt;code&gt;dir()&lt;/code&gt;&lt;/a&gt; returns a list of all the members in the specified object. You have not declared any members in &lt;code&gt;MyClass&lt;/code&gt;, so where is the list coming from? You can find out using the interactive interpreter:&lt;/p&gt;
9887 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9888 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9889 &lt;span class=&quot;go&quot;&gt;[&amp;#39;__class__&amp;#39;, &amp;#39;__delattr__&amp;#39;, &amp;#39;__dir__&amp;#39;, &amp;#39;__doc__&amp;#39;, &amp;#39;__eq__&amp;#39;, &amp;#39;__format__&amp;#39;,&lt;/span&gt;
9890 &lt;span class=&quot;go&quot;&gt;&amp;#39;__ge__&amp;#39;, &amp;#39;__getattribute__&amp;#39;, &amp;#39;__gt__&amp;#39;, &amp;#39;__hash__&amp;#39;, &amp;#39;__init__&amp;#39;,&lt;/span&gt;
9891 &lt;span class=&quot;go&quot;&gt;&amp;#39;__init_subclass__&amp;#39;, &amp;#39;__le__&amp;#39;, &amp;#39;__lt__&amp;#39;, &amp;#39;__ne__&amp;#39;, &amp;#39;__new__&amp;#39;, &amp;#39;__reduce__&amp;#39;,&lt;/span&gt;
9892 &lt;span class=&quot;go&quot;&gt;&amp;#39;__reduce_ex__&amp;#39;, &amp;#39;__repr__&amp;#39;, &amp;#39;__setattr__&amp;#39;, &amp;#39;__sizeof__&amp;#39;, &amp;#39;__str__&amp;#39;,&lt;/span&gt;
9893 &lt;span class=&quot;go&quot;&gt;&amp;#39;__subclasshook__&amp;#39;]&lt;/span&gt;
9894 &lt;/pre&gt;&lt;/div&gt;
9895 
9896 &lt;p&gt;As you can see, the two lists are nearly identical. There are some additional members in &lt;code&gt;MyClass&lt;/code&gt; like &lt;code&gt;__dict__&lt;/code&gt; and &lt;code&gt;__weakref__&lt;/code&gt;, but every single member of the &lt;code&gt;object&lt;/code&gt; class is also present in &lt;code&gt;MyClass&lt;/code&gt;.&lt;/p&gt;
9897 &lt;p&gt;This is because every class you create in Python implicitly derives from &lt;code&gt;object&lt;/code&gt;. You could be more explicit and write &lt;code&gt;class MyClass(object):&lt;/code&gt;, but it&amp;rsquo;s redundant and unnecessary.&lt;/p&gt;
9898 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
9899 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In Python 2, you have to explicitly derive from &lt;code&gt;object&lt;/code&gt; for reasons beyond the scope of this article, but you can read about it in the &lt;a href=&quot;https://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes&quot;&gt;New-style and classic classes&lt;/a&gt; section of the Python 2 documentation.&lt;/p&gt;
9900 &lt;/div&gt;
9901 &lt;h3 id=&quot;exceptions-are-an-exception&quot;&gt;Exceptions Are an Exception&lt;/h3&gt;
9902 &lt;p&gt;Every class that you create in Python will implicitly derive from &lt;code&gt;object&lt;/code&gt;. The exception to this rule are classes used to indicate errors by raising an &lt;a href=&quot;https://realpython.com/courses/python-exceptions-101/&quot;&gt;exception&lt;/a&gt;.&lt;/p&gt;
9903 &lt;p&gt;You can see the problem using the Python interactive interpreter:&lt;/p&gt;
9904 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9905 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
9906 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
9907 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9908 
9909 &lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
9910   File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
9911 &lt;span class=&quot;gr&quot;&gt;TypeError&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;exceptions must derive from BaseException&lt;/span&gt;
9912 &lt;/pre&gt;&lt;/div&gt;
9913 
9914 &lt;p&gt;You created a new class to indicate a type of error. Then you tried to use it to raise an exception. An exception is raised but the output states that the exception is of type &lt;code&gt;TypeError&lt;/code&gt; not &lt;code&gt;MyError&lt;/code&gt; and that all &lt;code&gt;exceptions must derive from BaseException&lt;/code&gt;.&lt;/p&gt;
9915 &lt;p&gt;&lt;code&gt;BaseException&lt;/code&gt; is a base class provided for all error types. To create a new error type, you must derive your class from &lt;code&gt;BaseException&lt;/code&gt; or one of its derived classes. The convention in Python is to derive your custom error types from &lt;code&gt;Exception&lt;/code&gt;, which in turn derives from &lt;code&gt;BaseException&lt;/code&gt;.&lt;/p&gt;
9916 &lt;p&gt;The correct way to define your error type is the following:&lt;/p&gt;
9917 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ne&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9918 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
9919 &lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
9920 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9921 
9922 &lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
9923   File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
9924 &lt;span class=&quot;err&quot;&gt;__main__.MyError&lt;/span&gt;
9925 &lt;/pre&gt;&lt;/div&gt;
9926 
9927 &lt;p&gt;As you can see, when you raise &lt;code&gt;MyError&lt;/code&gt;, the output correctly states the type of error raised.&lt;/p&gt;
9928 &lt;h3 id=&quot;creating-class-hierarchies&quot;&gt;Creating Class Hierarchies&lt;/h3&gt;
9929 &lt;p&gt;Inheritance is the mechanism you&amp;rsquo;ll use to create hierarchies of related classes. These related classes will share a common interface that will be defined in the base classes. Derived classes can specialize the interface by providing a particular implementation where applies.&lt;/p&gt;
9930 &lt;p&gt;In this section, you&amp;rsquo;ll start modeling an HR system. The example will demonstrate the use of inheritance and how derived classes can provide a concrete implementation of the base class interface.&lt;/p&gt;
9931 &lt;p&gt;The HR system needs to process payroll for the company&amp;rsquo;s employees, but there are different types of employees depending on how their payroll is calculated.&lt;/p&gt;
9932 &lt;p&gt;You start by implementing a &lt;code&gt;PayrollSystem&lt;/code&gt; class that processes payroll:&lt;/p&gt;
9933 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In hr.py&lt;/span&gt;
9934 
9935 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PayrollSystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9936     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9937         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Calculating Payroll&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9938         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;===================&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9939         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9940             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Payroll for: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{employee.id}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{employee.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9941             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;- Check amount: {employee.calculate_payroll()}&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9942             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9943 &lt;/pre&gt;&lt;/div&gt;
9944 
9945 &lt;p&gt;The &lt;code&gt;PayrollSystem&lt;/code&gt; implements a &lt;code&gt;.calculate_payroll()&lt;/code&gt; method that takes a collection of employees and prints their &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, and check amount using the &lt;code&gt;.calculate_payroll()&lt;/code&gt; method exposed on each employee object.&lt;/p&gt;
9946 &lt;p&gt;Now, you implement a base class &lt;code&gt;Employee&lt;/code&gt; that handles the common interface for every employee type:&lt;/p&gt;
9947 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In hr.py&lt;/span&gt;
9948 
9949 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
9950     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9951         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;
9952         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
9953 &lt;/pre&gt;&lt;/div&gt;
9954 
9955 &lt;p&gt;&lt;code&gt;Employee&lt;/code&gt; is the base class for all employee types. It is constructed with an &lt;code&gt;id&lt;/code&gt; and a &lt;code&gt;name&lt;/code&gt;. What you are saying is that every &lt;code&gt;Employee&lt;/code&gt; must have an &lt;code&gt;id&lt;/code&gt; assigned as well as a name.&lt;/p&gt;
9956 &lt;p&gt;The HR system requires that every &lt;code&gt;Employee&lt;/code&gt; processed must provide a &lt;code&gt;.calculate_payroll()&lt;/code&gt; interface that returns the weekly salary for the employee. The implementation of that interface differs depending on the type of &lt;code&gt;Employee&lt;/code&gt;.&lt;/p&gt;
9957 &lt;p&gt;For example, administrative workers have a fixed salary, so every week they get paid the same amount:&lt;/p&gt;
9958 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In hr.py&lt;/span&gt;
9959 
9960 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SalaryEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9961     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9962         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9963         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;
9964 
9965     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9966         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;
9967 &lt;/pre&gt;&lt;/div&gt;
9968 
9969 &lt;p&gt;You create a derived class &lt;code&gt;SalaryEmployee&lt;/code&gt; that inherits &lt;code&gt;Employee&lt;/code&gt;. The class is initialized with the &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;name&lt;/code&gt; required by the base class, and you use &lt;code&gt;super()&lt;/code&gt; to initialize the members of the base class. You can read all about &lt;code&gt;super()&lt;/code&gt; in &lt;a href=&quot;https://realpython.com/python-super/&quot;&gt;Supercharge Your Classes With Python super()&lt;/a&gt;.&lt;/p&gt;
9970 &lt;p&gt;&lt;code&gt;SalaryEmployee&lt;/code&gt; also requires a &lt;code&gt;weekly_salary&lt;/code&gt; initialization parameter that represents the amount the employee makes per week.&lt;/p&gt;
9971 &lt;p&gt;The class provides the required &lt;code&gt;.calculate_payroll()&lt;/code&gt; method used by the HR system. The implementation just returns the amount stored in &lt;code&gt;weekly_salary&lt;/code&gt;.&lt;/p&gt;
9972 &lt;p&gt;The company also employs manufacturing workers that are paid by the hour, so you add an &lt;code&gt;HourlyEmployee&lt;/code&gt; to the HR system:&lt;/p&gt;
9973 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In hr.py&lt;/span&gt;
9974 
9975 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HourlyEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9976     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9977         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9978         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt;
9979         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;
9980 
9981     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9982         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;
9983 &lt;/pre&gt;&lt;/div&gt;
9984 
9985 &lt;p&gt;The &lt;code&gt;HourlyEmployee&lt;/code&gt; class is initialized with &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;name&lt;/code&gt;, like the base class, plus the &lt;code&gt;hours_worked&lt;/code&gt; and the &lt;code&gt;hour_rate&lt;/code&gt; required to calculate the payroll. The &lt;code&gt;.calculate_payroll()&lt;/code&gt; method is implemented by returning the hours worked times the hour rate.&lt;/p&gt;
9986 &lt;p&gt;Finally, the company employs sales associates that are paid through a fixed salary plus a commission based on their sales, so you create a &lt;code&gt;CommissionEmployee&lt;/code&gt; class:&lt;/p&gt;
9987 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In hr.py&lt;/span&gt;
9988 
9989 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CommissionEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SalaryEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9990     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commission&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9991         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
9992         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;commission&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commission&lt;/span&gt;
9993 
9994     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
9995         &lt;span class=&quot;n&quot;&gt;fixed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
9996         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fixed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;commission&lt;/span&gt;
9997 &lt;/pre&gt;&lt;/div&gt;
9998 
9999 &lt;p&gt;You derive &lt;code&gt;CommissionEmployee&lt;/code&gt; from &lt;code&gt;SalaryEmployee&lt;/code&gt; because both classes have a &lt;code&gt;weekly_salary&lt;/code&gt; to consider. At the same time, &lt;code&gt;CommissionEmployee&lt;/code&gt; is initialized with a &lt;code&gt;commission&lt;/code&gt; value that is based on the sales for the employee.&lt;/p&gt;
10000 &lt;p&gt;&lt;code&gt;.calculate_payroll()&lt;/code&gt; leverages the implementation of the base class to retrieve the &lt;code&gt;fixed&lt;/code&gt; salary and adds the commission value.&lt;/p&gt;
10001 &lt;p&gt;Since &lt;code&gt;CommissionEmployee&lt;/code&gt; derives from &lt;code&gt;SalaryEmployee&lt;/code&gt;, you have access to the &lt;code&gt;weekly_salary&lt;/code&gt; property directly, and you could&amp;rsquo;ve implemented &lt;code&gt;.calculate_payroll()&lt;/code&gt; using the value of that property.&lt;/p&gt;
10002 &lt;p&gt;The problem with accessing the property directly is that if the implementation of &lt;code&gt;SalaryEmployee.calculate_payroll()&lt;/code&gt; changes, then you&amp;rsquo;ll have to also change the implementation of &lt;code&gt;CommissionEmployee.calculate_payroll()&lt;/code&gt;. It&amp;rsquo;s better to rely on the already implemented method in the base class and extend the functionality as needed.&lt;/p&gt;
10003 &lt;p&gt;You created your first class hierarchy for the system. The UML diagram of the classes looks like this:&lt;/p&gt;
10004 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/ic-initial-employee-inheritance.b5f1e65cb8d1.jpg&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/ic-initial-employee-inheritance.b5f1e65cb8d1.jpg&quot; width=&quot;785&quot; height=&quot;744&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/ic-initial-employee-inheritance.b5f1e65cb8d1.jpg&amp;amp;w=196&amp;amp;sig=58f0045964de008f5860e073922ac03b99fcaf9c 196w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/ic-initial-employee-inheritance.b5f1e65cb8d1.jpg&amp;amp;w=392&amp;amp;sig=fce4157604c9d5ad7f8670d79b4a386bae166b02 392w, https://files.realpython.com/media/ic-initial-employee-inheritance.b5f1e65cb8d1.jpg 785w&quot; sizes=&quot;75vw&quot; alt=&quot;Inheritance example with multiple Employee derived classes&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
10005 &lt;p&gt;The diagram shows the inheritance hierarchy of the classes. The derived classes implement the &lt;code&gt;IPayrollCalculator&lt;/code&gt; interface, which is required by the &lt;code&gt;PayrollSystem&lt;/code&gt;. The &lt;code&gt;PayrollSystem.calculate_payroll()&lt;/code&gt; implementation requires that the &lt;code&gt;employee&lt;/code&gt; objects passed contain an &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, and &lt;code&gt;calculate_payroll()&lt;/code&gt; implementation. &lt;/p&gt;
10006 &lt;p&gt;Interfaces are represented similarly to classes with the word &lt;strong&gt;interface&lt;/strong&gt; above the interface name. Interface names are usually prefixed with a capital &lt;code&gt;I&lt;/code&gt;.&lt;/p&gt;
10007 &lt;p&gt;The application creates its employees and passes them to the payroll system to process payroll:&lt;/p&gt;
10008 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In program.py&lt;/span&gt;
10009 
10010 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hr&lt;/span&gt;
10011 
10012 &lt;span class=&quot;n&quot;&gt;salary_employee&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SalaryEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;John Smith&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10013 &lt;span class=&quot;n&quot;&gt;hourly_employee&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HourlyEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Jane Doe&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10014 &lt;span class=&quot;n&quot;&gt;commission_employee&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CommissionEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Kevin Bacon&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10015 &lt;span class=&quot;n&quot;&gt;payroll_system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PayrollSystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
10016 &lt;span class=&quot;n&quot;&gt;payroll_system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
10017     &lt;span class=&quot;n&quot;&gt;salary_employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10018     &lt;span class=&quot;n&quot;&gt;hourly_employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10019     &lt;span class=&quot;n&quot;&gt;commission_employee&lt;/span&gt;
10020 &lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
10021 &lt;/pre&gt;&lt;/div&gt;
10022 
10023 &lt;p&gt;You can run the program in the command line and see the results:&lt;/p&gt;
10024 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python program.py
10025 
10026 &lt;span class=&quot;go&quot;&gt;Calculating Payroll&lt;/span&gt;
10027 &lt;span class=&quot;go&quot;&gt;===================&lt;/span&gt;
10028 &lt;span class=&quot;go&quot;&gt;Payroll for: 1 - John Smith&lt;/span&gt;
10029 &lt;span class=&quot;go&quot;&gt;- Check amount: 1500&lt;/span&gt;
10030 
10031 &lt;span class=&quot;go&quot;&gt;Payroll for: 2 - Jane Doe&lt;/span&gt;
10032 &lt;span class=&quot;go&quot;&gt;- Check amount: 600&lt;/span&gt;
10033 
10034 &lt;span class=&quot;go&quot;&gt;Payroll for: 3 - Kevin Bacon&lt;/span&gt;
10035 &lt;span class=&quot;go&quot;&gt;- Check amount: 1250&lt;/span&gt;
10036 &lt;/pre&gt;&lt;/div&gt;
10037 
10038 &lt;p&gt;The program creates three employee objects, one for each of the derived classes. Then, it creates the payroll system and passes a list of the employees to its &lt;code&gt;.calculate_payroll()&lt;/code&gt; method, which calculates the payroll for each employee and prints the results.&lt;/p&gt;
10039 &lt;p&gt;Notice how the &lt;code&gt;Employee&lt;/code&gt; base class doesn&amp;rsquo;t define a &lt;code&gt;.calculate_payroll()&lt;/code&gt; method. This means that if you were to create a plain &lt;code&gt;Employee&lt;/code&gt; object and pass it to the &lt;code&gt;PayrollSystem&lt;/code&gt;, then you&amp;rsquo;d get an error. You can try it in the Python interactive interpreter:&lt;/p&gt;
10040 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hr&lt;/span&gt;
10041 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Invalid&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10042 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payroll_system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PayrollSystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
10043 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payroll_system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
10044 
10045 &lt;span class=&quot;go&quot;&gt;Payroll for: 1 - Invalid&lt;/span&gt;
10046 &lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
10047   File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
10048   File &lt;span class=&quot;nb&quot;&gt;&amp;quot;/hr.py&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;39&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;
10049     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;- Check amount: {employee.calculate_payroll()}&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10050 &lt;span class=&quot;gr&quot;&gt;AttributeError&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;&amp;#39;Employee&amp;#39; object has no attribute &amp;#39;calculate_payroll&amp;#39;&lt;/span&gt;
10051 &lt;/pre&gt;&lt;/div&gt;
10052 
10053 &lt;p&gt;While you can instantiate an &lt;code&gt;Employee&lt;/code&gt; object, the object can&amp;rsquo;t be used by the &lt;code&gt;PayrollSystem&lt;/code&gt;. Why? Because it can&amp;rsquo;t &lt;code&gt;.calculate_payroll()&lt;/code&gt; for an &lt;code&gt;Employee&lt;/code&gt;. To meet the requirements of &lt;code&gt;PayrollSystem&lt;/code&gt;, you&amp;rsquo;ll want to convert the &lt;code&gt;Employee&lt;/code&gt; class, which is currently a concrete class, to an abstract class. That way, no employee is ever just an &lt;code&gt;Employee&lt;/code&gt;, but one that implements &lt;code&gt;.calculate_payroll()&lt;/code&gt;.&lt;/p&gt;
10054 &lt;h3 id=&quot;abstract-base-classes-in-python&quot;&gt;Abstract Base Classes in Python&lt;/h3&gt;
10055 &lt;p&gt;The &lt;code&gt;Employee&lt;/code&gt; class in the example above is what is called an abstract base class. Abstract base classes exist to be inherited, but never instantiated. Python provides the &lt;code&gt;abc&lt;/code&gt; module to define abstract base classes.&lt;/p&gt;
10056 &lt;p&gt;You can use &lt;a href=&quot;https://dbader.org/blog/meaning-of-underscores-in-python&quot;&gt;leading underscores&lt;/a&gt; in your class name to communicate that objects of that class should not be created. Underscores provide a friendly way to prevent misuse of your code, but they don&amp;rsquo;t prevent eager users from creating instances of that class. &lt;/p&gt;
10057 &lt;p&gt;The &lt;a href=&quot;https://docs.python.org/3/library/abc.html#module-abc&quot;&gt;&lt;code&gt;abc&lt;/code&gt; module&lt;/a&gt; in the Python standard library provides functionality to prevent creating objects from abstract base classes.&lt;/p&gt;
10058 &lt;p&gt;You can modify the implementation of the &lt;code&gt;Employee&lt;/code&gt; class to ensure that it can&amp;rsquo;t be instantiated:&lt;/p&gt;
10059 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In hr.py&lt;/span&gt;
10060 
10061 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;abc&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ABC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;abstractmethod&lt;/span&gt;
10062 
10063 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ABC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10064     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10065         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;
10066         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
10067 
10068     &lt;span class=&quot;nd&quot;&gt;@abstractmethod&lt;/span&gt;
10069     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10070         &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
10071 &lt;/pre&gt;&lt;/div&gt;
10072 
10073 &lt;p&gt;You derive &lt;code&gt;Employee&lt;/code&gt; from &lt;code&gt;ABC&lt;/code&gt;, making it an abstract base class. Then, you decorate the &lt;code&gt;.calculate_payroll()&lt;/code&gt; method with the &lt;code&gt;@abstractmethod&lt;/code&gt; &lt;a href=&quot;https://realpython.com/primer-on-python-decorators/&quot;&gt;decorator&lt;/a&gt;.&lt;/p&gt;
10074 &lt;p&gt;This change has two nice side-effects:&lt;/p&gt;
10075 &lt;ol&gt;
10076 &lt;li&gt;You&amp;rsquo;re telling users of the module that objects of type &lt;code&gt;Employee&lt;/code&gt; can&amp;rsquo;t be created. &lt;/li&gt;
10077 &lt;li&gt;You&amp;rsquo;re telling other developers working on the &lt;code&gt;hr&lt;/code&gt; module that if they derive from &lt;code&gt;Employee&lt;/code&gt;, then they must override the &lt;code&gt;.calculate_payroll()&lt;/code&gt; abstract method.&lt;/li&gt;
10078 &lt;/ol&gt;
10079 &lt;p&gt;You can see that objects of type &lt;code&gt;Employee&lt;/code&gt; can&amp;rsquo;t be created using the interactive interpreter:&lt;/p&gt;
10080 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hr&lt;/span&gt;
10081 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;abstract&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10082 
10083 &lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
10084   File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
10085 &lt;span class=&quot;gr&quot;&gt;TypeError&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;Can&amp;#39;t instantiate abstract class Employee with abstract methods &lt;/span&gt;
10086 &lt;span class=&quot;go&quot;&gt;calculate_payroll&lt;/span&gt;
10087 &lt;/pre&gt;&lt;/div&gt;
10088 
10089 &lt;p&gt;The output shows that the class cannot be instantiated because it contains an abstract method &lt;code&gt;calculate_payroll()&lt;/code&gt;. Derived classes must override the method to allow creating objects of their type.&lt;/p&gt;
10090 &lt;h3 id=&quot;implementation-inheritance-vs-interface-inheritance&quot;&gt;Implementation Inheritance vs Interface Inheritance&lt;/h3&gt;
10091 &lt;p&gt;When you derive one class from another, the derived class inherits both:&lt;/p&gt;
10092 &lt;ol&gt;
10093 &lt;li&gt;
10094 &lt;p&gt;&lt;strong&gt;The base class interface:&lt;/strong&gt; The derived class inherits all the methods, properties, and attributes of the base class.&lt;/p&gt;
10095 &lt;/li&gt;
10096 &lt;li&gt;
10097 &lt;p&gt;&lt;strong&gt;The base class implementation:&lt;/strong&gt; The derived class inherits the code that implements the class interface.&lt;/p&gt;
10098 &lt;/li&gt;
10099 &lt;/ol&gt;
10100 &lt;p&gt;Most of the time, you&amp;rsquo;ll want to inherit the implementation of a class, but you will want to implement multiple interfaces, so your objects can be used in different situations.&lt;/p&gt;
10101 &lt;p&gt;Modern programming languages are designed with this basic concept in mind. They allow you to inherit from a single class, but you can implement multiple interfaces.&lt;/p&gt;
10102 &lt;p&gt;In Python, you don&amp;rsquo;t have to explicitly declare an interface. Any object that implements the desired interface can be used in place of another object. This is known as &lt;a href=&quot;https://realpython.com/python-type-checking/#duck-typing&quot;&gt;&lt;strong&gt;duck typing&lt;/strong&gt;&lt;/a&gt;. Duck typing is usually explained as &amp;ldquo;if it behaves like a duck, then it&amp;rsquo;s a duck.&amp;rdquo;&lt;/p&gt;
10103 &lt;p&gt;To illustrate this, you will now add a &lt;code&gt;DisgruntledEmployee&lt;/code&gt; class to the example above which doesn&amp;rsquo;t derive from &lt;code&gt;Employee&lt;/code&gt;:&lt;/p&gt;
10104 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In disgruntled.py&lt;/span&gt;
10105 
10106 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DisgruntledEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10107     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10108         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;
10109         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
10110 
10111     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10112         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000000&lt;/span&gt;
10113 &lt;/pre&gt;&lt;/div&gt;
10114 
10115 &lt;p&gt;The &lt;code&gt;DisgruntledEmployee&lt;/code&gt; class doesn&amp;rsquo;t derive from &lt;code&gt;Employee&lt;/code&gt;, but it exposes the same interface required by the &lt;code&gt;PayrollSystem&lt;/code&gt;. The &lt;code&gt;PayrollSystem.calculate_payroll()&lt;/code&gt; requires a list of objects that implement the following interface:&lt;/p&gt;
10116 &lt;ul&gt;
10117 &lt;li&gt;An &lt;strong&gt;&lt;code&gt;id&lt;/code&gt;&lt;/strong&gt; property or attribute that returns the employee&amp;rsquo;s id&lt;/li&gt;
10118 &lt;li&gt;A &lt;strong&gt;&lt;code&gt;name&lt;/code&gt;&lt;/strong&gt; property or attribute that represents the employee&amp;rsquo;s name&lt;/li&gt;
10119 &lt;li&gt;A &lt;strong&gt;&lt;code&gt;.calculate_payroll()&lt;/code&gt;&lt;/strong&gt; method that doesn&amp;rsquo;t take any parameters and returns the payroll amount to process&lt;/li&gt;
10120 &lt;/ul&gt;
10121 &lt;p&gt;All these requirements are met by the &lt;code&gt;DisgruntledEmployee&lt;/code&gt; class, so the &lt;code&gt;PayrollSystem&lt;/code&gt; can still calculate its payroll.&lt;/p&gt;
10122 &lt;p&gt;You can modify the program to use the &lt;code&gt;DisgruntledEmployee&lt;/code&gt; class:&lt;/p&gt;
10123 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In program.py&lt;/span&gt;
10124 
10125 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hr&lt;/span&gt;
10126 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;disgruntled&lt;/span&gt;
10127 
10128 &lt;span class=&quot;n&quot;&gt;salary_employee&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SalaryEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;John Smith&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10129 &lt;span class=&quot;n&quot;&gt;hourly_employee&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HourlyEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Jane Doe&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10130 &lt;span class=&quot;n&quot;&gt;commission_employee&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CommissionEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Kevin Bacon&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10131 &lt;span class=&quot;n&quot;&gt;disgruntled_employee&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;disgruntled&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DisgruntledEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Anonymous&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10132 &lt;span class=&quot;n&quot;&gt;payroll_system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PayrollSystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
10133 &lt;span class=&quot;n&quot;&gt;payroll_system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
10134     &lt;span class=&quot;n&quot;&gt;salary_employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10135     &lt;span class=&quot;n&quot;&gt;hourly_employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10136     &lt;span class=&quot;n&quot;&gt;commission_employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10137     &lt;span class=&quot;n&quot;&gt;disgruntled_employee&lt;/span&gt;
10138 &lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
10139 &lt;/pre&gt;&lt;/div&gt;
10140 
10141 &lt;p&gt;The program creates a &lt;code&gt;DisgruntledEmployee&lt;/code&gt; object and adds it to the list processed by the &lt;code&gt;PayrollSystem&lt;/code&gt;. You can now run the program and see its output:&lt;/p&gt;
10142 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python program.py
10143 
10144 &lt;span class=&quot;go&quot;&gt;Calculating Payroll&lt;/span&gt;
10145 &lt;span class=&quot;go&quot;&gt;===================&lt;/span&gt;
10146 &lt;span class=&quot;go&quot;&gt;Payroll for: 1 - John Smith&lt;/span&gt;
10147 &lt;span class=&quot;go&quot;&gt;- Check amount: 1500&lt;/span&gt;
10148 
10149 &lt;span class=&quot;go&quot;&gt;Payroll for: 2 - Jane Doe&lt;/span&gt;
10150 &lt;span class=&quot;go&quot;&gt;- Check amount: 600&lt;/span&gt;
10151 
10152 &lt;span class=&quot;go&quot;&gt;Payroll for: 3 - Kevin Bacon&lt;/span&gt;
10153 &lt;span class=&quot;go&quot;&gt;- Check amount: 1250&lt;/span&gt;
10154 
10155 &lt;span class=&quot;go&quot;&gt;Payroll for: 20000 - Anonymous&lt;/span&gt;
10156 &lt;span class=&quot;go&quot;&gt;- Check amount: 1000000&lt;/span&gt;
10157 &lt;/pre&gt;&lt;/div&gt;
10158 
10159 &lt;p&gt;As you can see, the &lt;code&gt;PayrollSystem&lt;/code&gt; can still process the new object because it meets the desired interface.&lt;/p&gt;
10160 &lt;p&gt;Since you don&amp;rsquo;t have to derive from a specific class for your objects to be reusable by the program, you may be asking why you should use inheritance instead of just implementing the desired interface. The following rules may help you:&lt;/p&gt;
10161 &lt;ul&gt;
10162 &lt;li&gt;
10163 &lt;p&gt;&lt;strong&gt;Use inheritance to reuse an implementation:&lt;/strong&gt; Your derived classes should leverage most of their base class implementation. They must also model an &lt;strong&gt;is a&lt;/strong&gt; relationship. A &lt;code&gt;Customer&lt;/code&gt; class might also have an &lt;code&gt;id&lt;/code&gt; and a &lt;code&gt;name&lt;/code&gt;, but a &lt;code&gt;Customer&lt;/code&gt; is not an &lt;code&gt;Employee&lt;/code&gt;, so you should not use inheritance.&lt;/p&gt;
10164 &lt;/li&gt;
10165 &lt;li&gt;
10166 &lt;p&gt;&lt;strong&gt;Implement an interface to be reused:&lt;/strong&gt; When you want your class to be reused by a specific part of your application, you implement the required interface in your class, but you don&amp;rsquo;t need to provide a base class, or inherit from another class.&lt;/p&gt;
10167 &lt;/li&gt;
10168 &lt;/ul&gt;
10169 &lt;p&gt;You can now clean up the example above to move onto the next topic. You can delete the &lt;code&gt;disgruntled.py&lt;/code&gt; file and then modify the &lt;code&gt;hr&lt;/code&gt; module to its original state:&lt;/p&gt;
10170 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In hr.py&lt;/span&gt;
10171 
10172 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PayrollSystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10173     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10174         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Calculating Payroll&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10175         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;===================&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10176         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10177             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Payroll for: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{employee.id}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{employee.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10178             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;- Check amount: {employee.calculate_payroll()}&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10179             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10180 
10181 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10182     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10183         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;
10184         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
10185 
10186 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SalaryEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10187     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10188         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10189         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;
10190 
10191     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10192         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;
10193 
10194 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HourlyEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10195     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10196         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10197         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt;
10198         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;
10199 
10200     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10201         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;
10202 
10203 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CommissionEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SalaryEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10204     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commission&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10205         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10206         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;commission&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commission&lt;/span&gt;
10207 
10208     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10209         &lt;span class=&quot;n&quot;&gt;fixed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
10210         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fixed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;commission&lt;/span&gt;
10211 &lt;/pre&gt;&lt;/div&gt;
10212 
10213 &lt;p&gt;You removed the import of the &lt;code&gt;abc&lt;/code&gt; module since the &lt;code&gt;Employee&lt;/code&gt; class doesn&amp;rsquo;t need to be abstract. You also removed the abstract &lt;code&gt;calculate_payroll()&lt;/code&gt; method from it since it doesn&amp;rsquo;t provide any implementation.&lt;/p&gt;
10214 &lt;p&gt;Basically, you are inheriting the implementation of the &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;name&lt;/code&gt; attributes of the &lt;code&gt;Employee&lt;/code&gt; class in your derived classes. Since &lt;code&gt;.calculate_payroll()&lt;/code&gt; is just an interface to the &lt;code&gt;PayrollSystem.calculate_payroll()&lt;/code&gt; method, you don&amp;rsquo;t need to implement it in the &lt;code&gt;Employee&lt;/code&gt; base class.&lt;/p&gt;
10215 &lt;p&gt;Notice how the &lt;code&gt;CommissionEmployee&lt;/code&gt; class derives from &lt;code&gt;SalaryEmployee&lt;/code&gt;. This means that &lt;code&gt;CommissionEmployee&lt;/code&gt; inherits the implementation and interface of &lt;code&gt;SalaryEmployee&lt;/code&gt;. You can see how the &lt;code&gt;CommissionEmployee.calculate_payroll()&lt;/code&gt; method leverages the base class implementation because it relies on the result from &lt;code&gt;super().calculate_payroll()&lt;/code&gt; to implement its own version.&lt;/p&gt;
10216 &lt;h3 id=&quot;the-class-explosion-problem&quot;&gt;The Class Explosion Problem&lt;/h3&gt;
10217 &lt;p&gt;If you are not careful, inheritance can lead you to a huge hierarchical structure of classes that is hard to understand and maintain. This is known as the &lt;strong&gt;class explosion problem&lt;/strong&gt;.&lt;/p&gt;
10218 &lt;p&gt;You started building a class hierarchy of &lt;code&gt;Employee&lt;/code&gt; types used by the &lt;code&gt;PayrollSystem&lt;/code&gt; to calculate payroll. Now, you need to add some functionality to those classes, so they can be used with the new &lt;code&gt;ProductivitySystem&lt;/code&gt;. &lt;/p&gt;
10219 &lt;p&gt;The &lt;code&gt;ProductivitySystem&lt;/code&gt; tracks productivity based on employee roles. There are different employee roles:&lt;/p&gt;
10220 &lt;ul&gt;
10221 &lt;li&gt;&lt;strong&gt;Managers:&lt;/strong&gt; They walk around yelling at people telling them what to do. They are salaried employees and make more money.&lt;/li&gt;
10222 &lt;li&gt;&lt;strong&gt;Secretaries:&lt;/strong&gt; They do all the paper work for managers and ensure that everything gets billed and payed on time. They are also salaried employees but make less money.&lt;/li&gt;
10223 &lt;li&gt;&lt;strong&gt;Sales employees:&lt;/strong&gt; They make a lot of phone calls to sell products. They have a salary, but they also get commissions for sales.&lt;/li&gt;
10224 &lt;li&gt;&lt;strong&gt;Factory workers:&lt;/strong&gt; They manufacture the products for the company. They are paid by the hour.&lt;/li&gt;
10225 &lt;/ul&gt;
10226 &lt;p&gt;With those requirements, you start to see that &lt;code&gt;Employee&lt;/code&gt; and its derived classes might belong somewhere other than the &lt;code&gt;hr&lt;/code&gt; module because now they&amp;rsquo;re also used by the &lt;code&gt;ProductivitySystem&lt;/code&gt;.&lt;/p&gt;
10227 &lt;p&gt;You create an &lt;code&gt;employees&lt;/code&gt; module and move the classes there:&lt;/p&gt;
10228 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In employees.py&lt;/span&gt;
10229 
10230 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10231     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10232         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;
10233         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
10234 
10235 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SalaryEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10236     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10237         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10238         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;
10239 
10240     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10241         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;
10242 
10243 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HourlyEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10244     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10245         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10246         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt;
10247         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;
10248 
10249     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10250         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;
10251 
10252 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CommissionEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SalaryEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10253     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commission&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10254         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10255         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;commission&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commission&lt;/span&gt;
10256 
10257     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10258         &lt;span class=&quot;n&quot;&gt;fixed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
10259         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fixed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;commission&lt;/span&gt;
10260 &lt;/pre&gt;&lt;/div&gt;
10261 
10262 &lt;p&gt;The implementation remains the same, but you move the classes to the &lt;code&gt;employee&lt;/code&gt; module. Now, you change your program to support the change:&lt;/p&gt;
10263 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In program.py&lt;/span&gt;
10264 
10265 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hr&lt;/span&gt;
10266 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;employees&lt;/span&gt;
10267 
10268 &lt;span class=&quot;n&quot;&gt;salary_employee&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SalaryEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;John Smith&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10269 &lt;span class=&quot;n&quot;&gt;hourly_employee&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HourlyEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Jane Doe&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10270 &lt;span class=&quot;n&quot;&gt;commission_employee&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CommissionEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Kevin Bacon&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10271 &lt;span class=&quot;n&quot;&gt;payroll_system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PayrollSystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
10272 &lt;span class=&quot;n&quot;&gt;payroll_system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
10273     &lt;span class=&quot;n&quot;&gt;salary_employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10274     &lt;span class=&quot;n&quot;&gt;hourly_employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10275     &lt;span class=&quot;n&quot;&gt;commission_employee&lt;/span&gt;
10276 &lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
10277 &lt;/pre&gt;&lt;/div&gt;
10278 
10279 &lt;p&gt;You run the program and verify that it still works:&lt;/p&gt;
10280 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python program.py
10281 
10282 &lt;span class=&quot;go&quot;&gt;Calculating Payroll&lt;/span&gt;
10283 &lt;span class=&quot;go&quot;&gt;===================&lt;/span&gt;
10284 &lt;span class=&quot;go&quot;&gt;Payroll for: 1 - John Smith&lt;/span&gt;
10285 &lt;span class=&quot;go&quot;&gt;- Check amount: 1500&lt;/span&gt;
10286 
10287 &lt;span class=&quot;go&quot;&gt;Payroll for: 2 - Jane Doe&lt;/span&gt;
10288 &lt;span class=&quot;go&quot;&gt;- Check amount: 600&lt;/span&gt;
10289 
10290 &lt;span class=&quot;go&quot;&gt;Payroll for: 3 - Kevin Bacon&lt;/span&gt;
10291 &lt;span class=&quot;go&quot;&gt;- Check amount: 1250&lt;/span&gt;
10292 &lt;/pre&gt;&lt;/div&gt;
10293 
10294 &lt;p&gt;With everything in place, you start adding the new classes:&lt;/p&gt;
10295 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In employees.py&lt;/span&gt;
10296 
10297 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Manager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SalaryEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10298     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10299         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; screams and yells for &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{hours}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; hours.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10300 
10301 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Secretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SalaryEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10302     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10303         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; expends &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{hours}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; hours doing office paperwork.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10304 
10305 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SalesPerson&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CommissionEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10306     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10307         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; expends &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{hours}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; hours on the phone.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10308 
10309 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FactoryWorker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HourlyEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10310     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10311         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; manufactures gadgets for &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{hours}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; hours.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10312 &lt;/pre&gt;&lt;/div&gt;
10313 
10314 &lt;p&gt;First, you add a &lt;code&gt;Manager&lt;/code&gt; class that derives from &lt;code&gt;SalaryEmployee&lt;/code&gt;. The class exposes a method &lt;code&gt;work()&lt;/code&gt; that will be used by the productivity system. The method takes the &lt;code&gt;hours&lt;/code&gt; the employee worked.&lt;/p&gt;
10315 &lt;p&gt;Then you add &lt;code&gt;Secretary&lt;/code&gt;, &lt;code&gt;SalesPerson&lt;/code&gt;, and &lt;code&gt;FactoryWorker&lt;/code&gt; and then implement the &lt;code&gt;work()&lt;/code&gt; interface, so they can be used by the productivity system.&lt;/p&gt;
10316 &lt;p&gt;Now, you can add the &lt;code&gt;ProductivitySytem&lt;/code&gt; class:&lt;/p&gt;
10317 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In productivity.py&lt;/span&gt;
10318 
10319 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ProductivitySystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10320     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10321         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Tracking Employee Productivity&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10322         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;==============================&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10323         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10324             &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10325         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10326 &lt;/pre&gt;&lt;/div&gt;
10327 
10328 &lt;p&gt;The class tracks employees in the &lt;code&gt;track()&lt;/code&gt; method that takes a list of employees and the number of hours to track. You can now add the productivity system to your program:&lt;/p&gt;
10329 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In program.py&lt;/span&gt;
10330 
10331 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hr&lt;/span&gt;
10332 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;employees&lt;/span&gt;
10333 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;productivity&lt;/span&gt;
10334 
10335 &lt;span class=&quot;n&quot;&gt;manager&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Manager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Mary Poppins&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10336 &lt;span class=&quot;n&quot;&gt;secretary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Secretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;John Smith&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10337 &lt;span class=&quot;n&quot;&gt;sales_guy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SalesPerson&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Kevin Bacon&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10338 &lt;span class=&quot;n&quot;&gt;factory_worker&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FactoryWorker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Jane Doe&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10339 &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
10340     &lt;span class=&quot;n&quot;&gt;manager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10341     &lt;span class=&quot;n&quot;&gt;secretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10342     &lt;span class=&quot;n&quot;&gt;sales_guy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10343     &lt;span class=&quot;n&quot;&gt;factory_worker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10344 &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
10345 &lt;span class=&quot;n&quot;&gt;productivity_system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;productivity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ProductivitySystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
10346 &lt;span class=&quot;n&quot;&gt;productivity_system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10347 &lt;span class=&quot;n&quot;&gt;payroll_system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PayrollSystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
10348 &lt;span class=&quot;n&quot;&gt;payroll_system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10349 &lt;/pre&gt;&lt;/div&gt;
10350 
10351 &lt;p&gt;The program creates a list of employees of different types. The employee list is sent to the productivity system to track their work for 40 hours. Then the same list of employees is sent to the payroll system to calculate their payroll.&lt;/p&gt;
10352 &lt;p&gt;You can run the program to see the output:&lt;/p&gt;
10353 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python program.py
10354 
10355 &lt;span class=&quot;go&quot;&gt;Tracking Employee Productivity&lt;/span&gt;
10356 &lt;span class=&quot;go&quot;&gt;==============================&lt;/span&gt;
10357 &lt;span class=&quot;go&quot;&gt;Mary Poppins screams and yells for 40 hours.&lt;/span&gt;
10358 &lt;span class=&quot;go&quot;&gt;John Smith expends 40 hours doing office paperwork.&lt;/span&gt;
10359 &lt;span class=&quot;go&quot;&gt;Kevin Bacon expends 40 hours on the phone.&lt;/span&gt;
10360 &lt;span class=&quot;go&quot;&gt;Jane Doe manufactures gadgets for 40 hours.&lt;/span&gt;
10361 
10362 &lt;span class=&quot;go&quot;&gt;Calculating Payroll&lt;/span&gt;
10363 &lt;span class=&quot;go&quot;&gt;===================&lt;/span&gt;
10364 &lt;span class=&quot;go&quot;&gt;Payroll for: 1 - Mary Poppins&lt;/span&gt;
10365 &lt;span class=&quot;go&quot;&gt;- Check amount: 3000&lt;/span&gt;
10366 
10367 &lt;span class=&quot;go&quot;&gt;Payroll for: 2 - John Smith&lt;/span&gt;
10368 &lt;span class=&quot;go&quot;&gt;- Check amount: 1500&lt;/span&gt;
10369 
10370 &lt;span class=&quot;go&quot;&gt;Payroll for: 3 - Kevin Bacon&lt;/span&gt;
10371 &lt;span class=&quot;go&quot;&gt;- Check amount: 1250&lt;/span&gt;
10372 
10373 &lt;span class=&quot;go&quot;&gt;Payroll for: 4 - Jane Doe&lt;/span&gt;
10374 &lt;span class=&quot;go&quot;&gt;- Check amount: 600&lt;/span&gt;
10375 &lt;/pre&gt;&lt;/div&gt;
10376 
10377 &lt;p&gt;The program shows the employees working for 40 hours through the productivity system. Then it calculates and displays the payroll for each of the employees.&lt;/p&gt;
10378 &lt;p&gt;The program works as expected, but you had to add four new classes to support the changes. As new requirements come, your class hierarchy will inevitably grow, leading to the class explosion problem where your hierarchies will become so big that they&amp;rsquo;ll be hard to understand and maintain.&lt;/p&gt;
10379 &lt;p&gt;The following diagram shows the new class hierarchy:&lt;/p&gt;
10380 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/ic-class-explosion.a3d42b8c9b91.jpg&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/ic-class-explosion.a3d42b8c9b91.jpg&quot; width=&quot;1184&quot; height=&quot;1199&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/ic-class-explosion.a3d42b8c9b91.jpg&amp;amp;w=296&amp;amp;sig=3df3dc7dac2263ed3beba586e8731fc276936b39 296w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/ic-class-explosion.a3d42b8c9b91.jpg&amp;amp;w=592&amp;amp;sig=e2d4c2f1d5145230cd248e731aca8cdfe5e25709 592w, https://files.realpython.com/media/ic-class-explosion.a3d42b8c9b91.jpg 1184w&quot; sizes=&quot;75vw&quot; alt=&quot;Class design explosion by inheritance&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
10381 &lt;p&gt;The diagram shows how the class hierarchy is growing. Additional requirements might have an exponential effect in the number of classes with this design.&lt;/p&gt;
10382 &lt;h3 id=&quot;inheriting-multiple-classes&quot;&gt;Inheriting Multiple Classes&lt;/h3&gt;
10383 &lt;p&gt;Python is one of the few modern programming languages that supports multiple inheritance. Multiple inheritance is the ability to derive a class from multiple base classes at the same time.&lt;/p&gt;
10384 &lt;p&gt;Multiple inheritance has a bad reputation to the extent that most modern programming languages don&amp;rsquo;t support it. Instead, modern programming languages support the concept of interfaces. In those languages, you inherit from a single base class and then implement multiple interfaces, so your class can be re-used in different situations.&lt;/p&gt;
10385 &lt;p&gt;This approach puts some constraints in your designs. You can only inherit the implementation of one class by directly deriving from it. You can implement multiple interfaces, but you can&amp;rsquo;t inherit the implementation of multiple classes.&lt;/p&gt;
10386 &lt;p&gt;This constraint is good for software design because it forces you to design your classes with fewer dependencies on each other. You will see later in this article that you can leverage multiple implementations through composition, which makes software more flexible. This section, however, is about multiple inheritance, so let&amp;rsquo;s take a look at how it works.&lt;/p&gt;
10387 &lt;p&gt;It turns out that sometimes temporary secretaries are hired when there is too much paperwork to do. The &lt;code&gt;TemporarySecretary&lt;/code&gt; class performs the role of a &lt;code&gt;Secretary&lt;/code&gt; in the context of the &lt;code&gt;ProductivitySystem&lt;/code&gt;, but for payroll purposes, it is an &lt;code&gt;HourlyEmployee&lt;/code&gt;.&lt;/p&gt;
10388 &lt;p&gt;You look at your class design. It has grown a little bit, but you can still understand how it works. It seems you have two options:&lt;/p&gt;
10389 &lt;ol&gt;
10390 &lt;li&gt;
10391 &lt;p&gt;&lt;strong&gt;Derive from &lt;code&gt;Secretary&lt;/code&gt;:&lt;/strong&gt; You can derive from &lt;code&gt;Secretary&lt;/code&gt; to inherit the &lt;code&gt;.work()&lt;/code&gt; method for the role, and then override the &lt;code&gt;.calculate_payroll()&lt;/code&gt; method to implement it as an &lt;code&gt;HourlyEmployee&lt;/code&gt;.&lt;/p&gt;
10392 &lt;/li&gt;
10393 &lt;li&gt;
10394 &lt;p&gt;&lt;strong&gt;Derive from &lt;code&gt;HourlyEmployee&lt;/code&gt;:&lt;/strong&gt; You can derive from &lt;code&gt;HourlyEmployee&lt;/code&gt; to inherit the &lt;code&gt;.calculate_payroll()&lt;/code&gt; method, and then override the &lt;code&gt;.work()&lt;/code&gt; method to implement it as a &lt;code&gt;Secretary&lt;/code&gt;.&lt;/p&gt;
10395 &lt;/li&gt;
10396 &lt;/ol&gt;
10397 &lt;p&gt;Then, you remember that Python supports multiple inheritance, so you decide to derive from both &lt;code&gt;Secretary&lt;/code&gt; and &lt;code&gt;HourlyEmployee&lt;/code&gt;:&lt;/p&gt;
10398 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In employees.py&lt;/span&gt;
10399 
10400 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TemporarySecretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Secretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HourlyEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10401     &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
10402 &lt;/pre&gt;&lt;/div&gt;
10403 
10404 &lt;p&gt;Python allows you to inherit from two different classes by specifying them between parenthesis in the class declaration.&lt;/p&gt;
10405 &lt;p&gt;Now, you modify your program to add the new temporary secretary employee:&lt;/p&gt;
10406 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hr&lt;/span&gt;
10407 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;employees&lt;/span&gt;
10408 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;productivity&lt;/span&gt;
10409 
10410 &lt;span class=&quot;n&quot;&gt;manager&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Manager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Mary Poppins&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10411 &lt;span class=&quot;n&quot;&gt;secretary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Secretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;John Smith&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10412 &lt;span class=&quot;n&quot;&gt;sales_guy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SalesPerson&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Kevin Bacon&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10413 &lt;span class=&quot;n&quot;&gt;factory_worker&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FactoryWorker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Jane Doe&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10414 &lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;temporary_secretary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TemporarySecretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Robin Williams&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10415 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;company_employees&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
10416     &lt;span class=&quot;n&quot;&gt;manager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10417     &lt;span class=&quot;n&quot;&gt;secretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10418     &lt;span class=&quot;n&quot;&gt;sales_guy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10419     &lt;span class=&quot;n&quot;&gt;factory_worker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10420 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;temporary_secretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10421 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
10422 &lt;span class=&quot;n&quot;&gt;productivity_system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;productivity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ProductivitySystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
10423 &lt;span class=&quot;n&quot;&gt;productivity_system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;company_employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10424 &lt;span class=&quot;n&quot;&gt;payroll_system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PayrollSystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
10425 &lt;span class=&quot;n&quot;&gt;payroll_system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;company_employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10426 &lt;/pre&gt;&lt;/div&gt;
10427 
10428 &lt;p&gt;You run the program to test it:&lt;/p&gt;
10429 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python program.py
10430 
10431 &lt;span class=&quot;go&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
10432 &lt;span class=&quot;go&quot;&gt; File &amp;quot;.\program.py&amp;quot;, line 9, in &amp;lt;module&amp;gt;&lt;/span&gt;
10433 &lt;span class=&quot;go&quot;&gt;  temporary_secretary = employee.TemporarySecretary(5, &amp;#39;Robin Williams&amp;#39;, 40, 9)&lt;/span&gt;
10434 &lt;span class=&quot;go&quot;&gt;TypeError: __init__() takes 4 positional arguments but 5 were given&lt;/span&gt;
10435 &lt;/pre&gt;&lt;/div&gt;
10436 
10437 &lt;p&gt;You get a &lt;a href=&quot;https://docs.python.org/3/library/exceptions.html#TypeError&quot;&gt;&lt;code&gt;TypeError&lt;/code&gt;&lt;/a&gt; exception saying that &lt;code&gt;4&lt;/code&gt; positional arguments where expected, but &lt;code&gt;5&lt;/code&gt; were given.&lt;/p&gt;
10438 &lt;p&gt;This is because you derived &lt;code&gt;TemporarySecretary&lt;/code&gt; first from &lt;code&gt;Secretary&lt;/code&gt; and then from &lt;code&gt;HourlyEmployee&lt;/code&gt;, so the interpreter is trying to use &lt;code&gt;Secretary.__init__()&lt;/code&gt; to initialize the object.&lt;/p&gt;
10439 &lt;p&gt;Okay, let&amp;rsquo;s reverse it:&lt;/p&gt;
10440 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TemporarySecretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HourlyEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Secretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10441     &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
10442 &lt;/pre&gt;&lt;/div&gt;
10443 
10444 &lt;p&gt;Now, run the program again and see what happens:&lt;/p&gt;
10445 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python program.py
10446 
10447 &lt;span class=&quot;go&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
10448 &lt;span class=&quot;go&quot;&gt; File &amp;quot;.\program.py&amp;quot;, line 9, in &amp;lt;module&amp;gt;&lt;/span&gt;
10449 &lt;span class=&quot;go&quot;&gt;  temporary_secretary = employee.TemporarySecretary(5, &amp;#39;Robin Williams&amp;#39;, 40, 9)&lt;/span&gt;
10450 &lt;span class=&quot;go&quot;&gt; File &amp;quot;employee.py&amp;quot;, line 16, in __init__&lt;/span&gt;
10451 &lt;span class=&quot;go&quot;&gt;  super().__init__(id, name)&lt;/span&gt;
10452 &lt;span class=&quot;go&quot;&gt;TypeError: __init__() missing 1 required positional argument: &amp;#39;weekly_salary&amp;#39;&lt;/span&gt;
10453 &lt;/pre&gt;&lt;/div&gt;
10454 
10455 &lt;p&gt;Now it seems you are missing a &lt;code&gt;weekly_salary&lt;/code&gt; parameter, which is necessary to initialize &lt;code&gt;Secretary&lt;/code&gt;, but that parameter doesn&amp;rsquo;t make sense in the context of a &lt;code&gt;TemporarySecretary&lt;/code&gt; because it&amp;rsquo;s an &lt;code&gt;HourlyEmployee&lt;/code&gt;.&lt;/p&gt;
10456 &lt;p&gt;Maybe implementing &lt;code&gt;TemporarySecretary.__init__()&lt;/code&gt; will help:&lt;/p&gt;
10457 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In employees.py&lt;/span&gt;
10458 
10459 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TemporarySecretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HourlyEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Secretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10460     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10461         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10462 &lt;/pre&gt;&lt;/div&gt;
10463 
10464 &lt;p&gt;Try it:&lt;/p&gt;
10465 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python program.py
10466 
10467 &lt;span class=&quot;go&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
10468 &lt;span class=&quot;go&quot;&gt; File &amp;quot;.\program.py&amp;quot;, line 9, in &amp;lt;module&amp;gt;&lt;/span&gt;
10469 &lt;span class=&quot;go&quot;&gt;  temporary_secretary = employee.TemporarySecretary(5, &amp;#39;Robin Williams&amp;#39;, 40, 9)&lt;/span&gt;
10470 &lt;span class=&quot;go&quot;&gt; File &amp;quot;employee.py&amp;quot;, line 54, in __init__&lt;/span&gt;
10471 &lt;span class=&quot;go&quot;&gt;  super().__init__(id, name, hours_worked, hour_rate)&lt;/span&gt;
10472 &lt;span class=&quot;go&quot;&gt; File &amp;quot;employee.py&amp;quot;, line 16, in __init__&lt;/span&gt;
10473 &lt;span class=&quot;go&quot;&gt;  super().__init__(id, name)&lt;/span&gt;
10474 &lt;span class=&quot;go&quot;&gt;TypeError: __init__() missing 1 required positional argument: &amp;#39;weekly_salary&amp;#39;&lt;/span&gt;
10475 &lt;/pre&gt;&lt;/div&gt;
10476 
10477 &lt;p&gt;That didn&amp;rsquo;t work either. Okay, it&amp;rsquo;s time for you to dive into Python&amp;rsquo;s &lt;strong&gt;method resolution order&lt;/strong&gt; (MRO) to see what&amp;rsquo;s going on.&lt;/p&gt;
10478 &lt;p&gt;When a method or attribute of a class is accessed, Python uses the class &lt;a href=&quot;https://www.python.org/download/releases/2.3/mro/&quot;&gt;MRO&lt;/a&gt; to find it. The MRO is also used by &lt;code&gt;super()&lt;/code&gt; to determine which method or attribute to invoke. You can learn more about &lt;code&gt;super()&lt;/code&gt; in &lt;a href=&quot;https://realpython.com/python-super/&quot;&gt;Supercharge Your Classes With Python super()&lt;/a&gt;.&lt;/p&gt;
10479 &lt;p&gt;You can evaluate the &lt;code&gt;TemporarySecretary&lt;/code&gt; class MRO using the interactive interpreter:&lt;/p&gt;
10480 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;employees&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TemporarySecretary&lt;/span&gt;
10481 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TemporarySecretary&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;vm&quot;&gt;__mro__&lt;/span&gt;
10482 
10483 &lt;span class=&quot;go&quot;&gt;(&amp;lt;class &amp;#39;employees.TemporarySecretary&amp;#39;&amp;gt;,&lt;/span&gt;
10484 &lt;span class=&quot;go&quot;&gt; &amp;lt;class &amp;#39;employees.HourlyEmployee&amp;#39;&amp;gt;,&lt;/span&gt;
10485 &lt;span class=&quot;go&quot;&gt; &amp;lt;class &amp;#39;employees.Secretary&amp;#39;&amp;gt;,&lt;/span&gt;
10486 &lt;span class=&quot;go&quot;&gt; &amp;lt;class &amp;#39;employees.SalaryEmployee&amp;#39;&amp;gt;,&lt;/span&gt;
10487 &lt;span class=&quot;go&quot;&gt; &amp;lt;class &amp;#39;employees.Employee&amp;#39;&amp;gt;,&lt;/span&gt;
10488 &lt;span class=&quot;go&quot;&gt; &amp;lt;class &amp;#39;object&amp;#39;&amp;gt;&lt;/span&gt;
10489 &lt;span class=&quot;go&quot;&gt;)&lt;/span&gt;
10490 &lt;/pre&gt;&lt;/div&gt;
10491 
10492 &lt;p&gt;The MRO shows the order in which Python is going to look for a matching attribute or method. In the example, this is what happens when we create the &lt;code&gt;TemporarySecretary&lt;/code&gt; object:&lt;/p&gt;
10493 &lt;ol&gt;
10494 &lt;li&gt;
10495 &lt;p&gt;The &lt;code&gt;TemporarySecretary.__init__(self, id, name, hours_worked, hour_rate)&lt;/code&gt; method is called.&lt;/p&gt;
10496 &lt;/li&gt;
10497 &lt;li&gt;
10498 &lt;p&gt;The &lt;code&gt;super().__init__(id, name, hours_worked, hour_rate)&lt;/code&gt; call matches &lt;code&gt;HourlyEmployee.__init__(self, id, name, hour_worked, hour_rate)&lt;/code&gt;.&lt;/p&gt;
10499 &lt;/li&gt;
10500 &lt;li&gt;
10501 &lt;p&gt;&lt;code&gt;HourlyEmployee&lt;/code&gt; calls &lt;code&gt;super().__init__(id, name)&lt;/code&gt;, which the MRO is going to match to &lt;code&gt;Secretary.__init__()&lt;/code&gt;, which is inherited from &lt;code&gt;SalaryEmployee.__init__(self, id, name, weekly_salary)&lt;/code&gt;.&lt;/p&gt;
10502 &lt;/li&gt;
10503 &lt;/ol&gt;
10504 &lt;p&gt;Because the parameters don&amp;rsquo;t match, a &lt;code&gt;TypeError&lt;/code&gt; exception is raised.&lt;/p&gt;
10505 &lt;p&gt;You can bypass the MRO by reversing the inheritance order and directly calling &lt;code&gt;HourlyEmployee.__init__()&lt;/code&gt; as follows:&lt;/p&gt;
10506 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TemporarySecretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Secretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HourlyEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10507     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10508         &lt;span class=&quot;n&quot;&gt;HourlyEmployee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10509 &lt;/pre&gt;&lt;/div&gt;
10510 
10511 &lt;p&gt;That solves the problem of creating the object, but you will run into a similar problem when trying to calculate payroll. You can run the program to see the problem:&lt;/p&gt;
10512 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python program.py
10513 
10514 &lt;span class=&quot;go&quot;&gt;Tracking Employee Productivity&lt;/span&gt;
10515 &lt;span class=&quot;go&quot;&gt;==============================&lt;/span&gt;
10516 &lt;span class=&quot;go&quot;&gt;Mary Poppins screams and yells for 40 hours.&lt;/span&gt;
10517 &lt;span class=&quot;go&quot;&gt;John Smith expends 40 hours doing office paperwork.&lt;/span&gt;
10518 &lt;span class=&quot;go&quot;&gt;Kevin Bacon expends 40 hours on the phone.&lt;/span&gt;
10519 &lt;span class=&quot;go&quot;&gt;Jane Doe manufactures gadgets for 40 hours.&lt;/span&gt;
10520 &lt;span class=&quot;go&quot;&gt;Robin Williams expends 40 hours doing office paperwork.&lt;/span&gt;
10521 
10522 &lt;span class=&quot;go&quot;&gt;Calculating Payroll&lt;/span&gt;
10523 &lt;span class=&quot;go&quot;&gt;===================&lt;/span&gt;
10524 &lt;span class=&quot;go&quot;&gt;Payroll for: 1 - Mary Poppins&lt;/span&gt;
10525 &lt;span class=&quot;go&quot;&gt;- Check amount: 3000&lt;/span&gt;
10526 
10527 &lt;span class=&quot;go&quot;&gt;Payroll for: 2 - John Smith&lt;/span&gt;
10528 &lt;span class=&quot;go&quot;&gt;- Check amount: 1500&lt;/span&gt;
10529 
10530 &lt;span class=&quot;go&quot;&gt;Payroll for: 3 - Kevin Bacon&lt;/span&gt;
10531 &lt;span class=&quot;go&quot;&gt;- Check amount: 1250&lt;/span&gt;
10532 
10533 &lt;span class=&quot;go&quot;&gt;Payroll for: 4 - Jane Doe&lt;/span&gt;
10534 &lt;span class=&quot;go&quot;&gt;- Check amount: 600&lt;/span&gt;
10535 
10536 &lt;span class=&quot;go&quot;&gt;Payroll for: 5 - Robin Williams&lt;/span&gt;
10537 &lt;span class=&quot;go&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
10538 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;.\program.py&amp;quot;, line 20, in &amp;lt;module&amp;gt;&lt;/span&gt;
10539 &lt;span class=&quot;go&quot;&gt;    payroll_system.calculate_payroll(employees)&lt;/span&gt;
10540 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;hr.py&amp;quot;, line 7, in calculate_payroll&lt;/span&gt;
10541 &lt;span class=&quot;go&quot;&gt;    print(f&amp;#39;- Check amount: {employee.calculate_payroll()}&amp;#39;)&lt;/span&gt;
10542 &lt;span class=&quot;go&quot;&gt;  File &amp;quot;employee.py&amp;quot;, line 12, in calculate_payroll&lt;/span&gt;
10543 &lt;span class=&quot;go&quot;&gt;    return self.weekly_salary&lt;/span&gt;
10544 &lt;span class=&quot;go&quot;&gt;AttributeError: &amp;#39;TemporarySecretary&amp;#39; object has no attribute &amp;#39;weekly_salary&amp;#39;&lt;/span&gt;
10545 &lt;/pre&gt;&lt;/div&gt;
10546 
10547 &lt;p&gt;The problem now is that because you reversed the inheritance order, the MRO is finding the &lt;code&gt;.calculate_payroll()&lt;/code&gt; method of &lt;code&gt;SalariedEmployee&lt;/code&gt; before the one in &lt;code&gt;HourlyEmployee&lt;/code&gt;. You need to override &lt;code&gt;.calculate_payroll()&lt;/code&gt; in &lt;code&gt;TemporarySecretary&lt;/code&gt; and invoke the right implementation from it:&lt;/p&gt;
10548 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TemporarySecretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Secretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HourlyEmployee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10549     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10550         &lt;span class=&quot;n&quot;&gt;HourlyEmployee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10551 
10552     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10553         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HourlyEmployee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10554 &lt;/pre&gt;&lt;/div&gt;
10555 
10556 &lt;p&gt;The &lt;code&gt;calculate_payroll()&lt;/code&gt; method directly invokes  &lt;code&gt;HourlyEmployee.calculate_payroll()&lt;/code&gt; to ensure that you get the correct result. You can run the program again to see it working:&lt;/p&gt;
10557 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python program.py
10558 
10559 &lt;span class=&quot;go&quot;&gt;Tracking Employee Productivity&lt;/span&gt;
10560 &lt;span class=&quot;go&quot;&gt;==============================&lt;/span&gt;
10561 &lt;span class=&quot;go&quot;&gt;Mary Poppins screams and yells for 40 hours.&lt;/span&gt;
10562 &lt;span class=&quot;go&quot;&gt;John Smith expends 40 hours doing office paperwork.&lt;/span&gt;
10563 &lt;span class=&quot;go&quot;&gt;Kevin Bacon expends 40 hours on the phone.&lt;/span&gt;
10564 &lt;span class=&quot;go&quot;&gt;Jane Doe manufactures gadgets for 40 hours.&lt;/span&gt;
10565 &lt;span class=&quot;go&quot;&gt;Robin Williams expends 40 hours doing office paperwork.&lt;/span&gt;
10566 
10567 &lt;span class=&quot;go&quot;&gt;Calculating Payroll&lt;/span&gt;
10568 &lt;span class=&quot;go&quot;&gt;===================&lt;/span&gt;
10569 &lt;span class=&quot;go&quot;&gt;Payroll for: 1 - Mary Poppins&lt;/span&gt;
10570 &lt;span class=&quot;go&quot;&gt;- Check amount: 3000&lt;/span&gt;
10571 
10572 &lt;span class=&quot;go&quot;&gt;Payroll for: 2 - John Smith&lt;/span&gt;
10573 &lt;span class=&quot;go&quot;&gt;- Check amount: 1500&lt;/span&gt;
10574 
10575 &lt;span class=&quot;go&quot;&gt;Payroll for: 3 - Kevin Bacon&lt;/span&gt;
10576 &lt;span class=&quot;go&quot;&gt;- Check amount: 1250&lt;/span&gt;
10577 
10578 &lt;span class=&quot;go&quot;&gt;Payroll for: 4 - Jane Doe&lt;/span&gt;
10579 &lt;span class=&quot;go&quot;&gt;- Check amount: 600&lt;/span&gt;
10580 
10581 &lt;span class=&quot;go&quot;&gt;Payroll for: 5 - Robin Williams&lt;/span&gt;
10582 &lt;span class=&quot;go&quot;&gt;- Check amount: 360&lt;/span&gt;
10583 &lt;/pre&gt;&lt;/div&gt;
10584 
10585 &lt;p&gt;The program now works as expected because you&amp;rsquo;re forcing the method resolution order by explicitly telling the interpreter which method we want to use.&lt;/p&gt;
10586 &lt;p&gt;As you can see, multiple inheritance can be confusing, especially when you run into the &lt;a href=&quot;https://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem&quot;&gt;diamond problem&lt;/a&gt;.&lt;/p&gt;
10587 &lt;p&gt;The following diagram shows the diamond problem in your class hierarchy:&lt;/p&gt;
10588 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/ic-diamond-problem.8e685f12d3c2.jpg&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/ic-diamond-problem.8e685f12d3c2.jpg&quot; width=&quot;726&quot; height=&quot;845&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/ic-diamond-problem.8e685f12d3c2.jpg&amp;amp;w=181&amp;amp;sig=8c590866d2b03d475c750f72405d8fd9e325c350 181w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/ic-diamond-problem.8e685f12d3c2.jpg&amp;amp;w=363&amp;amp;sig=13e5b6884a585b8f452fc384238507c40f9c97b8 363w, https://files.realpython.com/media/ic-diamond-problem.8e685f12d3c2.jpg 726w&quot; sizes=&quot;75vw&quot; alt=&quot;Diamond problem caused by multiple inheritance&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
10589 &lt;p&gt;The diagram shows the diamond problem with the current class design. &lt;code&gt;TemporarySecretary&lt;/code&gt; uses multiple inheritance to derive from two classes that ultimately also derive from &lt;code&gt;Employee&lt;/code&gt;. This causes two paths to reach the &lt;code&gt;Employee&lt;/code&gt; base class, which is something you want to avoid in your designs.&lt;/p&gt;
10590 &lt;p&gt;The diamond problem appears when you&amp;rsquo;re using multiple inheritance and deriving from two classes that have a common base class. This can cause the wrong version of a method to be called. &lt;/p&gt;
10591 &lt;p&gt;As you&amp;rsquo;ve seen, Python provides a way to force the right method to be invoked, and analyzing the MRO can help you understand the problem.&lt;/p&gt;
10592 &lt;p&gt;Still, when you run into the diamond problem, it&amp;rsquo;s better to re-think the design. You will now make some changes to leverage multiple inheritance, avoiding the diamond problem.&lt;/p&gt;
10593 &lt;p&gt;The &lt;code&gt;Employee&lt;/code&gt; derived classes are used by two different systems:&lt;/p&gt;
10594 &lt;ol&gt;
10595 &lt;li&gt;
10596 &lt;p&gt;&lt;strong&gt;The productivity system&lt;/strong&gt; that tracks employee productivity.&lt;/p&gt;
10597 &lt;/li&gt;
10598 &lt;li&gt;
10599 &lt;p&gt;&lt;strong&gt;The payroll system&lt;/strong&gt; that calculates the employee payroll.&lt;/p&gt;
10600 &lt;/li&gt;
10601 &lt;/ol&gt;
10602 &lt;p&gt;This means that everything related to productivity should be together in one module and everything related to payroll should be together in another. You can start making changes to the productivity module:&lt;/p&gt;
10603 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In productivity.py&lt;/span&gt;
10604 
10605 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ProductivitySystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10606     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10607         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Tracking Employee Productivity&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10608         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;==============================&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10609         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10610             &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10611             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{employee.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{result}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10612         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10613 
10614 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ManagerRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10615     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10616         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;screams and yells for &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{hours}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; hours.&amp;#39;&lt;/span&gt;
10617 
10618 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SecretaryRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10619     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10620         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;expends &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{hours}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; hours doing office paperwork.&amp;#39;&lt;/span&gt;
10621 
10622 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SalesRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10623     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10624         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;expends &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{hours}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; hours on the phone.&amp;#39;&lt;/span&gt;
10625 
10626 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FactoryRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10627     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10628         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;manufactures gadgets for &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{hours}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; hours.&amp;#39;&lt;/span&gt;
10629 &lt;/pre&gt;&lt;/div&gt;
10630 
10631 &lt;p&gt;The &lt;code&gt;productivity&lt;/code&gt; module implements the &lt;code&gt;ProductivitySystem&lt;/code&gt; class, as well as the related roles it supports. The classes implement the &lt;code&gt;work()&lt;/code&gt; interface required by the system, but they don&amp;rsquo;t derived from &lt;code&gt;Employee&lt;/code&gt;.&lt;/p&gt;
10632 &lt;p&gt;You can do the same with the &lt;code&gt;hr&lt;/code&gt; module:&lt;/p&gt;
10633 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In hr.py&lt;/span&gt;
10634 
10635 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PayrollSystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10636     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10637         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Calculating Payroll&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10638         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;===================&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10639         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10640             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Payroll for: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{employee.id}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{employee.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10641             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;- Check amount: {employee.calculate_payroll()}&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10642             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10643 
10644 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SalaryPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10645     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10646         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;
10647 
10648     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10649         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;
10650 
10651 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HourlyPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10652     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10653         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt;
10654         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;
10655 
10656     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10657         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;
10658 
10659 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CommissionPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SalaryPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10660     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commission&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10661         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10662         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;commission&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commission&lt;/span&gt;
10663 
10664     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10665         &lt;span class=&quot;n&quot;&gt;fixed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
10666         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fixed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;commission&lt;/span&gt;
10667 &lt;/pre&gt;&lt;/div&gt;
10668 
10669 &lt;p&gt;The &lt;code&gt;hr&lt;/code&gt; module implements the &lt;code&gt;PayrollSystem&lt;/code&gt;, which calculates payroll for the employees. It also implements the policy classes for payroll. As you can see, the policy classes don&amp;rsquo;t derive from &lt;code&gt;Employee&lt;/code&gt; anymore.&lt;/p&gt;
10670 &lt;p&gt;You can now add the necessary classes to the &lt;code&gt;employee&lt;/code&gt; module:&lt;/p&gt;
10671 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In employees.py&lt;/span&gt;
10672 
10673 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hr&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
10674     &lt;span class=&quot;n&quot;&gt;SalaryPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10675     &lt;span class=&quot;n&quot;&gt;CommissionPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10676     &lt;span class=&quot;n&quot;&gt;HourlyPolicy&lt;/span&gt;
10677 &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10678 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;productivity&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
10679     &lt;span class=&quot;n&quot;&gt;ManagerRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10680     &lt;span class=&quot;n&quot;&gt;SecretaryRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10681     &lt;span class=&quot;n&quot;&gt;SalesRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10682     &lt;span class=&quot;n&quot;&gt;FactoryRole&lt;/span&gt;
10683 &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10684 
10685 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10686     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10687         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;
10688         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
10689 
10690 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Manager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ManagerRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SalaryPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10691     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10692         &lt;span class=&quot;n&quot;&gt;SalaryPolicy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10693         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10694 
10695 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Secretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SecretaryRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SalaryPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10696     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10697         &lt;span class=&quot;n&quot;&gt;SalaryPolicy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10698         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10699 
10700 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SalesPerson&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SalesRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CommissionPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10701     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commission&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10702         &lt;span class=&quot;n&quot;&gt;CommissionPolicy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commission&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10703         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10704 
10705 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FactoryWorker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FactoryRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HourlyPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10706     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10707         &lt;span class=&quot;n&quot;&gt;HourlyPolicy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10708         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10709 
10710 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TemporarySecretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SecretaryRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HourlyPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10711     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10712         &lt;span class=&quot;n&quot;&gt;HourlyPolicy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10713         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10714 &lt;/pre&gt;&lt;/div&gt;
10715 
10716 &lt;p&gt;The &lt;code&gt;employees&lt;/code&gt; module imports policies and roles from the other modules and implements the different &lt;code&gt;Employee&lt;/code&gt; types. You are still using multiple inheritance to inherit the implementation of the salary policy classes and the productivity roles, but the implementation of each class only needs to deal with initialization.&lt;/p&gt;
10717 &lt;p&gt;Notice that you still need to explicitly initialize the salary policies in the constructors. You probably saw that the initializations of &lt;code&gt;Manager&lt;/code&gt; and &lt;code&gt;Secretary&lt;/code&gt; are identical. Also, the initializations of &lt;code&gt;FactoryWorker&lt;/code&gt; and &lt;code&gt;TemporarySecretary&lt;/code&gt; are the same.&lt;/p&gt;
10718 &lt;p&gt;You will not want to have this kind of code duplication in more complex designs, so you have to be careful when designing class hierarchies.&lt;/p&gt;
10719 &lt;p&gt;Here&amp;rsquo;s the UML diagram for the new design:&lt;/p&gt;
10720 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/ic-inheritance-policies.0a0de2d42a25.jpg&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/ic-inheritance-policies.0a0de2d42a25.jpg&quot; width=&quot;1221&quot; height=&quot;1030&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/ic-inheritance-policies.0a0de2d42a25.jpg&amp;amp;w=305&amp;amp;sig=b0ddba71545417031c5f11c94ea9b3bd5870e6fb 305w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/ic-inheritance-policies.0a0de2d42a25.jpg&amp;amp;w=610&amp;amp;sig=eb49f48a49342bc70a46148ca0a780d517d69f39 610w, https://files.realpython.com/media/ic-inheritance-policies.0a0de2d42a25.jpg 1221w&quot; sizes=&quot;75vw&quot; alt=&quot;Policy based design using multiple inheritance&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
10721 &lt;p&gt;The diagram shows the relationships to define the &lt;code&gt;Secretary&lt;/code&gt; and &lt;code&gt;TemporarySecretary&lt;/code&gt; using multiple inheritance, but avoiding the diamond problem.&lt;/p&gt;
10722 &lt;p&gt;You can run the program and see how it works:&lt;/p&gt;
10723 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python program.py
10724 
10725 &lt;span class=&quot;go&quot;&gt;Tracking Employee Productivity&lt;/span&gt;
10726 &lt;span class=&quot;go&quot;&gt;==============================&lt;/span&gt;
10727 &lt;span class=&quot;go&quot;&gt;Mary Poppins: screams and yells for 40 hours.&lt;/span&gt;
10728 &lt;span class=&quot;go&quot;&gt;John Smith: expends 40 hours doing office paperwork.&lt;/span&gt;
10729 &lt;span class=&quot;go&quot;&gt;Kevin Bacon: expends 40 hours on the phone.&lt;/span&gt;
10730 &lt;span class=&quot;go&quot;&gt;Jane Doe: manufactures gadgets for 40 hours.&lt;/span&gt;
10731 &lt;span class=&quot;go&quot;&gt;Robin Williams: expends 40 hours doing office paperwork.&lt;/span&gt;
10732 
10733 &lt;span class=&quot;go&quot;&gt;Calculating Payroll&lt;/span&gt;
10734 &lt;span class=&quot;go&quot;&gt;===================&lt;/span&gt;
10735 &lt;span class=&quot;go&quot;&gt;Payroll for: 1 - Mary Poppins&lt;/span&gt;
10736 &lt;span class=&quot;go&quot;&gt;- Check amount: 3000&lt;/span&gt;
10737 
10738 &lt;span class=&quot;go&quot;&gt;Payroll for: 2 - John Smith&lt;/span&gt;
10739 &lt;span class=&quot;go&quot;&gt;- Check amount: 1500&lt;/span&gt;
10740 
10741 &lt;span class=&quot;go&quot;&gt;Payroll for: 3 - Kevin Bacon&lt;/span&gt;
10742 &lt;span class=&quot;go&quot;&gt;- Check amount: 1250&lt;/span&gt;
10743 
10744 &lt;span class=&quot;go&quot;&gt;Payroll for: 4 - Jane Doe&lt;/span&gt;
10745 &lt;span class=&quot;go&quot;&gt;- Check amount: 600&lt;/span&gt;
10746 
10747 &lt;span class=&quot;go&quot;&gt;Payroll for: 5 - Robin Williams&lt;/span&gt;
10748 &lt;span class=&quot;go&quot;&gt;- Check amount: 360&lt;/span&gt;
10749 &lt;/pre&gt;&lt;/div&gt;
10750 
10751 &lt;p&gt;You&amp;rsquo;ve seen how inheritance and multiple inheritance work in Python. You can now explore the topic of composition.&lt;/p&gt;
10752 &lt;h2 id=&quot;composition-in-python&quot;&gt;Composition in Python&lt;/h2&gt;
10753 &lt;p&gt;&lt;strong&gt;Composition&lt;/strong&gt; is an object oriented design concept that models a &lt;strong&gt;has a&lt;/strong&gt; relationship. In composition, a class known as &lt;strong&gt;composite&lt;/strong&gt; contains an object of another class known to as &lt;strong&gt;component&lt;/strong&gt;. In other words, a composite class &lt;strong&gt;has a&lt;/strong&gt; component of another class.&lt;/p&gt;
10754 &lt;p&gt;Composition allows composite classes to reuse the implementation of the components it contains. The composite class doesn&amp;rsquo;t inherit the component class interface, but it can leverage its implementation.&lt;/p&gt;
10755 &lt;p&gt;The composition relation between two classes is considered loosely coupled. That means that changes to the component class rarely affect the composite class, and changes to the composite class never affect the component class.&lt;/p&gt;
10756 &lt;p&gt;This provides better adaptability to change and allows applications to introduce new requirements without affecting existing code.&lt;/p&gt;
10757 &lt;p&gt;When looking at two competing software designs, one based on inheritance and another based on composition, the composition solution usually is the most flexible. You can now look at how composition works.&lt;/p&gt;
10758 &lt;p&gt;You&amp;rsquo;ve already used composition in our examples. If you look at the &lt;code&gt;Employee&lt;/code&gt; class, you&amp;rsquo;ll see that it contains two attributes:&lt;/p&gt;
10759 &lt;ol&gt;
10760 &lt;li&gt;&lt;strong&gt;&lt;code&gt;id&lt;/code&gt;&lt;/strong&gt; to identify an employee.&lt;/li&gt;
10761 &lt;li&gt;&lt;strong&gt;&lt;code&gt;name&lt;/code&gt;&lt;/strong&gt; to contain the name of the employee.&lt;/li&gt;
10762 &lt;/ol&gt;
10763 &lt;p&gt;These two attributes are objects that the &lt;code&gt;Employee&lt;/code&gt; class has. Therefore, you can say that an &lt;code&gt;Employee&lt;/code&gt; &lt;strong&gt;has an&lt;/strong&gt; &lt;code&gt;id&lt;/code&gt; and &lt;strong&gt;has a&lt;/strong&gt; name.&lt;/p&gt;
10764 &lt;p&gt;Another attribute for an &lt;code&gt;Employee&lt;/code&gt; might be an &lt;code&gt;Address&lt;/code&gt;:&lt;/p&gt;
10765 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In contacts.py&lt;/span&gt;
10766 
10767 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10768     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;street&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;city&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zipcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;street2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10769         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;street&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;street&lt;/span&gt;
10770         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;street2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;street2&lt;/span&gt;
10771         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;city&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;city&lt;/span&gt;
10772         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;
10773         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zipcode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zipcode&lt;/span&gt;
10774 
10775     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__str__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10776         &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;street&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
10777         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;street2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10778             &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;street2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10779         &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.city}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.state}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.zipcode}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10780         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10781 &lt;/pre&gt;&lt;/div&gt;
10782 
10783 &lt;p&gt;You implemented a basic address class that contains the usual components for an address. You made the &lt;code&gt;street2&lt;/code&gt; attribute optional because not all addresses will have that component.&lt;/p&gt;
10784 &lt;p&gt;You implemented &lt;code&gt;__str__()&lt;/code&gt; to provide a pretty representation of an &lt;code&gt;Address&lt;/code&gt;. You can see this implementation in the interactive interpreter:&lt;/p&gt;
10785 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;contacts&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Address&lt;/span&gt;
10786 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;55 Main St.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Concord&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;NH&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;03301&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10787 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10788 
10789 &lt;span class=&quot;go&quot;&gt;55 Main St.&lt;/span&gt;
10790 &lt;span class=&quot;go&quot;&gt;Concord, NH 03301&lt;/span&gt;
10791 &lt;/pre&gt;&lt;/div&gt;
10792 
10793 &lt;p&gt;When you &lt;code&gt;print()&lt;/code&gt; the &lt;code&gt;address&lt;/code&gt; variable, the special method &lt;code&gt;__str__()&lt;/code&gt; is invoked. Since you overloaded the method to return a string formatted as an address, you get a nice, readable representation. &lt;a href=&quot;https://realpython.com/operator-function-overloading/&quot;&gt;Operator and Function Overloading in Custom Python Classes&lt;/a&gt; gives a good overview of the special methods available in classes that can be implemented to customize the behavior of your objects.&lt;/p&gt;
10794 &lt;p&gt;You can now add the &lt;code&gt;Address&lt;/code&gt; to the &lt;code&gt;Employee&lt;/code&gt; class through composition:&lt;/p&gt;
10795 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In employees.py&lt;/span&gt;
10796 
10797 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10798     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10799         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;
10800         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
10801         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;
10802 &lt;/pre&gt;&lt;/div&gt;
10803 
10804 &lt;p&gt;You initialize the &lt;code&gt;address&lt;/code&gt; attribute to &lt;code&gt;None&lt;/code&gt; for now to make it optional, but by doing that, you can now assign an &lt;code&gt;Address&lt;/code&gt; to an &lt;code&gt;Employee&lt;/code&gt;. Also notice that there is no reference in the &lt;code&gt;employee&lt;/code&gt; module to the &lt;code&gt;contacts&lt;/code&gt; module.&lt;/p&gt;
10805 &lt;p&gt;Composition is a loosely coupled relationship that often doesn&amp;rsquo;t require the composite class to have knowledge of the component.&lt;/p&gt;
10806 &lt;p&gt;The UML diagram representing the relationship between &lt;code&gt;Employee&lt;/code&gt; and &lt;code&gt;Address&lt;/code&gt; looks like this:&lt;/p&gt;
10807 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/ic-employee-address.240e806b1101.jpg&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-33&quot; src=&quot;https://files.realpython.com/media/ic-employee-address.240e806b1101.jpg&quot; width=&quot;284&quot; height=&quot;469&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/ic-employee-address.240e806b1101.jpg&amp;amp;w=71&amp;amp;sig=871e9fce940fb84544980c474624a07c5d52b8e8 71w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/ic-employee-address.240e806b1101.jpg&amp;amp;w=142&amp;amp;sig=658578725c830eb5554ba1e349b27a198c2aa7e9 142w, https://files.realpython.com/media/ic-employee-address.240e806b1101.jpg 284w&quot; sizes=&quot;75vw&quot; alt=&quot;Composition example with Employee containing Address&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
10808 &lt;p&gt;The diagram shows the basic composition relationship between &lt;code&gt;Employee&lt;/code&gt; and &lt;code&gt;Address&lt;/code&gt;.&lt;/p&gt;
10809 &lt;p&gt;You can now modify the &lt;code&gt;PayrollSystem&lt;/code&gt; class to leverage the &lt;code&gt;address&lt;/code&gt; attribute in &lt;code&gt;Employee&lt;/code&gt;:&lt;/p&gt;
10810 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In hr.py&lt;/span&gt;
10811 
10812 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PayrollSystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10813     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10814         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Calculating Payroll&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10815         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;===================&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10816         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10817             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Payroll for: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{employee.id}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{employee.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10818             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;- Check amount: {employee.calculate_payroll()}&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10819 &lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10820 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;                &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;- Sent to:&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10821 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;                &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10822 &lt;/span&gt;            &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10823 &lt;/pre&gt;&lt;/div&gt;
10824 
10825 &lt;p&gt;You check to see if the &lt;code&gt;employee&lt;/code&gt; object has an address, and if it does, you print it. You can now modify the program to assign some addresses to the employees:&lt;/p&gt;
10826 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In program.py&lt;/span&gt;
10827 
10828 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hr&lt;/span&gt;
10829 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;employees&lt;/span&gt;
10830 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;productivity&lt;/span&gt;
10831 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;contacts&lt;/span&gt;
10832 
10833 &lt;span class=&quot;n&quot;&gt;manager&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Manager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Mary Poppins&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10834 &lt;span class=&quot;n&quot;&gt;manager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;contacts&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
10835     &lt;span class=&quot;s1&quot;&gt;&amp;#39;121 Admin Rd&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
10836     &lt;span class=&quot;s1&quot;&gt;&amp;#39;Concord&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
10837     &lt;span class=&quot;s1&quot;&gt;&amp;#39;NH&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
10838     &lt;span class=&quot;s1&quot;&gt;&amp;#39;03301&amp;#39;&lt;/span&gt;
10839 &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10840 &lt;span class=&quot;n&quot;&gt;secretary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Secretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;John Smith&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10841 &lt;span class=&quot;n&quot;&gt;secretary&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;contacts&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
10842     &lt;span class=&quot;s1&quot;&gt;&amp;#39;67 Paperwork Ave.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
10843     &lt;span class=&quot;s1&quot;&gt;&amp;#39;Manchester&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
10844     &lt;span class=&quot;s1&quot;&gt;&amp;#39;NH&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
10845     &lt;span class=&quot;s1&quot;&gt;&amp;#39;03101&amp;#39;&lt;/span&gt;
10846 &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10847 &lt;span class=&quot;n&quot;&gt;sales_guy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SalesPerson&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Kevin Bacon&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10848 &lt;span class=&quot;n&quot;&gt;factory_worker&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FactoryWorker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Jane Doe&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10849 &lt;span class=&quot;n&quot;&gt;temporary_secretary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TemporarySecretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Robin Williams&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10850 &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
10851     &lt;span class=&quot;n&quot;&gt;manager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10852     &lt;span class=&quot;n&quot;&gt;secretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10853     &lt;span class=&quot;n&quot;&gt;sales_guy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10854     &lt;span class=&quot;n&quot;&gt;factory_worker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10855     &lt;span class=&quot;n&quot;&gt;temporary_secretary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10856 &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
10857 &lt;span class=&quot;n&quot;&gt;productivity_system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;productivity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ProductivitySystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
10858 &lt;span class=&quot;n&quot;&gt;productivity_system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10859 &lt;span class=&quot;n&quot;&gt;payroll_system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PayrollSystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
10860 &lt;span class=&quot;n&quot;&gt;payroll_system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10861 &lt;/pre&gt;&lt;/div&gt;
10862 
10863 &lt;p&gt;You added a couple of addresses to the &lt;code&gt;manager&lt;/code&gt; and &lt;code&gt;secretary&lt;/code&gt; objects. When you run the program, you will see the addresses printed:&lt;/p&gt;
10864 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python program.py
10865 
10866 &lt;span class=&quot;go&quot;&gt;Tracking Employee Productivity&lt;/span&gt;
10867 &lt;span class=&quot;go&quot;&gt;==============================&lt;/span&gt;
10868 &lt;span class=&quot;go&quot;&gt;Mary Poppins: screams and yells for {hours} hours.&lt;/span&gt;
10869 &lt;span class=&quot;go&quot;&gt;John Smith: expends {hours} hours doing office paperwork.&lt;/span&gt;
10870 &lt;span class=&quot;go&quot;&gt;Kevin Bacon: expends {hours} hours on the phone.&lt;/span&gt;
10871 &lt;span class=&quot;go&quot;&gt;Jane Doe: manufactures gadgets for {hours} hours.&lt;/span&gt;
10872 &lt;span class=&quot;go&quot;&gt;Robin Williams: expends {hours} hours doing office paperwork.&lt;/span&gt;
10873 
10874 &lt;span class=&quot;go&quot;&gt;Calculating Payroll&lt;/span&gt;
10875 &lt;span class=&quot;go&quot;&gt;===================&lt;/span&gt;
10876 &lt;span class=&quot;go&quot;&gt;Payroll for: 1 - Mary Poppins&lt;/span&gt;
10877 &lt;span class=&quot;go&quot;&gt;- Check amount: 3000&lt;/span&gt;
10878 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
10879 &lt;span class=&quot;go&quot;&gt;121 Admin Rd&lt;/span&gt;
10880 &lt;span class=&quot;go&quot;&gt;Concord, NH 03301&lt;/span&gt;
10881 
10882 &lt;span class=&quot;go&quot;&gt;Payroll for: 2 - John Smith&lt;/span&gt;
10883 &lt;span class=&quot;go&quot;&gt;- Check amount: 1500&lt;/span&gt;
10884 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
10885 &lt;span class=&quot;go&quot;&gt;67 Paperwork Ave.&lt;/span&gt;
10886 &lt;span class=&quot;go&quot;&gt;Manchester, NH 03101&lt;/span&gt;
10887 
10888 &lt;span class=&quot;go&quot;&gt;Payroll for: 3 - Kevin Bacon&lt;/span&gt;
10889 &lt;span class=&quot;go&quot;&gt;- Check amount: 1250&lt;/span&gt;
10890 
10891 &lt;span class=&quot;go&quot;&gt;Payroll for: 4 - Jane Doe&lt;/span&gt;
10892 &lt;span class=&quot;go&quot;&gt;- Check amount: 600&lt;/span&gt;
10893 
10894 &lt;span class=&quot;go&quot;&gt;Payroll for: 5 - Robin Williams&lt;/span&gt;
10895 &lt;span class=&quot;go&quot;&gt;- Check amount: 360&lt;/span&gt;
10896 &lt;/pre&gt;&lt;/div&gt;
10897 
10898 &lt;p&gt;Notice how the payroll output for the &lt;code&gt;manager&lt;/code&gt; and &lt;code&gt;secretary&lt;/code&gt; objects show the addresses where the checks were sent.&lt;/p&gt;
10899 &lt;p&gt;The &lt;code&gt;Employee&lt;/code&gt; class leverages the implementation of the &lt;code&gt;Address&lt;/code&gt; class without any knowledge of what an &lt;code&gt;Address&lt;/code&gt; object is or how it&amp;rsquo;s represented. This type of design is so flexible that you can change the &lt;code&gt;Address&lt;/code&gt; class without any impact to the &lt;code&gt;Employee&lt;/code&gt; class.&lt;/p&gt;
10900 &lt;h3 id=&quot;flexible-designs-with-composition&quot;&gt;Flexible Designs With Composition&lt;/h3&gt;
10901 &lt;p&gt;Composition is more flexible than inheritance because it models a loosely coupled relationship. Changes to a component class have minimal or no effects on the composite class. Designs based on composition are more suitable to change.&lt;/p&gt;
10902 &lt;p&gt;You change behavior by providing new components that implement those behaviors instead of adding new classes to your hierarchy.&lt;/p&gt;
10903 &lt;p&gt;Take a look at the multiple inheritance example above. Imagine how new payroll policies will affect the design. Try to picture what the class hierarchy will look like if new roles are needed. As you saw before, relying too heavily on inheritance can lead to class explosion.&lt;/p&gt;
10904 &lt;p&gt;The biggest problem is not so much the number of classes in your design, but how tightly coupled the relationships between those classes are. Tightly coupled classes affect each other when changes are introduced.&lt;/p&gt;
10905 &lt;p&gt;In this section, you are going to use composition to implement a better design that still fits the requirements of the &lt;code&gt;PayrollSystem&lt;/code&gt; and the &lt;code&gt;ProductivitySystem&lt;/code&gt;.&lt;/p&gt;
10906 &lt;p&gt;You can start by implementing the functionality of the &lt;code&gt;ProductivitySystem&lt;/code&gt;:&lt;/p&gt;
10907 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In productivity.py&lt;/span&gt;
10908 
10909 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ProductivitySystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10910     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10911         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_roles&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
10912             &lt;span class=&quot;s1&quot;&gt;&amp;#39;manager&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ManagerRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10913             &lt;span class=&quot;s1&quot;&gt;&amp;#39;secretary&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SecretaryRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10914             &lt;span class=&quot;s1&quot;&gt;&amp;#39;sales&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SalesRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10915             &lt;span class=&quot;s1&quot;&gt;&amp;#39;factory&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FactoryRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
10916         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
10917 
10918     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_role&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;role_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10919         &lt;span class=&quot;n&quot;&gt;role_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_roles&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;role_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10920         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;role_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10921             &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;role_id&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10922         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;role_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
10923 
10924     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10925         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Tracking Employee Productivity&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10926         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;==============================&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10927         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10928             &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10929         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10930 &lt;/pre&gt;&lt;/div&gt;
10931 
10932 &lt;p&gt;The &lt;code&gt;ProductivitySystem&lt;/code&gt; class defines some roles using a string identifier mapped to a role class that implements the role. It exposes a &lt;code&gt;.get_role()&lt;/code&gt; method that, given a role identifier, returns the role type object. If the role is not found, then a &lt;code&gt;ValueError&lt;/code&gt; exception is raised.&lt;/p&gt;
10933 &lt;p&gt;It also exposes the previous functionality in the &lt;code&gt;.track()&lt;/code&gt; method, where given a list of employees it tracks the productivity of those employees.&lt;/p&gt;
10934 &lt;p&gt;You can now implement the different role classes:&lt;/p&gt;
10935 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In productivity.py&lt;/span&gt;
10936 
10937 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ManagerRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10938     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;perform_duties&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10939         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;screams and yells for &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{hours}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; hours.&amp;#39;&lt;/span&gt;
10940 
10941 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SecretaryRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10942     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;perform_duties&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10943         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;does paperwork for &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{hours}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; hours.&amp;#39;&lt;/span&gt;
10944 
10945 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SalesRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10946     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;perform_duties&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10947         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;expends &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{hours}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; hours on the phone.&amp;#39;&lt;/span&gt;
10948 
10949 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FactoryRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10950     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;perform_duties&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10951         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;manufactures gadgets for &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{hours}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; hours.&amp;#39;&lt;/span&gt;
10952 &lt;/pre&gt;&lt;/div&gt;
10953 
10954 &lt;p&gt;Each of the roles you implemented expose a &lt;code&gt;.perform_duties()&lt;/code&gt; that takes the number of &lt;code&gt;hours&lt;/code&gt; worked. The methods return a string representing the duties.&lt;/p&gt;
10955 &lt;p&gt;The role classes are independent of each other, but they expose the same interface, so they are interchangeable. You&amp;rsquo;ll see later how they are used in the application.&lt;/p&gt;
10956 &lt;p&gt;Now, you can implement the &lt;code&gt;PayrollSystem&lt;/code&gt; for the application:&lt;/p&gt;
10957 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In hr.py&lt;/span&gt;
10958 
10959 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PayrollSystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10960     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10961         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_employee_policies&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
10962             &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SalaryPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
10963             &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SalaryPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
10964             &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CommissionPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
10965             &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HourlyPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
10966             &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HourlyPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10967         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
10968 
10969     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10970         &lt;span class=&quot;n&quot;&gt;policy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_employee_policies&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10971         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10972             &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10973         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;policy&lt;/span&gt;
10974 
10975     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10976         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Calculating Payroll&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10977         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;===================&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10978         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10979             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Payroll for: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{employee.id}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{employee.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10980             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;- Check amount: {employee.calculate_payroll()}&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10981             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10982                 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;- Sent to:&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10983                 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10984             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
10985 &lt;/pre&gt;&lt;/div&gt;
10986 
10987 &lt;p&gt;The &lt;code&gt;PayrollSystem&lt;/code&gt; keeps an internal database of payroll policies for each employee. It exposes a &lt;code&gt;.get_policy()&lt;/code&gt; that, given an employee &lt;code&gt;id&lt;/code&gt;, returns its payroll policy. If a specified &lt;code&gt;id&lt;/code&gt; doesn&amp;rsquo;t exist in the system, then the method raises a &lt;code&gt;ValueError&lt;/code&gt; exception.&lt;/p&gt;
10988 &lt;p&gt;The implementation of &lt;code&gt;.calculate_payroll()&lt;/code&gt; works the same as before. It takes a list of employees, calculates the payroll, and prints the results.&lt;/p&gt;
10989 &lt;p&gt;You can now implement the payroll policy classes:&lt;/p&gt;
10990 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In hr.py&lt;/span&gt;
10991 
10992 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PayrollPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
10993     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10994         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
10995 
10996     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;track_work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
10997         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;
10998 
10999 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SalaryPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PayrollPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11000     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11001         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11002         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;
11003 
11004     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11005         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;
11006 
11007 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HourlyPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PayrollPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11008     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11009         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11010         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;
11011 
11012     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11013         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hour_rate&lt;/span&gt;
11014 
11015 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CommissionPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SalaryPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11016     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commission_per_sale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11017         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weekly_salary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11018         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;commission_per_sale&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;commission_per_sale&lt;/span&gt;
11019 
11020     &lt;span class=&quot;nd&quot;&gt;@property&lt;/span&gt;
11021     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;commission&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11022         &lt;span class=&quot;n&quot;&gt;sales&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours_worked&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;
11023         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sales&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;commission_per_sale&lt;/span&gt;
11024 
11025     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11026         &lt;span class=&quot;n&quot;&gt;fixed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11027         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fixed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;commission&lt;/span&gt;
11028 &lt;/pre&gt;&lt;/div&gt;
11029 
11030 &lt;p&gt;You first implement a &lt;code&gt;PayrollPolicy&lt;/code&gt; class that serves as a base class for all the payroll policies. This class tracks the &lt;code&gt;hours_worked&lt;/code&gt;, which is common to all payroll policies.&lt;/p&gt;
11031 &lt;p&gt;The other policy classes derive from &lt;code&gt;PayrollPolicy&lt;/code&gt;. We use inheritance here because we want to leverage the implementation of &lt;code&gt;PayrollPolicy&lt;/code&gt;. Also, &lt;code&gt;SalaryPolicy&lt;/code&gt;, &lt;code&gt;HourlyPolicy&lt;/code&gt;, and &lt;code&gt;CommissionPolicy&lt;/code&gt; &lt;strong&gt;are a&lt;/strong&gt; &lt;code&gt;PayrollPolicy&lt;/code&gt;.&lt;/p&gt;
11032 &lt;p&gt;&lt;code&gt;SalaryPolicy&lt;/code&gt; is initialized with a &lt;code&gt;weekly_salary&lt;/code&gt; value that is then used in &lt;code&gt;.calculate_payroll()&lt;/code&gt;. &lt;code&gt;HourlyPolicy&lt;/code&gt; is initialized with the &lt;code&gt;hour_rate&lt;/code&gt;, and implements &lt;code&gt;.calculate_payroll()&lt;/code&gt; by leveraging the base class &lt;code&gt;hours_worked&lt;/code&gt;.&lt;/p&gt;
11033 &lt;p&gt;The &lt;code&gt;CommissionPolicy&lt;/code&gt; class derives from &lt;code&gt;SalaryPolicy&lt;/code&gt; because it wants to inherit its implementation. It is initialized with the &lt;code&gt;weekly_salary&lt;/code&gt; parameters, but it also requires a &lt;code&gt;commission_per_sale&lt;/code&gt; parameter.&lt;/p&gt;
11034 &lt;p&gt;The &lt;code&gt;commission_per_sale&lt;/code&gt; is used to calculate the &lt;code&gt;.commission&lt;/code&gt;, which is implemented as a property so it gets calculated when requested. In the example, we are assuming that a sale happens every 5 hours worked, and the &lt;code&gt;.commission&lt;/code&gt; is the number of sales times the &lt;code&gt;commission_per_sale&lt;/code&gt; value.&lt;/p&gt;
11035 &lt;p&gt;&lt;code&gt;CommissionPolicy&lt;/code&gt; implements the &lt;code&gt;.calculate_payroll()&lt;/code&gt; method by first leveraging the implementation in &lt;code&gt;SalaryPolicy&lt;/code&gt; and then adding the calculated commission.&lt;/p&gt;
11036 &lt;p&gt;You can now add an &lt;code&gt;AddressBook&lt;/code&gt; class to manage employee addresses:&lt;/p&gt;
11037 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In contacts.py&lt;/span&gt;
11038 
11039 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AddressBook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11040     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11041         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_employee_addresses&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
11042             &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;121 Admin Rd.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Concord&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;NH&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;03301&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
11043             &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;67 Paperwork Ave&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Manchester&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;NH&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;03101&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
11044             &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;15 Rose St&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Concord&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;NH&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;03301&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Apt. B-1&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
11045             &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;39 Sole St.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Concord&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;NH&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;03301&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
11046             &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;99 Mountain Rd.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Concord&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;NH&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;03301&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
11047         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
11048 
11049     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_employee_address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11050         &lt;span class=&quot;n&quot;&gt;address&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_employee_addresses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11051         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11052             &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11053         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;
11054 &lt;/pre&gt;&lt;/div&gt;
11055 
11056 &lt;p&gt;The &lt;code&gt;AddressBook&lt;/code&gt; class keeps an internal database of &lt;code&gt;Address&lt;/code&gt; objects for each employee. It exposes a &lt;code&gt;get_employee_address()&lt;/code&gt; method that returns the address of the specified employee &lt;code&gt;id&lt;/code&gt;. If the employee &lt;code&gt;id&lt;/code&gt; doesn&amp;rsquo;t exist, then it raises a &lt;code&gt;ValueError&lt;/code&gt;.&lt;/p&gt;
11057 &lt;p&gt;The &lt;code&gt;Address&lt;/code&gt; class implementation remains the same as before:&lt;/p&gt;
11058 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In contacts.py&lt;/span&gt;
11059 
11060 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11061     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;street&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;city&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zipcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;street2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11062         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;street&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;street&lt;/span&gt;
11063         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;street2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;street2&lt;/span&gt;
11064         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;city&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;city&lt;/span&gt;
11065         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;
11066         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zipcode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zipcode&lt;/span&gt;
11067 
11068     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__str__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11069         &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;street&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
11070         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;street2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11071             &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;street2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11072         &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.city}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.state}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.zipcode}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11073         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11074 &lt;/pre&gt;&lt;/div&gt;
11075 
11076 &lt;p&gt;The class manages the address components and provides a pretty representation of an address.&lt;/p&gt;
11077 &lt;p&gt;So far, the new classes have been extended to support more functionality, but there are no significant changes to the previous design. This is going to change with the design of the &lt;code&gt;employees&lt;/code&gt; module and its classes.&lt;/p&gt;
11078 &lt;p&gt;You can start by implementing an &lt;code&gt;EmployeeDatabase&lt;/code&gt; class:&lt;/p&gt;
11079 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In employees.py&lt;/span&gt;
11080 
11081 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;productivity&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ProductivitySystem&lt;/span&gt;
11082 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hr&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PayrollSystem&lt;/span&gt;
11083 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;contacts&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AddressBook&lt;/span&gt;
11084 
11085 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;EmployeeDatabase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11086     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11087         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_employees&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
11088             &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
11089                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;id&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
11090                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Mary Poppins&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
11091                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;role&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;manager&amp;#39;&lt;/span&gt;
11092             &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
11093             &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
11094                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;id&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
11095                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;John Smith&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
11096                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;role&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;secretary&amp;#39;&lt;/span&gt;
11097             &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
11098             &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
11099                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;id&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
11100                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Kevin Bacon&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
11101                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;role&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;sales&amp;#39;&lt;/span&gt;
11102             &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
11103             &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
11104                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;id&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
11105                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Jane Doe&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
11106                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;role&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;factory&amp;#39;&lt;/span&gt;
11107             &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
11108             &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
11109                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;id&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
11110                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Robin Williams&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
11111                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;role&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;secretary&amp;#39;&lt;/span&gt;
11112             &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
11113         &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
11114         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;productivity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ProductivitySystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11115         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payroll&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PayrollSystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11116         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee_addresses&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AddressBook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11117 
11118     &lt;span class=&quot;nd&quot;&gt;@property&lt;/span&gt;
11119     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11120         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_create_employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
11121 
11122     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_create_employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;role&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11123         &lt;span class=&quot;n&quot;&gt;address&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee_addresses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_employee_address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11124         &lt;span class=&quot;n&quot;&gt;employee_role&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;productivity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_role&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;role&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11125         &lt;span class=&quot;n&quot;&gt;payroll_policy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payroll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11126         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee_role&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payroll_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11127 &lt;/pre&gt;&lt;/div&gt;
11128 
11129 &lt;p&gt;The &lt;code&gt;EmployeeDatabase&lt;/code&gt; keeps track of all the employees in the company. For each employee, it tracks the &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, and &lt;code&gt;role&lt;/code&gt;. It &lt;strong&gt;has an&lt;/strong&gt; instance of the &lt;code&gt;ProductivitySystem&lt;/code&gt;, the &lt;code&gt;PayrollSystem&lt;/code&gt;, and the &lt;code&gt;AddressBook&lt;/code&gt;. These instances are used to create employees.&lt;/p&gt;
11130 &lt;p&gt;It exposes an &lt;code&gt;.employees&lt;/code&gt; property that returns the list of employees. The &lt;code&gt;Employee&lt;/code&gt; objects are created in an internal method &lt;code&gt;._create_employee()&lt;/code&gt;. Notice that you don&amp;rsquo;t have different types of &lt;code&gt;Employee&lt;/code&gt; classes. You just need to implement a single &lt;code&gt;Employee&lt;/code&gt; class:&lt;/p&gt;
11131 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In employees.py&lt;/span&gt;
11132 
11133 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11134     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;role&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11135         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;
11136         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
11137         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;
11138         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;role&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;role&lt;/span&gt;
11139         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payroll&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payroll&lt;/span&gt;
11140 
11141     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11142         &lt;span class=&quot;n&quot;&gt;duties&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;role&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;perform_duties&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11143         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Employee &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.id}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;:&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11144         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;- &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{duties}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11145         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11146         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payroll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;track_work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11147 
11148     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11149         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payroll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11150 &lt;/pre&gt;&lt;/div&gt;
11151 
11152 &lt;p&gt;The &lt;code&gt;Employee&lt;/code&gt; class is initialized with the &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, and &lt;code&gt;address&lt;/code&gt; attributes. It also requires the productivity &lt;code&gt;role&lt;/code&gt; for the employee and the &lt;code&gt;payroll&lt;/code&gt; policy.&lt;/p&gt;
11153 &lt;p&gt;The class exposes a &lt;code&gt;.work()&lt;/code&gt; method that takes the hours worked. This method first retrieves the &lt;code&gt;duties&lt;/code&gt; from the &lt;code&gt;role&lt;/code&gt;. In other words, it delegates to the &lt;code&gt;role&lt;/code&gt; object to perform its duties.&lt;/p&gt;
11154 &lt;p&gt;In the same way, it delegates to the &lt;code&gt;payroll&lt;/code&gt; object to track the work &lt;code&gt;hours&lt;/code&gt;. The &lt;code&gt;payroll&lt;/code&gt;, as you saw, uses those hours to calculate the payroll if needed.&lt;/p&gt;
11155 &lt;p&gt;The following diagram shows the composition design used:&lt;/p&gt;
11156 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/ic-policy-based-composition.6e78bdb5824f.jpg&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/ic-policy-based-composition.6e78bdb5824f.jpg&quot; width=&quot;1219&quot; height=&quot;1381&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/ic-policy-based-composition.6e78bdb5824f.jpg&amp;amp;w=304&amp;amp;sig=8baa24bb7a519fc76f40f994c95c05833c8ded67 304w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/ic-policy-based-composition.6e78bdb5824f.jpg&amp;amp;w=609&amp;amp;sig=2a14268f94d82ac5f5c64d115f02f8eeea808580 609w, https://files.realpython.com/media/ic-policy-based-composition.6e78bdb5824f.jpg 1219w&quot; sizes=&quot;75vw&quot; alt=&quot;Policy based design using composition&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
11157 &lt;p&gt;The diagram shows the design of composition based policies. There is a single &lt;code&gt;Employee&lt;/code&gt; that is composed of other data objects like &lt;code&gt;Address&lt;/code&gt; and depends on the &lt;code&gt;IRole&lt;/code&gt; and &lt;code&gt;IPayrollCalculator&lt;/code&gt; interfaces to delegate the work. There are multiple implementations of these interfaces.&lt;/p&gt;
11158 &lt;p&gt;You can now use this design in your program:&lt;/p&gt;
11159 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In program.py&lt;/span&gt;
11160 
11161 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hr&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PayrollSystem&lt;/span&gt;
11162 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;productivity&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ProductivitySystem&lt;/span&gt;
11163 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;employees&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EmployeeDatabase&lt;/span&gt;
11164 
11165 &lt;span class=&quot;n&quot;&gt;productivity_system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ProductivitySystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11166 &lt;span class=&quot;n&quot;&gt;payroll_system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PayrollSystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11167 &lt;span class=&quot;n&quot;&gt;employee_database&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EmployeeDatabase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11168 &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee_database&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;
11169 &lt;span class=&quot;n&quot;&gt;productivity_system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11170 &lt;span class=&quot;n&quot;&gt;payroll_system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11171 &lt;/pre&gt;&lt;/div&gt;
11172 
11173 &lt;p&gt;You can run the program to see its output:&lt;/p&gt;
11174 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python program.py
11175 
11176 &lt;span class=&quot;go&quot;&gt;Tracking Employee Productivity&lt;/span&gt;
11177 &lt;span class=&quot;go&quot;&gt;==============================&lt;/span&gt;
11178 &lt;span class=&quot;go&quot;&gt;Employee 1 - Mary Poppins:&lt;/span&gt;
11179 &lt;span class=&quot;go&quot;&gt;- screams and yells for 40 hours.&lt;/span&gt;
11180 
11181 &lt;span class=&quot;go&quot;&gt;Employee 2 - John Smith:&lt;/span&gt;
11182 &lt;span class=&quot;go&quot;&gt;- does paperwork for 40 hours.&lt;/span&gt;
11183 
11184 &lt;span class=&quot;go&quot;&gt;Employee 3 - Kevin Bacon:&lt;/span&gt;
11185 &lt;span class=&quot;go&quot;&gt;- expends 40 hours on the phone.&lt;/span&gt;
11186 
11187 &lt;span class=&quot;go&quot;&gt;Employee 4 - Jane Doe:&lt;/span&gt;
11188 &lt;span class=&quot;go&quot;&gt;- manufactures gadgets for 40 hours.&lt;/span&gt;
11189 
11190 &lt;span class=&quot;go&quot;&gt;Employee 5 - Robin Williams:&lt;/span&gt;
11191 &lt;span class=&quot;go&quot;&gt;- does paperwork for 40 hours.&lt;/span&gt;
11192 
11193 
11194 &lt;span class=&quot;go&quot;&gt;Calculating Payroll&lt;/span&gt;
11195 &lt;span class=&quot;go&quot;&gt;===================&lt;/span&gt;
11196 &lt;span class=&quot;go&quot;&gt;Payroll for: 1 - Mary Poppins&lt;/span&gt;
11197 &lt;span class=&quot;go&quot;&gt;- Check amount: 3000&lt;/span&gt;
11198 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
11199 &lt;span class=&quot;go&quot;&gt;121 Admin Rd.&lt;/span&gt;
11200 &lt;span class=&quot;go&quot;&gt;Concord, NH 03301&lt;/span&gt;
11201 
11202 &lt;span class=&quot;go&quot;&gt;Payroll for: 2 - John Smith&lt;/span&gt;
11203 &lt;span class=&quot;go&quot;&gt;- Check amount: 1500&lt;/span&gt;
11204 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
11205 &lt;span class=&quot;go&quot;&gt;67 Paperwork Ave&lt;/span&gt;
11206 &lt;span class=&quot;go&quot;&gt;Manchester, NH 03101&lt;/span&gt;
11207 
11208 &lt;span class=&quot;go&quot;&gt;Payroll for: 3 - Kevin Bacon&lt;/span&gt;
11209 &lt;span class=&quot;go&quot;&gt;- Check amount: 1800.0&lt;/span&gt;
11210 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
11211 &lt;span class=&quot;go&quot;&gt;15 Rose St&lt;/span&gt;
11212 &lt;span class=&quot;go&quot;&gt;Apt. B-1&lt;/span&gt;
11213 &lt;span class=&quot;go&quot;&gt;Concord, NH 03301&lt;/span&gt;
11214 
11215 &lt;span class=&quot;go&quot;&gt;Payroll for: 4 - Jane Doe&lt;/span&gt;
11216 &lt;span class=&quot;go&quot;&gt;- Check amount: 600&lt;/span&gt;
11217 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
11218 &lt;span class=&quot;go&quot;&gt;39 Sole St.&lt;/span&gt;
11219 &lt;span class=&quot;go&quot;&gt;Concord, NH 03301&lt;/span&gt;
11220 
11221 &lt;span class=&quot;go&quot;&gt;Payroll for: 5 - Robin Williams&lt;/span&gt;
11222 &lt;span class=&quot;go&quot;&gt;- Check amount: 360&lt;/span&gt;
11223 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
11224 &lt;span class=&quot;go&quot;&gt;99 Mountain Rd.&lt;/span&gt;
11225 &lt;span class=&quot;go&quot;&gt;Concord, NH 03301&lt;/span&gt;
11226 &lt;/pre&gt;&lt;/div&gt;
11227 
11228 &lt;p&gt;This design is what is called &lt;a href=&quot;https://en.wikipedia.org/wiki/Modern_C%2B%2B_Design#Policy-based_design&quot;&gt;policy-based design&lt;/a&gt;, where classes are composed of policies, and they delegate to those policies to do the work.&lt;/p&gt;
11229 &lt;p&gt;Policy-based design was introduced in the book &lt;a href=&quot;https://realpython.com/asins/B00AU3JUHG&quot;&gt;Modern C++ Design&lt;/a&gt;, and it uses template metaprogramming in C++ to achieve the results.&lt;/p&gt;
11230 &lt;p&gt;Python does not support templates, but you can achieve similar results using composition, as you saw in the example above.&lt;/p&gt;
11231 &lt;p&gt;This type of design gives you all the flexibility you&amp;rsquo;ll need as requirements change. Imagine you need to change the way payroll is calculated for an object at run-time.&lt;/p&gt;
11232 &lt;h3 id=&quot;customizing-behavior-with-composition&quot;&gt;Customizing Behavior With Composition&lt;/h3&gt;
11233 &lt;p&gt;If your design relies on inheritance, you need to find a way to change the type of an object to change its behavior. With composition, you just need to change the policy the object uses.&lt;/p&gt;
11234 &lt;p&gt;Imagine that our &lt;code&gt;manager&lt;/code&gt; all of a sudden becomes a temporary employee that gets paid by the hour. You can modify the object during the execution of the program in the following way:&lt;/p&gt;
11235 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In program.py&lt;/span&gt;
11236 
11237 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hr&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PayrollSystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HourlyPolicy&lt;/span&gt;
11238 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;productivity&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ProductivitySystem&lt;/span&gt;
11239 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;employees&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EmployeeDatabase&lt;/span&gt;
11240 
11241 &lt;span class=&quot;n&quot;&gt;productivity_system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ProductivitySystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11242 &lt;span class=&quot;n&quot;&gt;payroll_system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PayrollSystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11243 &lt;span class=&quot;n&quot;&gt;employee_database&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EmployeeDatabase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11244 &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee_database&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;
11245 &lt;span class=&quot;n&quot;&gt;manager&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
11246 &lt;span class=&quot;n&quot;&gt;manager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;payroll&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HourlyPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;55&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11247 
11248 &lt;span class=&quot;n&quot;&gt;productivity_system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11249 &lt;span class=&quot;n&quot;&gt;payroll_system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11250 &lt;/pre&gt;&lt;/div&gt;
11251 
11252 &lt;p&gt;The program gets the employee list from the &lt;code&gt;EmployeeDatabase&lt;/code&gt; and retrieves the first employee, which is the manager we want. Then it creates a new &lt;code&gt;HourlyPolicy&lt;/code&gt; initialized at $55 per hour and assigns it to the manager object.&lt;/p&gt;
11253 &lt;p&gt;The new policy is now used by the &lt;code&gt;PayrollSystem&lt;/code&gt; modifying the existing behavior. You can run the program again to see the result:&lt;/p&gt;
11254 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python program.py
11255 
11256 &lt;span class=&quot;go&quot;&gt;Tracking Employee Productivity&lt;/span&gt;
11257 &lt;span class=&quot;go&quot;&gt;==============================&lt;/span&gt;
11258 &lt;span class=&quot;go&quot;&gt;Employee 1 - Mary Poppins:&lt;/span&gt;
11259 &lt;span class=&quot;go&quot;&gt;- screams and yells for 40 hours.&lt;/span&gt;
11260 
11261 &lt;span class=&quot;go&quot;&gt;Employee 2 - John Smith:&lt;/span&gt;
11262 &lt;span class=&quot;go&quot;&gt;- does paperwork for 40 hours.&lt;/span&gt;
11263 
11264 &lt;span class=&quot;go&quot;&gt;Employee 3 - Kevin Bacon:&lt;/span&gt;
11265 &lt;span class=&quot;go&quot;&gt;- expends 40 hours on the phone.&lt;/span&gt;
11266 
11267 &lt;span class=&quot;go&quot;&gt;Employee 4 - Jane Doe:&lt;/span&gt;
11268 &lt;span class=&quot;go&quot;&gt;- manufactures gadgets for 40 hours.&lt;/span&gt;
11269 
11270 &lt;span class=&quot;go&quot;&gt;Employee 5 - Robin Williams:&lt;/span&gt;
11271 &lt;span class=&quot;go&quot;&gt;- does paperwork for 40 hours.&lt;/span&gt;
11272 
11273 
11274 &lt;span class=&quot;go&quot;&gt;Calculating Payroll&lt;/span&gt;
11275 &lt;span class=&quot;go&quot;&gt;===================&lt;/span&gt;
11276 &lt;span class=&quot;go&quot;&gt;Payroll for: 1 - Mary Poppins&lt;/span&gt;
11277 &lt;span class=&quot;go&quot;&gt;- Check amount: 2200&lt;/span&gt;
11278 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
11279 &lt;span class=&quot;go&quot;&gt;121 Admin Rd.&lt;/span&gt;
11280 &lt;span class=&quot;go&quot;&gt;Concord, NH 03301&lt;/span&gt;
11281 
11282 &lt;span class=&quot;go&quot;&gt;Payroll for: 2 - John Smith&lt;/span&gt;
11283 &lt;span class=&quot;go&quot;&gt;- Check amount: 1500&lt;/span&gt;
11284 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
11285 &lt;span class=&quot;go&quot;&gt;67 Paperwork Ave&lt;/span&gt;
11286 &lt;span class=&quot;go&quot;&gt;Manchester, NH 03101&lt;/span&gt;
11287 
11288 &lt;span class=&quot;go&quot;&gt;Payroll for: 3 - Kevin Bacon&lt;/span&gt;
11289 &lt;span class=&quot;go&quot;&gt;- Check amount: 1800.0&lt;/span&gt;
11290 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
11291 &lt;span class=&quot;go&quot;&gt;15 Rose St&lt;/span&gt;
11292 &lt;span class=&quot;go&quot;&gt;Apt. B-1&lt;/span&gt;
11293 &lt;span class=&quot;go&quot;&gt;Concord, NH 03301&lt;/span&gt;
11294 
11295 &lt;span class=&quot;go&quot;&gt;Payroll for: 4 - Jane Doe&lt;/span&gt;
11296 &lt;span class=&quot;go&quot;&gt;- Check amount: 600&lt;/span&gt;
11297 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
11298 &lt;span class=&quot;go&quot;&gt;39 Sole St.&lt;/span&gt;
11299 &lt;span class=&quot;go&quot;&gt;Concord, NH 03301&lt;/span&gt;
11300 
11301 &lt;span class=&quot;go&quot;&gt;Payroll for: 5 - Robin Williams&lt;/span&gt;
11302 &lt;span class=&quot;go&quot;&gt;- Check amount: 360&lt;/span&gt;
11303 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
11304 &lt;span class=&quot;go&quot;&gt;99 Mountain Rd.&lt;/span&gt;
11305 &lt;span class=&quot;go&quot;&gt;Concord, NH 03301&lt;/span&gt;
11306 &lt;/pre&gt;&lt;/div&gt;
11307 
11308 &lt;p&gt;The check for Mary Poppins, our manager, is now for $2200 instead of the fixed salary of $3000 that she had per week.&lt;/p&gt;
11309 &lt;p&gt;Notice how we added that business rule to the program without changing any of the existing classes. Consider what type of changes would&amp;rsquo;ve been required with an inheritance design. &lt;/p&gt;
11310 &lt;p&gt;You would&amp;rsquo;ve had to create a new class and change the type of the manager employee. There is no chance you could&amp;rsquo;ve changed the policy at run-time.&lt;/p&gt;
11311 &lt;h2 id=&quot;choosing-between-inheritance-and-composition-in-python&quot;&gt;Choosing Between Inheritance and Composition in Python&lt;/h2&gt;
11312 &lt;p&gt;So far, you&amp;rsquo;ve seen how inheritance and composition work in Python. You&amp;rsquo;ve seen that derived classes inherit the interface and implementation of their base classes. You&amp;rsquo;ve also seen that composition allows you to reuse the implementation of another class.&lt;/p&gt;
11313 &lt;p&gt;You&amp;rsquo;ve implemented two solutions to the same problem. The first solution used multiple inheritance, and the second one used composition.&lt;/p&gt;
11314 &lt;p&gt;You&amp;rsquo;ve also seen that Python&amp;rsquo;s duck typing allows you to reuse objects with existing parts of a program by implementing the desired interface. In Python, it isn&amp;rsquo;t necessary to derive from a base class for your classes to be reused.&lt;/p&gt;
11315 &lt;p&gt;At this point, you might be asking when to use inheritance vs composition in Python. They both enable code reuse. Inheritance and composition can tackle similar problems in your Python programs.&lt;/p&gt;
11316 &lt;p&gt;The general advice is to use the relationship that creates fewer dependencies between two classes. This relation is composition. Still, there will be times where inheritance will make more sense.&lt;/p&gt;
11317 &lt;p&gt;The following sections provide some guidelines to help you make the right choice between inheritance and composition in Python.&lt;/p&gt;
11318 &lt;h3 id=&quot;inheritance-to-model-is-a-relationship&quot;&gt;Inheritance to Model &amp;ldquo;Is A&amp;rdquo; Relationship&lt;/h3&gt;
11319 &lt;p&gt;Inheritance should only be used to model an &lt;strong&gt;is a&lt;/strong&gt; relationship. Liskov&amp;rsquo;s substitution principle says that an object of type &lt;code&gt;Derived&lt;/code&gt;, which inherits from &lt;code&gt;Base&lt;/code&gt;, can replace an object of type &lt;code&gt;Base&lt;/code&gt; without altering the desirable properties of a program.&lt;/p&gt;
11320 &lt;p&gt;Liskov&amp;rsquo;s substitution principle is the most important guideline to determine if inheritance is the appropriate design solution. Still, the answer might not be straightforward in all situations. Fortunately, there is a simple test you can use to determine if your design follows Liskov&amp;rsquo;s substitution principle.&lt;/p&gt;
11321 &lt;p&gt;Let&amp;rsquo;s say you have a class &lt;code&gt;A&lt;/code&gt; that provides an implementation and interface you want to reuse in another class &lt;code&gt;B&lt;/code&gt;. Your initial thought is that you can derive &lt;code&gt;B&lt;/code&gt; from &lt;code&gt;A&lt;/code&gt; and inherit both the interface and implementation. To be sure this is the right design, you follow theses steps:&lt;/p&gt;
11322 &lt;ol&gt;
11323 &lt;li&gt;
11324 &lt;p&gt;&lt;strong&gt;Evaluate &lt;code&gt;B&lt;/code&gt; is an &lt;code&gt;A&lt;/code&gt;:&lt;/strong&gt; Think about this relationship and justify it. Does it make sense?&lt;/p&gt;
11325 &lt;/li&gt;
11326 &lt;li&gt;
11327 &lt;p&gt;&lt;strong&gt;Evaluate &lt;code&gt;A&lt;/code&gt; is a &lt;code&gt;B&lt;/code&gt;:&lt;/strong&gt; Reverse the relationship and justify it. Does it also make sense?&lt;/p&gt;
11328 &lt;/li&gt;
11329 &lt;/ol&gt;
11330 &lt;p&gt;If you can justify both relationships, then you should never inherit those classes from one another. Let&amp;rsquo;s look at a more concrete example.&lt;/p&gt;
11331 &lt;p&gt;You have a class &lt;code&gt;Rectangle&lt;/code&gt; which exposes an &lt;code&gt;.area&lt;/code&gt; property. You need a class &lt;code&gt;Square&lt;/code&gt;, which also has an &lt;code&gt;.area&lt;/code&gt;. It seems that a &lt;code&gt;Square&lt;/code&gt; is a special type of &lt;code&gt;Rectangle&lt;/code&gt;, so maybe you can derive from it and leverage both the interface and implementation.&lt;/p&gt;
11332 &lt;p&gt;Before you jump into the implementation, you use Liskov&amp;rsquo;s substitution principle to evaluate the relationship.&lt;/p&gt;
11333 &lt;p&gt;A &lt;code&gt;Square&lt;/code&gt; &lt;strong&gt;is a&lt;/strong&gt; &lt;code&gt;Rectangle&lt;/code&gt; because its area is calculated from the product of its &lt;code&gt;height&lt;/code&gt; times its &lt;code&gt;length&lt;/code&gt;. The constraint is that &lt;code&gt;Square.height&lt;/code&gt; and &lt;code&gt;Square.length&lt;/code&gt; must be equal.&lt;/p&gt;
11334 &lt;p&gt;It makes sense. You can justify the relationship and explain why a &lt;code&gt;Square&lt;/code&gt; &lt;strong&gt;is a&lt;/strong&gt; &lt;code&gt;Rectangle&lt;/code&gt;. Let&amp;rsquo;s reverse the relationship to see if it makes sense.&lt;/p&gt;
11335 &lt;p&gt;A &lt;code&gt;Rectangle&lt;/code&gt; &lt;strong&gt;is a&lt;/strong&gt; &lt;code&gt;Square&lt;/code&gt; because its area is calculated from the product of its &lt;code&gt;height&lt;/code&gt; times its &lt;code&gt;length&lt;/code&gt;. The difference is that &lt;code&gt;Rectangle.height&lt;/code&gt; and &lt;code&gt;Rectangle.width&lt;/code&gt; can change independently.&lt;/p&gt;
11336 &lt;p&gt;It also makes sense. You can justify the relationship and describe the special constraints for each class. This is a good sign that these two classes should never derive from each other.&lt;/p&gt;
11337 &lt;p&gt;You might have seen other examples that derive &lt;code&gt;Square&lt;/code&gt; from &lt;code&gt;Rectangle&lt;/code&gt; to explain inheritance. You might be skeptical with the little test you just did. Fair enough. Let&amp;rsquo;s write a program that illustrates the problem with deriving &lt;code&gt;Square&lt;/code&gt; from &lt;code&gt;Rectangle&lt;/code&gt;.&lt;/p&gt;
11338 &lt;p&gt;First, you implement &lt;code&gt;Rectangle&lt;/code&gt;. You&amp;rsquo;re even going to &lt;a href=&quot;https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)&quot;&gt;encapsulate&lt;/a&gt; the attributes to ensure that all the constraints are met:&lt;/p&gt;
11339 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In rectangle_square_demo.py&lt;/span&gt;
11340 
11341 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Rectangle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11342     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11343         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;
11344         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_height&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;height&lt;/span&gt;
11345 
11346     &lt;span class=&quot;nd&quot;&gt;@property&lt;/span&gt;
11347     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;area&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11348         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_height&lt;/span&gt;
11349 &lt;/pre&gt;&lt;/div&gt;
11350 
11351 &lt;p&gt;The &lt;code&gt;Rectangle&lt;/code&gt; class is initialized with a &lt;code&gt;length&lt;/code&gt; and a &lt;code&gt;height&lt;/code&gt;, and it provides an &lt;code&gt;.area&lt;/code&gt; property that returns the area. The &lt;code&gt;length&lt;/code&gt; and &lt;code&gt;height&lt;/code&gt; are encapsulated to avoid changing them directly.&lt;/p&gt;
11352 &lt;p&gt;Now, you derive &lt;code&gt;Square&lt;/code&gt; from &lt;code&gt;Rectangle&lt;/code&gt; and override the necessary interface to meet the constraints of a &lt;code&gt;Square&lt;/code&gt;:&lt;/p&gt;
11353 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In rectangle_square_demo.py&lt;/span&gt;
11354 
11355 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Rectangle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11356     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;side_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11357         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;side_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;side_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11358 &lt;/pre&gt;&lt;/div&gt;
11359 
11360 &lt;p&gt;The &lt;code&gt;Square&lt;/code&gt; class is initialized with a &lt;code&gt;side_size&lt;/code&gt;, which is used to initialize both components of the base class. Now, you write a small program to test the behavior:&lt;/p&gt;
11361 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In rectangle_square_demo.py&lt;/span&gt;
11362 
11363 &lt;span class=&quot;n&quot;&gt;rectangle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Rectangle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11364 &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rectangle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;area&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;
11365 
11366 &lt;span class=&quot;n&quot;&gt;square&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11367 &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;area&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;
11368 
11369 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;OK!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11370 &lt;/pre&gt;&lt;/div&gt;
11371 
11372 &lt;p&gt;The program creates a &lt;code&gt;Rectangle&lt;/code&gt; and a &lt;code&gt;Square&lt;/code&gt; and asserts that their &lt;code&gt;.area&lt;/code&gt; is calculated correctly. You can run the program and see that everything is &lt;code&gt;OK&lt;/code&gt; so far:&lt;/p&gt;
11373 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python rectangle_square_demo.py
11374 
11375 &lt;span class=&quot;go&quot;&gt;OK!&lt;/span&gt;
11376 &lt;/pre&gt;&lt;/div&gt;
11377 
11378 &lt;p&gt;The program executes correctly, so it seems that &lt;code&gt;Square&lt;/code&gt; is just a special case of a &lt;code&gt;Rectangle&lt;/code&gt;.&lt;/p&gt;
11379 &lt;p&gt;Later on, you need to support resizing &lt;code&gt;Rectangle&lt;/code&gt; objects, so you make the appropriate changes to the class:&lt;/p&gt;
11380 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In rectangle_square_demo.py&lt;/span&gt;
11381 
11382 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Rectangle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11383     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11384         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;
11385         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_height&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;height&lt;/span&gt;
11386 
11387     &lt;span class=&quot;nd&quot;&gt;@property&lt;/span&gt;
11388     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;area&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11389         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_height&lt;/span&gt;
11390 
11391     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;resize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new_height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11392         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new_length&lt;/span&gt;
11393         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_height&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new_height&lt;/span&gt;
11394 &lt;/pre&gt;&lt;/div&gt;
11395 
11396 &lt;p&gt;&lt;code&gt;.resize()&lt;/code&gt; takes the &lt;code&gt;new_length&lt;/code&gt; and &lt;code&gt;new_width&lt;/code&gt; for the object. You can add the following code to the program to verify that it works correctly:&lt;/p&gt;
11397 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In rectangle_square_demo.py&lt;/span&gt;
11398 
11399 &lt;span class=&quot;n&quot;&gt;rectangle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;resize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11400 &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rectangle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;area&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;
11401 
11402 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;OK!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11403 &lt;/pre&gt;&lt;/div&gt;
11404 
11405 &lt;p&gt;You resize the rectangle object and assert that the new area is correct. You can run the program to verify the behavior:&lt;/p&gt;
11406 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python rectangle_square_demo.py
11407 
11408 &lt;span class=&quot;go&quot;&gt;OK!&lt;/span&gt;
11409 &lt;/pre&gt;&lt;/div&gt;
11410 
11411 &lt;p&gt;The assertion passes, and you see that the program runs correctly.&lt;/p&gt;
11412 &lt;p&gt;So, what happens if you resize a square? Modify the program, and try to modify the &lt;code&gt;square&lt;/code&gt; object:&lt;/p&gt;
11413 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In rectangle_square_demo.py&lt;/span&gt;
11414 
11415 &lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;resize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11416 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Square area: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{square.area}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11417 &lt;/pre&gt;&lt;/div&gt;
11418 
11419 &lt;p&gt;You pass the same parameters to &lt;code&gt;square.resize()&lt;/code&gt; that you used with &lt;code&gt;rectangle&lt;/code&gt;, and print the area. When you run the program you see:&lt;/p&gt;
11420 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python rectangle_square_demo.py
11421 
11422 &lt;span class=&quot;go&quot;&gt;Square area: 15&lt;/span&gt;
11423 &lt;span class=&quot;go&quot;&gt;OK!&lt;/span&gt;
11424 &lt;/pre&gt;&lt;/div&gt;
11425 
11426 &lt;p&gt;The program shows that the new area is &lt;code&gt;15&lt;/code&gt; like the &lt;code&gt;rectangle&lt;/code&gt; object. The problem now is that the &lt;code&gt;square&lt;/code&gt; object no longer meets the &lt;code&gt;Square&lt;/code&gt; class constraint that the &lt;code&gt;length&lt;/code&gt; and &lt;code&gt;height&lt;/code&gt; must be equal.&lt;/p&gt;
11427 &lt;p&gt;How can you fix that problem? You can try several approaches, but all of them will be awkward. You can override &lt;code&gt;.resize()&lt;/code&gt; in &lt;code&gt;square&lt;/code&gt; and ignore the &lt;code&gt;height&lt;/code&gt; parameter, but that will be confusing for people looking at other parts of the program where &lt;code&gt;rectangles&lt;/code&gt; are being resized and some of them are not getting the expected areas because they are really &lt;code&gt;squares&lt;/code&gt;.&lt;/p&gt;
11428 &lt;p&gt;In a small program like this one, it might be easy to spot the causes of the weird behavior, but in a more complex program, the problem will be harder to find.&lt;/p&gt;
11429 &lt;p&gt;The reality is that if you&amp;rsquo;re able to justify an inheritance relationship between two classes both ways, you should not derive one class from another. &lt;/p&gt;
11430 &lt;p&gt;In the example, it doesn&amp;rsquo;t make sense that &lt;code&gt;Square&lt;/code&gt; inherits the interface and implementation of &lt;code&gt;.resize()&lt;/code&gt; from &lt;code&gt;Rectangle&lt;/code&gt;. That doesn&amp;rsquo;t mean that &lt;code&gt;Square&lt;/code&gt; objects can&amp;rsquo;t be resized. It means that the interface is different because it only needs a &lt;code&gt;side_size&lt;/code&gt; parameter.&lt;/p&gt;
11431 &lt;p&gt;This difference in interface justifies not deriving &lt;code&gt;Square&lt;/code&gt; from &lt;code&gt;Rectangle&lt;/code&gt; like the test above advised.&lt;/p&gt;
11432 &lt;h3 id=&quot;mixing-features-with-mixin-classes&quot;&gt;Mixing Features With Mixin Classes&lt;/h3&gt;
11433 &lt;p&gt;One of the uses of multiple inheritance in Python is to extend a class features through &lt;a href=&quot;https://en.wikipedia.org/wiki/Mixin&quot;&gt;mixins&lt;/a&gt;. A &lt;strong&gt;mixin&lt;/strong&gt; is a class that provides methods to other classes but are not considered a base class.&lt;/p&gt;
11434 &lt;p&gt;A mixin allows other classes to reuse its interface and implementation without becoming a super class. They implement a unique behavior that can be aggregated to other unrelated classes. They are similar to composition but they create a stronger relationship.&lt;/p&gt;
11435 &lt;p&gt;Let&amp;rsquo;s say you want to convert objects of certain types in your application to a dictionary representation of the object. You could provide a &lt;code&gt;.to_dict()&lt;/code&gt; method in every class that you want to support this feature, but the implementation of &lt;code&gt;.to_dict()&lt;/code&gt; seems to be very similar.&lt;/p&gt;
11436 &lt;p&gt;This could be a good candidate for a mixin. You start by slightly modifying the &lt;code&gt;Employee&lt;/code&gt; class from the composition example:&lt;/p&gt;
11437 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In employees.py&lt;/span&gt;
11438 
11439 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11440     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;role&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11441         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;
11442         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
11443         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;
11444 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_role&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;role&lt;/span&gt;
11445 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_payroll&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payroll&lt;/span&gt;
11446 &lt;/span&gt;
11447 
11448     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11449         &lt;span class=&quot;n&quot;&gt;duties&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_role&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;perform_duties&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11450         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Employee &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.id}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;:&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11451         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;- &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{duties}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11452         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11453         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_payroll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;track_work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11454 
11455     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11456         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_payroll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11457 &lt;/pre&gt;&lt;/div&gt;
11458 
11459 &lt;p&gt;The change is very small. You just changed the &lt;code&gt;role&lt;/code&gt; and &lt;code&gt;payroll&lt;/code&gt; attributes to be internal by adding a leading underscore to their name. You will see soon why you are making that change.&lt;/p&gt;
11460 &lt;p&gt;Now, you add the &lt;code&gt;AsDictionaryMixin&lt;/code&gt; class:&lt;/p&gt;
11461 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In representations.py&lt;/span&gt;
11462 
11463 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AsDictionaryMixin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11464     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;to_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11465         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
11466             &lt;span class=&quot;n&quot;&gt;prop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_represent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11467             &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;vm&quot;&gt;__dict__&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11468             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_is_internal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11469         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
11470 
11471     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_represent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11472         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11473             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;hasattr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;to_dict&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11474                 &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11475             &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11476                 &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11477         &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11478             &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
11479 
11480     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_is_internal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11481         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;startswith&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;_&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11482 &lt;/pre&gt;&lt;/div&gt;
11483 
11484 &lt;p&gt;The &lt;code&gt;AsDictionaryMixin&lt;/code&gt; class exposes a &lt;code&gt;.to_dict()&lt;/code&gt; method that returns the representation of itself as a dictionary. The method is implemented as a &lt;a href=&quot;https://www.python.org/dev/peps/pep-0274/&quot;&gt;&lt;code&gt;dict&lt;/code&gt; comprehension&lt;/a&gt; that says, &amp;ldquo;Create a dictionary mapping &lt;code&gt;prop&lt;/code&gt; to &lt;code&gt;value&lt;/code&gt; for each item in &lt;code&gt;self.__dict__.items()&lt;/code&gt; if the &lt;code&gt;prop&lt;/code&gt; is not internal.&amp;rdquo;&lt;/p&gt;
11485 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
11486 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This is why we made the role and payroll attributes internal in the &lt;code&gt;Employee&lt;/code&gt; class, because we don&amp;rsquo;t want to represent them in the dictionary.&lt;/p&gt;
11487 &lt;/div&gt;
11488 &lt;p&gt;As you saw at the beginning, creating a class inherits some members from &lt;code&gt;object&lt;/code&gt;, and one of those members is &lt;code&gt;__dict__&lt;/code&gt;, which is basically a mapping of all the attributes in an object to their value.&lt;/p&gt;
11489 &lt;p&gt;You iterate through all the items in &lt;code&gt;__dict__&lt;/code&gt; and filter out the ones that have a name that starts with an underscore using &lt;code&gt;._is_internal()&lt;/code&gt;.&lt;/p&gt;
11490 &lt;p&gt;&lt;code&gt;._represent()&lt;/code&gt; checks the specified value. If the value &lt;strong&gt;is an&lt;/strong&gt; &lt;code&gt;object&lt;/code&gt;, then it looks to see if it also has a &lt;code&gt;.to_dict()&lt;/code&gt; member and uses it to represent the object. Otherwise, it returns a string representation. If the value is not an &lt;code&gt;object&lt;/code&gt;, then it simply returns the value.&lt;/p&gt;
11491 &lt;p&gt;You can modify the &lt;code&gt;Employee&lt;/code&gt; class to support this mixin:&lt;/p&gt;
11492 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In employees.py&lt;/span&gt;
11493 
11494 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;representations&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AsDictionaryMixin&lt;/span&gt;
11495 
11496 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AsDictionaryMixin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11497     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;role&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11498         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;
11499         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
11500         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;
11501         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_role&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;role&lt;/span&gt;
11502         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_payroll&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payroll&lt;/span&gt;
11503 
11504     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11505         &lt;span class=&quot;n&quot;&gt;duties&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_role&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;perform_duties&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11506         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Employee &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.id}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;:&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11507         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;- &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{duties}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11508         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11509         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_payroll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;track_work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11510 
11511     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11512         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_payroll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11513 &lt;/pre&gt;&lt;/div&gt;
11514 
11515 &lt;p&gt;All you have to do is inherit the &lt;code&gt;AsDictionaryMixin&lt;/code&gt; to support the functionality. It will be nice to support the same functionality in the &lt;code&gt;Address&lt;/code&gt; class, so the &lt;code&gt;Employee.address&lt;/code&gt; attribute is represented in the same way:&lt;/p&gt;
11516 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In contacts.py&lt;/span&gt;
11517 
11518 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;representations&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AsDictionaryMixin&lt;/span&gt;
11519 
11520 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AsDictionaryMixin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11521     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;street&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;city&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zipcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;street2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11522         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;street&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;street&lt;/span&gt;
11523         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;street2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;street2&lt;/span&gt;
11524         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;city&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;city&lt;/span&gt;
11525         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;
11526         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zipcode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zipcode&lt;/span&gt;
11527 
11528     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__str__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11529         &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;street&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
11530         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;street2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11531             &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;street2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11532         &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.city}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.state}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.zipcode}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11533         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11534 &lt;/pre&gt;&lt;/div&gt;
11535 
11536 &lt;p&gt;You apply the mixin to the &lt;code&gt;Address&lt;/code&gt; class to support the feature. Now, you can write a small program to test it:&lt;/p&gt;
11537 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# In program.py&lt;/span&gt;
11538 
11539  &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;json&lt;/span&gt;
11540  &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;employees&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EmployeeDatabase&lt;/span&gt;
11541 
11542  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11543     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dumps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;indent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
11544 
11545 &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EmployeeDatabase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11546     &lt;span class=&quot;n&quot;&gt;print_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
11547 &lt;/pre&gt;&lt;/div&gt;
11548 
11549 &lt;p&gt;The program implements a &lt;code&gt;print_dict()&lt;/code&gt; that converts the dictionary to a &lt;a href=&quot;http://json.org/&quot;&gt;JSON&lt;/a&gt; string using indentation so the output looks better.&lt;/p&gt;
11550 &lt;p&gt;Then, it iterates through all the employees, printing the dictionary representation provided by &lt;code&gt;.to_dict()&lt;/code&gt;. You can run the program to see its output:&lt;/p&gt;
11551 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt; $&lt;/span&gt; python program.py
11552 
11553 &lt;span class=&quot;go&quot;&gt; {&lt;/span&gt;
11554 &lt;span class=&quot;go&quot;&gt;  &amp;quot;id&amp;quot;: &amp;quot;1&amp;quot;,&lt;/span&gt;
11555 &lt;span class=&quot;go&quot;&gt;  &amp;quot;name&amp;quot;: &amp;quot;Mary Poppins&amp;quot;,&lt;/span&gt;
11556 &lt;span class=&quot;go&quot;&gt;  &amp;quot;address&amp;quot;: {&lt;/span&gt;
11557 &lt;span class=&quot;go&quot;&gt;    &amp;quot;street&amp;quot;: &amp;quot;121 Admin Rd.&amp;quot;,&lt;/span&gt;
11558 &lt;span class=&quot;go&quot;&gt;    &amp;quot;street2&amp;quot;: &amp;quot;&amp;quot;,&lt;/span&gt;
11559 &lt;span class=&quot;go&quot;&gt;    &amp;quot;city&amp;quot;: &amp;quot;Concord&amp;quot;,&lt;/span&gt;
11560 &lt;span class=&quot;go&quot;&gt;    &amp;quot;state&amp;quot;: &amp;quot;NH&amp;quot;,&lt;/span&gt;
11561 &lt;span class=&quot;go&quot;&gt;    &amp;quot;zipcode&amp;quot;: &amp;quot;03301&amp;quot;&lt;/span&gt;
11562 &lt;span class=&quot;go&quot;&gt;  }&lt;/span&gt;
11563 &lt;span class=&quot;go&quot;&gt;}&lt;/span&gt;
11564 &lt;span class=&quot;go&quot;&gt;{&lt;/span&gt;
11565 &lt;span class=&quot;go&quot;&gt;  &amp;quot;id&amp;quot;: &amp;quot;2&amp;quot;,&lt;/span&gt;
11566 &lt;span class=&quot;go&quot;&gt;  &amp;quot;name&amp;quot;: &amp;quot;John Smith&amp;quot;,&lt;/span&gt;
11567 &lt;span class=&quot;go&quot;&gt;  &amp;quot;address&amp;quot;: {&lt;/span&gt;
11568 &lt;span class=&quot;go&quot;&gt;    &amp;quot;street&amp;quot;: &amp;quot;67 Paperwork Ave&amp;quot;,&lt;/span&gt;
11569 &lt;span class=&quot;go&quot;&gt;    &amp;quot;street2&amp;quot;: &amp;quot;&amp;quot;,&lt;/span&gt;
11570 &lt;span class=&quot;go&quot;&gt;    &amp;quot;city&amp;quot;: &amp;quot;Manchester&amp;quot;,&lt;/span&gt;
11571 &lt;span class=&quot;go&quot;&gt;    &amp;quot;state&amp;quot;: &amp;quot;NH&amp;quot;,&lt;/span&gt;
11572 &lt;span class=&quot;go&quot;&gt;    &amp;quot;zipcode&amp;quot;: &amp;quot;03101&amp;quot;&lt;/span&gt;
11573 &lt;span class=&quot;go&quot;&gt;  }&lt;/span&gt;
11574 &lt;span class=&quot;go&quot;&gt;}&lt;/span&gt;
11575 &lt;span class=&quot;go&quot;&gt;{&lt;/span&gt;
11576 &lt;span class=&quot;go&quot;&gt;  &amp;quot;id&amp;quot;: &amp;quot;3&amp;quot;,&lt;/span&gt;
11577 &lt;span class=&quot;go&quot;&gt;  &amp;quot;name&amp;quot;: &amp;quot;Kevin Bacon&amp;quot;,&lt;/span&gt;
11578 &lt;span class=&quot;go&quot;&gt;  &amp;quot;address&amp;quot;: {&lt;/span&gt;
11579 &lt;span class=&quot;go&quot;&gt;    &amp;quot;street&amp;quot;: &amp;quot;15 Rose St&amp;quot;,&lt;/span&gt;
11580 &lt;span class=&quot;go&quot;&gt;    &amp;quot;street2&amp;quot;: &amp;quot;Apt. B-1&amp;quot;,&lt;/span&gt;
11581 &lt;span class=&quot;go&quot;&gt;    &amp;quot;city&amp;quot;: &amp;quot;Concord&amp;quot;,&lt;/span&gt;
11582 &lt;span class=&quot;go&quot;&gt;    &amp;quot;state&amp;quot;: &amp;quot;NH&amp;quot;,&lt;/span&gt;
11583 &lt;span class=&quot;go&quot;&gt;    &amp;quot;zipcode&amp;quot;: &amp;quot;03301&amp;quot;&lt;/span&gt;
11584 &lt;span class=&quot;go&quot;&gt;  }&lt;/span&gt;
11585 &lt;span class=&quot;go&quot;&gt;}&lt;/span&gt;
11586 &lt;span class=&quot;go&quot;&gt;{&lt;/span&gt;
11587 &lt;span class=&quot;go&quot;&gt;  &amp;quot;id&amp;quot;: &amp;quot;4&amp;quot;,&lt;/span&gt;
11588 &lt;span class=&quot;go&quot;&gt;  &amp;quot;name&amp;quot;: &amp;quot;Jane Doe&amp;quot;,&lt;/span&gt;
11589 &lt;span class=&quot;go&quot;&gt;  &amp;quot;address&amp;quot;: {&lt;/span&gt;
11590 &lt;span class=&quot;go&quot;&gt;    &amp;quot;street&amp;quot;: &amp;quot;39 Sole St.&amp;quot;,&lt;/span&gt;
11591 &lt;span class=&quot;go&quot;&gt;    &amp;quot;street2&amp;quot;: &amp;quot;&amp;quot;,&lt;/span&gt;
11592 &lt;span class=&quot;go&quot;&gt;    &amp;quot;city&amp;quot;: &amp;quot;Concord&amp;quot;,&lt;/span&gt;
11593 &lt;span class=&quot;go&quot;&gt;    &amp;quot;state&amp;quot;: &amp;quot;NH&amp;quot;,&lt;/span&gt;
11594 &lt;span class=&quot;go&quot;&gt;    &amp;quot;zipcode&amp;quot;: &amp;quot;03301&amp;quot;&lt;/span&gt;
11595 &lt;span class=&quot;go&quot;&gt;  }&lt;/span&gt;
11596 &lt;span class=&quot;go&quot;&gt;}&lt;/span&gt;
11597 &lt;span class=&quot;go&quot;&gt;{&lt;/span&gt;
11598 &lt;span class=&quot;go&quot;&gt;  &amp;quot;id&amp;quot;: &amp;quot;5&amp;quot;,&lt;/span&gt;
11599 &lt;span class=&quot;go&quot;&gt;  &amp;quot;name&amp;quot;: &amp;quot;Robin Williams&amp;quot;,&lt;/span&gt;
11600 &lt;span class=&quot;go&quot;&gt;  &amp;quot;address&amp;quot;: {&lt;/span&gt;
11601 &lt;span class=&quot;go&quot;&gt;    &amp;quot;street&amp;quot;: &amp;quot;99 Mountain Rd.&amp;quot;,&lt;/span&gt;
11602 &lt;span class=&quot;go&quot;&gt;    &amp;quot;street2&amp;quot;: &amp;quot;&amp;quot;,&lt;/span&gt;
11603 &lt;span class=&quot;go&quot;&gt;    &amp;quot;city&amp;quot;: &amp;quot;Concord&amp;quot;,&lt;/span&gt;
11604 &lt;span class=&quot;go&quot;&gt;    &amp;quot;state&amp;quot;: &amp;quot;NH&amp;quot;,&lt;/span&gt;
11605 &lt;span class=&quot;go&quot;&gt;    &amp;quot;zipcode&amp;quot;: &amp;quot;03301&amp;quot;&lt;/span&gt;
11606 &lt;span class=&quot;go&quot;&gt;  }&lt;/span&gt;
11607 &lt;span class=&quot;go&quot;&gt;}&lt;/span&gt;
11608 &lt;/pre&gt;&lt;/div&gt;
11609 
11610 &lt;p&gt;You leveraged the implementation of &lt;code&gt;AsDictionaryMixin&lt;/code&gt; in both &lt;code&gt;Employee&lt;/code&gt; and &lt;code&gt;Address&lt;/code&gt; classes even when they are not related. Because &lt;code&gt;AsDictionaryMixin&lt;/code&gt; only provides behavior, it is easy to reuse with other classes without causing problems.&lt;/p&gt;
11611 &lt;h3 id=&quot;composition-to-model-has-a-relationship&quot;&gt;Composition to Model &amp;ldquo;Has A&amp;rdquo; Relationship&lt;/h3&gt;
11612 &lt;p&gt;Composition models a &lt;strong&gt;has a&lt;/strong&gt; relationship. With composition, a class &lt;code&gt;Composite&lt;/code&gt; &lt;strong&gt;has an&lt;/strong&gt; instance of class &lt;code&gt;Component&lt;/code&gt; and can leverage its implementation. The &lt;code&gt;Component&lt;/code&gt; class can be reused in other classes completely unrelated to the &lt;code&gt;Composite&lt;/code&gt;.&lt;/p&gt;
11613 &lt;p&gt;In the composition example above, the &lt;code&gt;Employee&lt;/code&gt; class &lt;strong&gt;has an&lt;/strong&gt; &lt;code&gt;Address&lt;/code&gt; object. &lt;code&gt;Address&lt;/code&gt; implements all the functionality to handle addresses, and it can be reused by other classes.&lt;/p&gt;
11614 &lt;p&gt;Other classes like &lt;code&gt;Customer&lt;/code&gt; or &lt;code&gt;Vendor&lt;/code&gt; can reuse &lt;code&gt;Address&lt;/code&gt; without being related to &lt;code&gt;Employee&lt;/code&gt;. They can leverage the same implementation ensuring that addresses are handled consistently across the application.&lt;/p&gt;
11615 &lt;p&gt;A problem you may run into when using composition is that some of your classes may start growing by using multiple components. Your classes may require multiple parameters in the constructor just to pass in the components they are made of. This can make your classes hard to use.&lt;/p&gt;
11616 &lt;p&gt;A way to avoid the problem is by using the &lt;a href=&quot;https://realpython.com/factory-method-python/&quot;&gt;Factory Method&lt;/a&gt; to construct your objects. You did that with the composition example.&lt;/p&gt;
11617 &lt;p&gt;If you look at the implementation of the &lt;code&gt;EmployeeDatabase&lt;/code&gt; class, you&amp;rsquo;ll notice that it uses &lt;code&gt;._create_employee()&lt;/code&gt; to construct an &lt;code&gt;Employee&lt;/code&gt; object with the right parameters.&lt;/p&gt;
11618 &lt;p&gt;This design will work, but ideally, you should be able to construct an &lt;code&gt;Employee&lt;/code&gt; object just by specifying an &lt;code&gt;id&lt;/code&gt;, for example &lt;code&gt;employee = Employee(1)&lt;/code&gt;.&lt;/p&gt;
11619 &lt;p&gt;The following changes might improve your design. You can start with the &lt;code&gt;productivity&lt;/code&gt; module:&lt;/p&gt;
11620 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In productivity.py&lt;/span&gt;
11621 
11622 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;_ProductivitySystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11623     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11624         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_roles&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
11625             &lt;span class=&quot;s1&quot;&gt;&amp;#39;manager&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ManagerRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
11626             &lt;span class=&quot;s1&quot;&gt;&amp;#39;secretary&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SecretaryRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
11627             &lt;span class=&quot;s1&quot;&gt;&amp;#39;sales&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SalesRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
11628             &lt;span class=&quot;s1&quot;&gt;&amp;#39;factory&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FactoryRole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
11629         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
11630 
11631     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_role&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;role_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11632         &lt;span class=&quot;n&quot;&gt;role_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_roles&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;role_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11633         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;role_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11634             &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;role_id&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11635         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;role_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11636 
11637     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11638         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Tracking Employee Productivity&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11639         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;==============================&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11640         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11641             &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11642         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11643 
11644 &lt;span class=&quot;c1&quot;&gt;# Role classes implementation omitted&lt;/span&gt;
11645 
11646 &lt;span class=&quot;n&quot;&gt;_productivity_system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_ProductivitySystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11647 
11648 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_role&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;role_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11649     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_productivity_system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_role&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;role_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11650 
11651 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11652     &lt;span class=&quot;n&quot;&gt;_productivity_system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11653 &lt;/pre&gt;&lt;/div&gt;
11654 
11655 &lt;p&gt;First, you make the &lt;code&gt;_ProductivitySystem&lt;/code&gt; class internal, and then provide a &lt;code&gt;_productivity_system&lt;/code&gt; internal variable to the module. You are communicating to other developers that they should not create or use the &lt;code&gt;_ProductivitySystem&lt;/code&gt; directly. Instead, you provide two functions, &lt;code&gt;get_role()&lt;/code&gt; and &lt;code&gt;track()&lt;/code&gt;, as the public interface to the module. This is what other modules should use.&lt;/p&gt;
11656 &lt;p&gt;What you are saying is that the &lt;code&gt;_ProductivitySystem&lt;/code&gt; is a &lt;a href=&quot;https://en.wikipedia.org/wiki/Singleton_pattern&quot;&gt;Singleton&lt;/a&gt;, and there should only be one object created from it.&lt;/p&gt;
11657 &lt;p&gt;Now, you can do the same with the &lt;code&gt;hr&lt;/code&gt; module:&lt;/p&gt;
11658 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In hr.py&lt;/span&gt;
11659 
11660 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;_PayrollSystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11661     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11662         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_employee_policies&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
11663             &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SalaryPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
11664             &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SalaryPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
11665             &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CommissionPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
11666             &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HourlyPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
11667             &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HourlyPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11668         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
11669 
11670     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11671         &lt;span class=&quot;n&quot;&gt;policy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_employee_policies&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11672         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11673             &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11674         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;policy&lt;/span&gt;
11675 
11676     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11677         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Calculating Payroll&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11678         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;===================&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11679         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11680             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Payroll for: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{employee.id}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{employee.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11681             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;- Check amount: {employee.calculate_payroll()}&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11682             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11683                 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;- Sent to:&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11684                 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11685             &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11686 
11687 &lt;span class=&quot;c1&quot;&gt;# Policy classes implementation omitted&lt;/span&gt;
11688 
11689 &lt;span class=&quot;n&quot;&gt;_payroll_system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_PayrollSystem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11690 
11691 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11692     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_payroll_system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11693 
11694 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11695     &lt;span class=&quot;n&quot;&gt;_payroll_system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11696 &lt;/pre&gt;&lt;/div&gt;
11697 
11698 &lt;p&gt;Again, you make the &lt;code&gt;_PayrollSystem&lt;/code&gt; internal and provide a public interface to it. The application will use the public interface to get policies and calculate payroll.&lt;/p&gt;
11699 &lt;p&gt;You will now do the same with the &lt;code&gt;contacts&lt;/code&gt; module:&lt;/p&gt;
11700 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In contacts.py&lt;/span&gt;
11701 
11702 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;_AddressBook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11703     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11704         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_employee_addresses&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
11705             &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;121 Admin Rd.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Concord&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;NH&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;03301&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
11706             &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;67 Paperwork Ave&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Manchester&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;NH&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;03101&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
11707             &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;15 Rose St&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Concord&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;NH&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;03301&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Apt. B-1&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
11708             &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;39 Sole St.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Concord&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;NH&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;03301&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
11709             &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;99 Mountain Rd.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Concord&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;NH&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;03301&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
11710         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
11711 
11712     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_employee_address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11713         &lt;span class=&quot;n&quot;&gt;address&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_employee_addresses&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11714         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11715             &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11716         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;
11717 
11718 &lt;span class=&quot;c1&quot;&gt;# Implementation of Address class omitted&lt;/span&gt;
11719 
11720 &lt;span class=&quot;n&quot;&gt;_address_book&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_AddressBook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11721 
11722 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_employee_address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11723     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_address_book&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_employee_address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11724 &lt;/pre&gt;&lt;/div&gt;
11725 
11726 &lt;p&gt;You are basically saying that there should only be one &lt;code&gt;_AddressBook&lt;/code&gt;, one &lt;code&gt;_PayrollSystem&lt;/code&gt;, and one &lt;code&gt;_ProductivitySystem&lt;/code&gt;. Again, this design pattern is called the &lt;a href=&quot;https://en.wikipedia.org/wiki/Singleton_pattern&quot;&gt;Singleton&lt;/a&gt; design pattern, which comes in handy for classes from which there should only be one, single instance.&lt;/p&gt;
11727 &lt;p&gt;Now, you can work on the &lt;code&gt;employees&lt;/code&gt; module. You will also make a Singleton out of the &lt;code&gt;_EmployeeDatabase&lt;/code&gt;, but you will make some additional changes:&lt;/p&gt;
11728 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In employees.py&lt;/span&gt;
11729 
11730 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;productivity&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_role&lt;/span&gt;
11731 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hr&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_policy&lt;/span&gt;
11732 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;contacts&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_employee_address&lt;/span&gt;
11733 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;representations&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AsDictionaryMixin&lt;/span&gt;
11734 
11735 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;_EmployeeDatabase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11736     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11737         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_employees&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
11738             &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
11739                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Mary Poppins&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
11740                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;role&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;manager&amp;#39;&lt;/span&gt;
11741             &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
11742             &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
11743                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;John Smith&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
11744                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;role&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;secretary&amp;#39;&lt;/span&gt;
11745             &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
11746             &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
11747                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Kevin Bacon&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
11748                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;role&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;sales&amp;#39;&lt;/span&gt;
11749             &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
11750             &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
11751                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Jane Doe&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
11752                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;role&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;factory&amp;#39;&lt;/span&gt;
11753             &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
11754             &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
11755                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Robin Williams&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
11756                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;role&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;secretary&amp;#39;&lt;/span&gt;
11757             &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
11758         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
11759 
11760 
11761     &lt;span class=&quot;nd&quot;&gt;@property&lt;/span&gt;
11762     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11763         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sorted&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
11764 
11765     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_employee_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11766         &lt;span class=&quot;n&quot;&gt;info&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_employees&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11767         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11768             &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employee_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11769         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;
11770 
11771 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AsDictionaryMixin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11772     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11773         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;
11774         &lt;span class=&quot;n&quot;&gt;info&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee_database&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_employee_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11775         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11776         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_employee_address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11777         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_role&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_role&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;role&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
11778         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_payroll&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11779 
11780     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11781         &lt;span class=&quot;n&quot;&gt;duties&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_role&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;perform_duties&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11782         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Employee &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.id}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;:&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11783         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;- &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{duties}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11784         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11785         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_payroll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;track_work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11786 
11787     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11788         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_payroll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11789 
11790 
11791 &lt;span class=&quot;n&quot;&gt;employee_database&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_EmployeeDatabase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11792 &lt;/pre&gt;&lt;/div&gt;
11793 
11794 &lt;p&gt;You first import the relevant functions and classes from other modules. The &lt;code&gt;_EmployeeDatabase&lt;/code&gt; is made internal, and at the bottom, you create a single instance. This instance is public and part of the interface because you will want to use it in the application.&lt;/p&gt;
11795 &lt;p&gt;You changed the &lt;code&gt;_EmployeeDatabase._employees&lt;/code&gt; attribute to be a dictionary where the key is the employee &lt;code&gt;id&lt;/code&gt; and the value is the employee information. You also exposed a &lt;code&gt;.get_employee_info()&lt;/code&gt; method to return the information for the specified employee &lt;code&gt;employee_id&lt;/code&gt;.&lt;/p&gt;
11796 &lt;p&gt;The &lt;code&gt;_EmployeeDatabase.employees&lt;/code&gt; property now sorts the keys to return the employees sorted by their &lt;code&gt;id&lt;/code&gt;. You replaced the method that constructed the &lt;code&gt;Employee&lt;/code&gt; objects with calls to the &lt;code&gt;Employee&lt;/code&gt; initializer directly.&lt;/p&gt;
11797 &lt;p&gt;The &lt;code&gt;Employee&lt;/code&gt; class now is initialized with the &lt;code&gt;id&lt;/code&gt; and uses the public functions exposed in the other modules to initialize its attributes.&lt;/p&gt;
11798 &lt;p&gt;You can now change the program to test the changes:&lt;/p&gt;
11799 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In program.py&lt;/span&gt;
11800 
11801 &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;json&lt;/span&gt;
11802 
11803 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hr&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;
11804 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;productivity&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;
11805 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;employees&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee_database&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Employee&lt;/span&gt;
11806 
11807 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11808     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dumps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;indent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
11809 
11810 &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee_database&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;
11811 
11812 &lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11813 &lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11814 
11815 &lt;span class=&quot;n&quot;&gt;temp_secretary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11816 &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Temporary Secretary:&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11817 &lt;span class=&quot;n&quot;&gt;print_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temp_secretary&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
11818 &lt;/pre&gt;&lt;/div&gt;
11819 
11820 &lt;p&gt;You import the relevant functions from the &lt;code&gt;hr&lt;/code&gt; and &lt;code&gt;productivity&lt;/code&gt; modules, as well as the &lt;code&gt;employee_database&lt;/code&gt; and &lt;code&gt;Employee&lt;/code&gt; class. The program is cleaner because you exposed the required interface and encapsulated how objects are accessed.&lt;/p&gt;
11821 &lt;p&gt;Notice that you can now create an &lt;code&gt;Employee&lt;/code&gt; object directly just using its &lt;code&gt;id&lt;/code&gt;. You can run the program to see its output:&lt;/p&gt;
11822 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python program.py
11823 
11824 &lt;span class=&quot;go&quot;&gt;Tracking Employee Productivity&lt;/span&gt;
11825 &lt;span class=&quot;go&quot;&gt;==============================&lt;/span&gt;
11826 &lt;span class=&quot;go&quot;&gt;Employee 1 - Mary Poppins:&lt;/span&gt;
11827 &lt;span class=&quot;go&quot;&gt;- screams and yells for 40 hours.&lt;/span&gt;
11828 
11829 &lt;span class=&quot;go&quot;&gt;Employee 2 - John Smith:&lt;/span&gt;
11830 &lt;span class=&quot;go&quot;&gt;- does paperwork for 40 hours.&lt;/span&gt;
11831 
11832 &lt;span class=&quot;go&quot;&gt;Employee 3 - Kevin Bacon:&lt;/span&gt;
11833 &lt;span class=&quot;go&quot;&gt;- expends 40 hours on the phone.&lt;/span&gt;
11834 
11835 &lt;span class=&quot;go&quot;&gt;Employee 4 - Jane Doe:&lt;/span&gt;
11836 &lt;span class=&quot;go&quot;&gt;- manufactures gadgets for 40 hours.&lt;/span&gt;
11837 
11838 &lt;span class=&quot;go&quot;&gt;Employee 5 - Robin Williams:&lt;/span&gt;
11839 &lt;span class=&quot;go&quot;&gt;- does paperwork for 40 hours.&lt;/span&gt;
11840 
11841 &lt;span class=&quot;go&quot;&gt;Calculating Payroll&lt;/span&gt;
11842 &lt;span class=&quot;go&quot;&gt;===================&lt;/span&gt;
11843 &lt;span class=&quot;go&quot;&gt;Payroll for: 1 - Mary Poppins&lt;/span&gt;
11844 &lt;span class=&quot;go&quot;&gt;- Check amount: 3000&lt;/span&gt;
11845 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
11846 &lt;span class=&quot;go&quot;&gt;121 Admin Rd.&lt;/span&gt;
11847 &lt;span class=&quot;go&quot;&gt;Concord, NH 03301&lt;/span&gt;
11848 
11849 &lt;span class=&quot;go&quot;&gt;Payroll for: 2 - John Smith&lt;/span&gt;
11850 &lt;span class=&quot;go&quot;&gt;- Check amount: 1500&lt;/span&gt;
11851 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
11852 &lt;span class=&quot;go&quot;&gt;67 Paperwork Ave&lt;/span&gt;
11853 &lt;span class=&quot;go&quot;&gt;Manchester, NH 03101&lt;/span&gt;
11854 
11855 &lt;span class=&quot;go&quot;&gt;Payroll for: 3 - Kevin Bacon&lt;/span&gt;
11856 &lt;span class=&quot;go&quot;&gt;- Check amount: 1800.0&lt;/span&gt;
11857 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
11858 &lt;span class=&quot;go&quot;&gt;15 Rose St&lt;/span&gt;
11859 &lt;span class=&quot;go&quot;&gt;Apt. B-1&lt;/span&gt;
11860 &lt;span class=&quot;go&quot;&gt;Concord, NH 03301&lt;/span&gt;
11861 
11862 &lt;span class=&quot;go&quot;&gt;Payroll for: 4 - Jane Doe&lt;/span&gt;
11863 &lt;span class=&quot;go&quot;&gt;- Check amount: 600&lt;/span&gt;
11864 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
11865 &lt;span class=&quot;go&quot;&gt;39 Sole St.&lt;/span&gt;
11866 &lt;span class=&quot;go&quot;&gt;Concord, NH 03301&lt;/span&gt;
11867 
11868 &lt;span class=&quot;go&quot;&gt;Payroll for: 5 - Robin Williams&lt;/span&gt;
11869 &lt;span class=&quot;go&quot;&gt;- Check amount: 360&lt;/span&gt;
11870 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
11871 &lt;span class=&quot;go&quot;&gt;99 Mountain Rd.&lt;/span&gt;
11872 &lt;span class=&quot;go&quot;&gt;Concord, NH 03301&lt;/span&gt;
11873 
11874 &lt;span class=&quot;go&quot;&gt;Temporary Secretary:&lt;/span&gt;
11875 &lt;span class=&quot;go&quot;&gt;{&lt;/span&gt;
11876 &lt;span class=&quot;go&quot;&gt;  &amp;quot;id&amp;quot;: &amp;quot;5&amp;quot;,&lt;/span&gt;
11877 &lt;span class=&quot;go&quot;&gt;  &amp;quot;name&amp;quot;: &amp;quot;Robin Williams&amp;quot;,&lt;/span&gt;
11878 &lt;span class=&quot;go&quot;&gt;  &amp;quot;address&amp;quot;: {&lt;/span&gt;
11879 &lt;span class=&quot;go&quot;&gt;    &amp;quot;street&amp;quot;: &amp;quot;99 Mountain Rd.&amp;quot;,&lt;/span&gt;
11880 &lt;span class=&quot;go&quot;&gt;    &amp;quot;street2&amp;quot;: &amp;quot;&amp;quot;,&lt;/span&gt;
11881 &lt;span class=&quot;go&quot;&gt;    &amp;quot;city&amp;quot;: &amp;quot;Concord&amp;quot;,&lt;/span&gt;
11882 &lt;span class=&quot;go&quot;&gt;    &amp;quot;state&amp;quot;: &amp;quot;NH&amp;quot;,&lt;/span&gt;
11883 &lt;span class=&quot;go&quot;&gt;    &amp;quot;zipcode&amp;quot;: &amp;quot;03301&amp;quot;&lt;/span&gt;
11884 &lt;span class=&quot;go&quot;&gt;  }&lt;/span&gt;
11885 &lt;span class=&quot;go&quot;&gt;}&lt;/span&gt;
11886 &lt;/pre&gt;&lt;/div&gt;
11887 
11888 &lt;p&gt;The program works the same as before, but now you can see that a single &lt;code&gt;Employee&lt;/code&gt; object can be created from its &lt;code&gt;id&lt;/code&gt; and display its dictionary representation.&lt;/p&gt;
11889 &lt;p&gt;Take a closer look at the &lt;code&gt;Employee&lt;/code&gt; class:&lt;/p&gt;
11890 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In employees.py&lt;/span&gt;
11891 
11892 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AsDictionaryMixin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11893     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11894         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;
11895         &lt;span class=&quot;n&quot;&gt;info&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee_database&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_employee_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11896         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11897         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_employee_address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11898         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_role&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_role&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;role&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
11899         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_payroll&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11900 
11901     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11902         &lt;span class=&quot;n&quot;&gt;duties&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_role&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;perform_duties&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11903         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Employee &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.id}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;:&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11904         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;- &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{duties}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11905         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11906         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_payroll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;track_work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11907 
11908     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11909         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_payroll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11910 &lt;/pre&gt;&lt;/div&gt;
11911 
11912 &lt;p&gt;The &lt;code&gt;Employee&lt;/code&gt; class is a composite that contains multiple objects providing different functionality. It contains an &lt;code&gt;Address&lt;/code&gt; that implements all the functionality related to where the employee lives.&lt;/p&gt;
11913 &lt;p&gt;&lt;code&gt;Employee&lt;/code&gt; also contains a productivity role provided by the &lt;code&gt;productivity&lt;/code&gt; module, and a payroll policy provided by the &lt;code&gt;hr&lt;/code&gt; module. These two objects provide implementations that are leveraged by the &lt;code&gt;Employee&lt;/code&gt; class to track work in the &lt;code&gt;.work()&lt;/code&gt; method and to calculate the payroll in the &lt;code&gt;.calculate_payroll()&lt;/code&gt; method.&lt;/p&gt;
11914 &lt;p&gt;You are using composition in two different ways. The &lt;code&gt;Address&lt;/code&gt; class provides additional data to &lt;code&gt;Employee&lt;/code&gt; where the role and payroll objects provide additional behavior.&lt;/p&gt;
11915 &lt;p&gt;Still, the relationship between &lt;code&gt;Employee&lt;/code&gt; and those objects is loosely coupled, which provides some interesting capabilities that you&amp;rsquo;ll see in the next section.&lt;/p&gt;
11916 &lt;h3 id=&quot;composition-to-change-run-time-behavior&quot;&gt;Composition to Change Run-Time Behavior&lt;/h3&gt;
11917 &lt;p&gt;Inheritance, as opposed to composition, is a tightly couple relationship. With inheritance, there is only one way to change and customize behavior. Method overriding is the only way to customize the behavior of a base class. This creates rigid designs that are difficult to change.&lt;/p&gt;
11918 &lt;p&gt;Composition, on the other hand, provides a loosely coupled relationship that enables flexible designs and can be used to change behavior at run-time.&lt;/p&gt;
11919 &lt;p&gt;Imagine you need to support a long-term disability (LTD) policy when calculating payroll. The policy states that an employee on LTD should be paid 60% of their weekly salary assuming 40 hours of work.&lt;/p&gt;
11920 &lt;p&gt;With an inheritance design, this can be a very difficult requirement to support. Adding it to the composition example is a lot easier. Let&amp;rsquo;s start by adding the policy class:&lt;/p&gt;
11921 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In hr.py&lt;/span&gt;
11922 
11923 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LTDPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11924     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11925         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_base_policy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;
11926 
11927     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;track_work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11928         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_check_base_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11929         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_base_policy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;track_work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11930 
11931     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11932         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_check_base_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11933         &lt;span class=&quot;n&quot;&gt;base_salary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_base_policy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11934         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base_salary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.6&lt;/span&gt;
11935 
11936     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;apply_to_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11937         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_base_policy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base_policy&lt;/span&gt;
11938 
11939     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_check_base_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11940         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_base_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
11941             &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;RuntimeError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Base policy missing&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11942 &lt;/pre&gt;&lt;/div&gt;
11943 
11944 &lt;p&gt;Notice that &lt;code&gt;LTDPolicy&lt;/code&gt; doesn&amp;rsquo;t inherit &lt;code&gt;PayrollPolicy&lt;/code&gt;, but implements the same interface. This is because the implementation is completely different, so we don&amp;rsquo;t want to inherit any of the &lt;code&gt;PayrollPolicy&lt;/code&gt; implementation.&lt;/p&gt;
11945 &lt;p&gt;The &lt;code&gt;LTDPolicy&lt;/code&gt; initializes &lt;code&gt;_base_policy&lt;/code&gt; to &lt;code&gt;None&lt;/code&gt;, and provides an internal &lt;code&gt;._check_base_policy()&lt;/code&gt; method that raises an exception if the &lt;code&gt;._base_policy&lt;/code&gt; has not been applied. Then, it provides a &lt;code&gt;.apply_to_policy()&lt;/code&gt; method to assign the &lt;code&gt;_base_policy&lt;/code&gt;.&lt;/p&gt;
11946 &lt;p&gt;The public interface first checks that the &lt;code&gt;_base_policy&lt;/code&gt; has been applied, and then implements the functionality in terms of that base policy. The &lt;code&gt;.track_work()&lt;/code&gt; method just delegates to the base policy, and &lt;code&gt;.calculate_payroll()&lt;/code&gt; uses it to calculate the &lt;code&gt;base_salary&lt;/code&gt; and then return the 60%.&lt;/p&gt;
11947 &lt;p&gt;You can now make a small change to the &lt;code&gt;Employee&lt;/code&gt; class:&lt;/p&gt;
11948 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In employees.py&lt;/span&gt;
11949 
11950 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Employee&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AsDictionaryMixin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11951     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11952         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;
11953         &lt;span class=&quot;n&quot;&gt;info&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee_database&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_employee_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11954         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;name&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11955         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_employee_address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11956         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_role&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_role&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;role&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
11957         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_payroll&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11958 
11959 
11960     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11961         &lt;span class=&quot;n&quot;&gt;duties&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_role&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;perform_duties&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11962         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Employee &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.id}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; - &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{self.name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;:&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11963         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;- &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{duties}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11964         &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11965         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_payroll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;track_work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11966 
11967     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11968         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_payroll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11969 
11970     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;apply_payroll_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
11971         &lt;span class=&quot;n&quot;&gt;new_policy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;apply_to_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11972         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_payroll&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new_policy&lt;/span&gt;
11973 &lt;/pre&gt;&lt;/div&gt;
11974 
11975 &lt;p&gt;You added an &lt;code&gt;.apply_payroll_policy()&lt;/code&gt; method that applies the existing payroll policy to the new policy and then substitutes it. You can now modify the program to apply the policy to an &lt;code&gt;Employee&lt;/code&gt; object:&lt;/p&gt;
11976 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# In program.py&lt;/span&gt;
11977 
11978 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hr&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LTDPolicy&lt;/span&gt;
11979 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;productivity&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;
11980 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;employees&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee_database&lt;/span&gt;
11981 
11982 &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employee_database&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;
11983 
11984 &lt;span class=&quot;n&quot;&gt;sales_employee&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
11985 &lt;span class=&quot;n&quot;&gt;ltd_policy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LTDPolicy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
11986 &lt;span class=&quot;n&quot;&gt;sales_employee&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;apply_payroll_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ltd_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11987 
11988 &lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11989 &lt;span class=&quot;n&quot;&gt;calculate_payroll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;employees&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
11990 &lt;/pre&gt;&lt;/div&gt;
11991 
11992 &lt;p&gt;The program accesses &lt;code&gt;sales_employee&lt;/code&gt;, which is located at index &lt;code&gt;2&lt;/code&gt;, creates the &lt;code&gt;LTDPolicy&lt;/code&gt; object, and applies the policy to the employee. When &lt;code&gt;.calculate_payroll()&lt;/code&gt; is called, the change is reflected. You can run the program to evaluate the output:&lt;/p&gt;
11993 &lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python program.py
11994 
11995 &lt;span class=&quot;go&quot;&gt;Tracking Employee Productivity&lt;/span&gt;
11996 &lt;span class=&quot;go&quot;&gt;==============================&lt;/span&gt;
11997 &lt;span class=&quot;go&quot;&gt;Employee 1 - Mary Poppins:&lt;/span&gt;
11998 &lt;span class=&quot;go&quot;&gt;- screams and yells for 40 hours.&lt;/span&gt;
11999 
12000 &lt;span class=&quot;go&quot;&gt;Employee 2 - John Smith:&lt;/span&gt;
12001 &lt;span class=&quot;go&quot;&gt;- Does paperwork for 40 hours.&lt;/span&gt;
12002 
12003 &lt;span class=&quot;go&quot;&gt;Employee 3 - Kevin Bacon:&lt;/span&gt;
12004 &lt;span class=&quot;go&quot;&gt;- Expends 40 hours on the phone.&lt;/span&gt;
12005 
12006 &lt;span class=&quot;go&quot;&gt;Employee 4 - Jane Doe:&lt;/span&gt;
12007 &lt;span class=&quot;go&quot;&gt;- Manufactures gadgets for 40 hours.&lt;/span&gt;
12008 
12009 &lt;span class=&quot;go&quot;&gt;Employee 5 - Robin Williams:&lt;/span&gt;
12010 &lt;span class=&quot;go&quot;&gt;- Does paperwork for 40 hours.&lt;/span&gt;
12011 
12012 
12013 &lt;span class=&quot;go&quot;&gt;Calculating Payroll&lt;/span&gt;
12014 &lt;span class=&quot;go&quot;&gt;===================&lt;/span&gt;
12015 &lt;span class=&quot;go&quot;&gt;Payroll for: 1 - Mary Poppins&lt;/span&gt;
12016 &lt;span class=&quot;go&quot;&gt;- Check amount: 3000&lt;/span&gt;
12017 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
12018 &lt;span class=&quot;go&quot;&gt;121 Admin Rd.&lt;/span&gt;
12019 &lt;span class=&quot;go&quot;&gt;Concord, NH 03301&lt;/span&gt;
12020 
12021 &lt;span class=&quot;go&quot;&gt;Payroll for: 2 - John Smith&lt;/span&gt;
12022 &lt;span class=&quot;go&quot;&gt;- Check amount: 1500&lt;/span&gt;
12023 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
12024 &lt;span class=&quot;go&quot;&gt;67 Paperwork Ave&lt;/span&gt;
12025 &lt;span class=&quot;go&quot;&gt;Manchester, NH 03101&lt;/span&gt;
12026 
12027 &lt;span class=&quot;go&quot;&gt;Payroll for: 3 - Kevin Bacon&lt;/span&gt;
12028 &lt;span class=&quot;go&quot;&gt;- Check amount: 1080.0&lt;/span&gt;
12029 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
12030 &lt;span class=&quot;go&quot;&gt;15 Rose St&lt;/span&gt;
12031 &lt;span class=&quot;go&quot;&gt;Apt. B-1&lt;/span&gt;
12032 &lt;span class=&quot;go&quot;&gt;Concord, NH 03301&lt;/span&gt;
12033 
12034 &lt;span class=&quot;go&quot;&gt;Payroll for: 4 - Jane Doe&lt;/span&gt;
12035 &lt;span class=&quot;go&quot;&gt;- Check amount: 600&lt;/span&gt;
12036 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
12037 &lt;span class=&quot;go&quot;&gt;39 Sole St.&lt;/span&gt;
12038 &lt;span class=&quot;go&quot;&gt;Concord, NH 03301&lt;/span&gt;
12039 
12040 &lt;span class=&quot;go&quot;&gt;Payroll for: 5 - Robin Williams&lt;/span&gt;
12041 &lt;span class=&quot;go&quot;&gt;- Check amount: 360&lt;/span&gt;
12042 &lt;span class=&quot;go&quot;&gt;- Sent to:&lt;/span&gt;
12043 &lt;span class=&quot;go&quot;&gt;99 Mountain Rd.&lt;/span&gt;
12044 &lt;span class=&quot;go&quot;&gt;Concord, NH 03301&lt;/span&gt;
12045 &lt;/pre&gt;&lt;/div&gt;
12046 
12047 &lt;p&gt;The check amount for employee Kevin Bacon, who is the sales employee, is now for $1080 instead of $1800. That&amp;rsquo;s because the &lt;code&gt;LTDPolicy&lt;/code&gt; has been applied to the salary.&lt;/p&gt;
12048 &lt;p&gt;As you can see, you were able to support the changes just by adding a new policy and modifying a couple interfaces. This is the kind of flexibility that policy design based on composition gives you.&lt;/p&gt;
12049 &lt;h3 id=&quot;choosing-between-inheritance-and-composition-in-python_1&quot;&gt;Choosing Between Inheritance and Composition in Python&lt;/h3&gt;
12050 &lt;p&gt;Python, as an object oriented programming language, supports both inheritance and composition. You saw that inheritance is best used to model an &lt;strong&gt;is a&lt;/strong&gt; relationship, whereas composition models a &lt;strong&gt;has a&lt;/strong&gt; relationship.&lt;/p&gt;
12051 &lt;p&gt;Sometimes, it&amp;rsquo;s hard to see what the relationship between two classes should be, but you can follow these guidelines:&lt;/p&gt;
12052 &lt;ul&gt;
12053 &lt;li&gt;
12054 &lt;p&gt;&lt;strong&gt;Use inheritance over composition in Python&lt;/strong&gt; to model a clear &lt;strong&gt;is a&lt;/strong&gt; relationship. First, justify the relationship between the derived class and its base. Then, reverse the relationship and try to justify it. If you can justify the relationship in both directions, then you should not use inheritance between them.&lt;/p&gt;
12055 &lt;/li&gt;
12056 &lt;li&gt;
12057 &lt;p&gt;&lt;strong&gt;Use inheritance over composition in Python&lt;/strong&gt; to leverage both the interface and implementation of the base class.&lt;/p&gt;
12058 &lt;/li&gt;
12059 &lt;li&gt;
12060 &lt;p&gt;&lt;strong&gt;Use inheritance over composition in Python&lt;/strong&gt; to provide &lt;strong&gt;mixin&lt;/strong&gt; features to several unrelated classes when there is only one implementation of that feature.&lt;/p&gt;
12061 &lt;/li&gt;
12062 &lt;li&gt;
12063 &lt;p&gt;&lt;strong&gt;Use composition over inheritance in Python&lt;/strong&gt; to model a &lt;strong&gt;has a&lt;/strong&gt; relationship that leverages the implementation of the component class.&lt;/p&gt;
12064 &lt;/li&gt;
12065 &lt;li&gt;
12066 &lt;p&gt;&lt;strong&gt;Use composition over inheritance in Python&lt;/strong&gt; to create components that can be reused by multiple classes in your Python applications.&lt;/p&gt;
12067 &lt;/li&gt;
12068 &lt;li&gt;
12069 &lt;p&gt;&lt;strong&gt;Use composition over inheritance in Python&lt;/strong&gt; to implement groups of behaviors and policies that can be applied interchangeably to other classes to customize their behavior.&lt;/p&gt;
12070 &lt;/li&gt;
12071 &lt;li&gt;
12072 &lt;p&gt;&lt;strong&gt;Use composition over inheritance in Python&lt;/strong&gt; to enable run-time behavior changes without affecting existing classes.&lt;/p&gt;
12073 &lt;/li&gt;
12074 &lt;/ul&gt;
12075 &lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
12076 &lt;p&gt;You explored &lt;strong&gt;inheritance and composition in Python&lt;/strong&gt;. You learned about the type of relationships that inheritance and composition create. You also went through a series of exercises to understand how inheritance and composition are implemented in Python.&lt;/p&gt;
12077 &lt;p&gt;In this article, you learned how to:&lt;/p&gt;
12078 &lt;ul&gt;
12079 &lt;li&gt;Use inheritance to express an &lt;strong&gt;is a&lt;/strong&gt; relationship between two classes&lt;/li&gt;
12080 &lt;li&gt;Evaluate if inheritance is the right relationship&lt;/li&gt;
12081 &lt;li&gt;Use multiple inheritance in Python and evaluate Python&amp;rsquo;s MRO to troubleshoot multiple inheritance problems&lt;/li&gt;
12082 &lt;li&gt;Extend classes with mixins and reuse their implementation&lt;/li&gt;
12083 &lt;li&gt;Use composition to express a &lt;strong&gt;has a&lt;/strong&gt; relationship between two classes&lt;/li&gt;
12084 &lt;li&gt;Provide flexible designs using composition&lt;/li&gt;
12085 &lt;li&gt;Reuse existing code through policy design based on composition&lt;/li&gt;
12086 &lt;/ul&gt;
12087 &lt;h2 id=&quot;recommended-reading&quot;&gt;Recommended Reading&lt;/h2&gt;
12088 &lt;p&gt;Here are some books and articles that further explore object oriented design and can be useful to help you understand the correct use of inheritance and composition in Python or other languages:&lt;/p&gt;
12089 &lt;ul&gt;
12090 &lt;li&gt;&lt;a href=&quot;https://realpython.com/asins/B000SEIBB8&quot;&gt;Design Patterns: Elements of Reusable Object-Oriented Software&lt;/a&gt;&lt;/li&gt;
12091 &lt;li&gt;&lt;a href=&quot;https://realpython.com/asins/B00AA36RZY&quot;&gt;Head First Design Patterns: A Brain-Friendly Guide&lt;/a&gt;&lt;/li&gt;
12092 &lt;li&gt;&lt;a href=&quot;https://realpython.com/asins/B001GSTOAM&quot;&gt;Clean Code: A Handbook of Agile Software Craftsmanship&lt;/a&gt;&lt;/li&gt;
12093 &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/SOLID&quot;&gt;SOLID Principles&lt;/a&gt;&lt;/li&gt;
12094 &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Liskov_substitution_principle&quot;&gt;Liskov Substitution Principle&lt;/a&gt;&lt;/li&gt;
12095 &lt;/ul&gt;
12096         &lt;hr /&gt;
12097         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
12098       </content>
12099     </entry>
12100   
12101     <entry>
12102       <title>11 Beginner Tips for Learning Python</title>
12103       <id>https://realpython.com/courses/python-beginner-tips/</id>
12104       <link href="https://realpython.com/courses/python-beginner-tips/"/>
12105       <updated>2019-08-06T14:00:00+00:00</updated>
12106       <summary>In this course, you&#39;ll see several learning strategies and tips that will help you jumpstart your journey towards becoming a rockstar Python programmer!</summary>
12107       <content type="html">
12108         &lt;p&gt;We are so excited that you have decided to embark on the journey of learning Python! One of the most common questions we receive from our readers is โ€œWhatโ€™s the best way to learn Python?โ€&lt;/p&gt;
12109 &lt;p&gt;The first step in learning any programming language is making sure that you understand how to learn. Learning how to learn is arguably the most critical skill involved in computer programming.&lt;/p&gt;
12110 &lt;p&gt;Why is knowing how to learn so important? Languages evolve, libraries are created, and tools are upgraded. Knowing how to learn will be essential to keeping up with these changes and becoming a successful programmer.&lt;/p&gt;
12111 &lt;p&gt;In this course, you&amp;rsquo;ll see several learning strategies that will help you jumpstart your journey towards becoming a rockstar Python programmer!&lt;/p&gt;
12112         &lt;hr /&gt;
12113         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
12114       </content>
12115     </entry>
12116   
12117     <entry>
12118       <title>What You Need to Know to Manage Users in Django Admin</title>
12119       <id>https://realpython.com/manage-users-in-django-admin/</id>
12120       <link href="https://realpython.com/manage-users-in-django-admin/"/>
12121       <updated>2019-08-05T14:00:00+00:00</updated>
12122       <summary>In this Python tutorial, you&#39;ll learn what you need to know to manage users in Django admin. Out of the box, Django admin doesn&#39;t enforce special restrictions on the user admin. This can lead to dangerous scenarios that might compromise your system.</summary>
12123       <content type="html">
12124         &lt;p&gt;User management in Django admin is a tricky subject. If you enforce too many permissions, then you might interfere with day-to-day operations. If you allow for permissions to be granted freely without supervision, then you put your system at risk.&lt;/p&gt;
12125 &lt;p&gt;Django provides a good authentication framework with tight integration to Django admin. Out of the box, Django admin does not enforce special restrictions on the user admin. This can lead to dangerous scenarios that might compromise your system.&lt;/p&gt;
12126 &lt;p&gt;Did you know staff users that manage other users in the admin can edit their own permissions? Did you know they can also make themselves superusers? There is nothing in Django admin that prevents that, so it&amp;rsquo;s up to you!&lt;/p&gt;
12127 &lt;p&gt;&lt;strong&gt;By the end of this tutorial, you&amp;rsquo;ll know how to protect your system:&lt;/strong&gt;&lt;/p&gt;
12128 &lt;ul&gt;
12129 &lt;li&gt;&lt;strong&gt;Protect against permission escalation&lt;/strong&gt; by preventing users from editing their own permissions&lt;/li&gt;
12130 &lt;li&gt;&lt;strong&gt;Keep permissions tidy and maintainable&lt;/strong&gt; by only forcing users to manage permissions only using groups&lt;/li&gt;
12131 &lt;li&gt;&lt;strong&gt;Prevent permissions from leaking through custom actions&lt;/strong&gt; by explicitly enforcing the necessary permissions&lt;/li&gt;
12132 &lt;/ul&gt;
12133 &lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
12134 &lt;p&gt;&lt;strong&gt;Follow Along:&lt;/strong&gt;&lt;/p&gt;
12135 &lt;p&gt;To follow along with this tutorial, it&amp;rsquo;s best to setup a small project to play with. If you aren&amp;rsquo;t sure how to do that, then check out &lt;a href=&quot;https://realpython.com/get-started-with-django-1/#hello-world&quot;&gt;Get Started With Django&lt;/a&gt;.&lt;/p&gt;
12136 &lt;p&gt;This tutorial also assumes a basic understanding of user management in Django. If you aren&amp;rsquo;t familiar with that, then check out &lt;a href=&quot;https://docs.djangoproject.com/en/2.2/topics/auth/&quot;&gt;the official documentation&lt;/a&gt;.&lt;/p&gt;
12137 &lt;/div&gt;
12138 &lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Free Bonus:&lt;/strong&gt; &lt;a href=&quot;#&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-django-resources-learing-guide&quot; data-focus=&quot;false&quot;&gt;Click here to get access to a free Django Learning Resources Guide (PDF)&lt;/a&gt; that shows you tips and tricks as well as common pitfalls to avoid when building Python + Django web applications.&lt;/p&gt;&lt;/div&gt;
12139 
12140 &lt;h2 id=&quot;model-permissions&quot;&gt;Model Permissions&lt;/h2&gt;
12141 &lt;p&gt;Permissions are tricky. If you don&amp;rsquo;t set permissions, then you put your system at risk of intruders, data leaks, and human errors. If you abuse permissions or use them too much, then you risk interfering with day-to-day operations.&lt;/p&gt;
12142 &lt;p&gt;Django comes with a built-in &lt;a href=&quot;https://docs.djangoproject.com/en/2.2/topics/auth/&quot;&gt;authentication system&lt;/a&gt;. The authentication system includes users, groups, and permissions.&lt;/p&gt;
12143 &lt;p&gt;When a model is created, Django will automatically create four &lt;a href=&quot;https://docs.djangoproject.com/en/2.2/topics/auth/default/#default-permissions&quot;&gt;default permissions&lt;/a&gt; for the following actions:&lt;/p&gt;
12144 &lt;ol&gt;
12145 &lt;li&gt;&lt;strong&gt;&lt;code&gt;add&lt;/code&gt;:&lt;/strong&gt; Users with this permission can add an instance of the model.&lt;/li&gt;
12146 &lt;li&gt;&lt;strong&gt;&lt;code&gt;delete&lt;/code&gt;:&lt;/strong&gt; Users with this permission can delete an instance of the model.&lt;/li&gt;
12147 &lt;li&gt;&lt;strong&gt;&lt;code&gt;change&lt;/code&gt;:&lt;/strong&gt; Users with this permission can update an instance of the model.&lt;/li&gt;
12148 &lt;li&gt;&lt;strong&gt;&lt;code&gt;view&lt;/code&gt;:&lt;/strong&gt; Users with this permission can view instances of this model. This permission was a much anticipated one, and it was finally added in Django 2.1.&lt;/li&gt;
12149 &lt;/ol&gt;
12150 &lt;p&gt;Permission names follow a very specific naming convention:  &lt;code&gt;&amp;lt;app&amp;gt;.&amp;lt;action&amp;gt;_&amp;lt;modelname&amp;gt;&lt;/code&gt;.&lt;/p&gt;
12151 &lt;p&gt;Let&amp;rsquo;s break that down:&lt;/p&gt;
12152 &lt;ul&gt;
12153 &lt;li&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;app&amp;gt;&lt;/code&gt;&lt;/strong&gt; is the name of the app. For example, the &lt;code&gt;User&lt;/code&gt; model is imported from the &lt;code&gt;auth&lt;/code&gt; app (&lt;code&gt;django.contrib.auth&lt;/code&gt;).&lt;/li&gt;
12154 &lt;li&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;action&amp;gt;&lt;/code&gt;&lt;/strong&gt; is one of the actions above (&lt;code&gt;add&lt;/code&gt;, &lt;code&gt;delete&lt;/code&gt;, &lt;code&gt;change&lt;/code&gt;, or &lt;code&gt;view&lt;/code&gt;).&lt;/li&gt;
12155 &lt;li&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;modelname&amp;gt;&lt;/code&gt;&lt;/strong&gt; is the name of the model, in all lowercase letters.&lt;/li&gt;
12156 &lt;/ul&gt;
12157 &lt;p&gt;Knowing this naming convention can help you manage permissions more easily. For example, the name of the permission to change a user is &lt;code&gt;auth.change_user&lt;/code&gt;.&lt;/p&gt;
12158 &lt;h3 id=&quot;how-to-check-permissions&quot;&gt;How to Check Permissions&lt;/h3&gt;
12159 &lt;p&gt;Model permissions are granted to users or groups. To check if a user has a certain permission, you can do the following:&lt;/p&gt;
12160 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.models&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;
12161 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objects&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;haki&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12162 &lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;has_perm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;auth.change_user&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12163 &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;False&lt;/span&gt;
12164 &lt;/pre&gt;&lt;/div&gt;
12165 
12166 &lt;p&gt;It&amp;rsquo;s worth mentioning that &lt;a href=&quot;https://github.com/django/django/blob/bf9e0e342da3ed2f74ee0ec34e75bdcbedde40a9/django/contrib/auth/models.py#L255&quot;&gt;&lt;code&gt;.has_perm()&lt;/code&gt; will always return &lt;code&gt;True&lt;/code&gt;&lt;/a&gt; for active superuser, even if the permission doesn&amp;rsquo;t really exist:&lt;/p&gt;
12167 &lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.models&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;
12168 &lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;superuser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objects&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_superuser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
12169 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;superhaki&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
12170 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;me@hakibenita.com&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
12171 &lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;secret&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
12172 &lt;span class=&quot;go&quot;&gt;)&lt;/span&gt;
12173 &lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;superuser&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;has_perm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;does.not.exist&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12174 &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;True&lt;/span&gt;
12175 &lt;/pre&gt;&lt;/div&gt;
12176 
12177 &lt;p&gt;As you can see, when you&amp;rsquo;re checking permissions for a superuser, the permissions are not really being checked.&lt;/p&gt;
12178 &lt;h3 id=&quot;how-to-enforce-permissions&quot;&gt;How to Enforce Permissions&lt;/h3&gt;
12179 &lt;p&gt;Django models don&amp;rsquo;t enforce permissions themselves. The only place permissions are enforced out of the box by default is Django Admin.&lt;/p&gt;
12180 &lt;p&gt;The reason models don&amp;rsquo;t enforce permissions is that, normally, the model is unaware of the user performing the action. In Django apps, the user is usually obtained from the request. This is why, most of the time, permissions are enforced at the view layer.&lt;/p&gt;
12181 &lt;p&gt;For example, to prevent a user without view permissions on the &lt;code&gt;User&lt;/code&gt; model from accessing a view that shows user information, do the following:&lt;/p&gt;
12182 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.core.exceptions&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PermissionDenied&lt;/span&gt;
12183 
12184 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;users_list_view&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12185     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;has_perm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;auth.view_user&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12186         &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PermissionDenied&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
12187 &lt;/pre&gt;&lt;/div&gt;
12188 
12189 &lt;p&gt;If the user making the request logged in and was authenticated, then &lt;a href=&quot;https://docs.djangoproject.com/en/2.2/ref/request-response/#django.http.HttpRequest.user&quot;&gt;&lt;code&gt;request.user&lt;/code&gt;&lt;/a&gt; will hold an instance of &lt;code&gt;User&lt;/code&gt;. If the user did not login, then &lt;code&gt;request.user&lt;/code&gt; will be an instance of &lt;a href=&quot;https://docs.djangoproject.com/en/2.2/ref/contrib/auth/#anonymoususer-object&quot;&gt;&lt;code&gt;AnonymousUser&lt;/code&gt;&lt;/a&gt;. This is a special object used by Django to indicate an unauthenticated user. Using &lt;code&gt;has_perm&lt;/code&gt; on &lt;code&gt;AnonymousUser&lt;/code&gt; will always return &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;
12190 &lt;p&gt;If the user making the request doesn&amp;rsquo;t have the &lt;code&gt;view_user&lt;/code&gt; permission, then you raise a &lt;code&gt;PermissionDenied&lt;/code&gt; exception, and a response with status &lt;code&gt;403&lt;/code&gt; is returned to the client.&lt;/p&gt;
12191 &lt;p&gt;To make it easier to enforce permissions in views, Django provides a shortcut &lt;a href=&quot;https://realpython.com/primer-on-python-decorators/&quot;&gt;decorator&lt;/a&gt; called &lt;a href=&quot;https://docs.djangoproject.com/en/2.2/topics/auth/default/#the-permission-required-decorator&quot;&gt;&lt;code&gt;permission_required&lt;/code&gt;&lt;/a&gt; that does the same thing:&lt;/p&gt;
12192 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.decorators&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;permission_required&lt;/span&gt;
12193 
12194 &lt;span class=&quot;nd&quot;&gt;@permission_required&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;auth.view_user&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12195 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;users_list_view&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12196     &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
12197 &lt;/pre&gt;&lt;/div&gt;
12198 
12199 &lt;p&gt;To enforce permissions in templates, you can access the current user permissions through a special template variable called &lt;a href=&quot;https://docs.djangoproject.com/en/2.2/topics/auth/default/#permissions&quot;&gt;&lt;code&gt;perms&lt;/code&gt;&lt;/a&gt;. For example, if you want to show a delete button only to users with delete permission, then do the following:&lt;/p&gt;
12200 &lt;div class=&quot;highlight htmldjango&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;{%&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;perms.auth.delete_user&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%}&lt;/span&gt;
12201 &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;Delete user!&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
12202 &lt;span class=&quot;cp&quot;&gt;{%&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;endif&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%}&lt;/span&gt;
12203 &lt;/pre&gt;&lt;/div&gt;
12204 
12205 &lt;p&gt;Some popular third party apps such as the &lt;a href=&quot;https://www.django-rest-framework.org/&quot;&gt;Django rest framework&lt;/a&gt; also provide &lt;a href=&quot;https://www.django-rest-framework.org/api-guide/permissions/#djangomodelpermissions&quot;&gt;useful integration with Django model permissions&lt;/a&gt;.&lt;/p&gt;
12206 &lt;h3 id=&quot;django-admin-and-model-permissions&quot;&gt;Django Admin and Model Permissions&lt;/h3&gt;
12207 &lt;p&gt;Django admin has a very &lt;a href=&quot;https://docs.djangoproject.com/en/2.2/topics/auth/default/#permissions-and-authorization&quot;&gt;tight integration&lt;/a&gt; with the built-in authentication system, and model permissions in particular. Out of the box, Django admin is enforcing model permissions:&lt;/p&gt;
12208 &lt;ul&gt;
12209 &lt;li&gt;If the user has no permissions on a model, then they won&amp;rsquo;t be able to see it or access it in the admin.&lt;/li&gt;
12210 &lt;li&gt;If the user has view and change permissions on a model, then they will be able to view and update instances, but they won&amp;rsquo;t be able to add new instances or delete existing ones.&lt;/li&gt;
12211 &lt;/ul&gt;
12212 &lt;p&gt;With proper permissions in place, admin users are less likely to make mistakes, and intruders will have a harder time causing harm.&lt;/p&gt;
12213 &lt;h2 id=&quot;implement-custom-business-roles-in-django-admin&quot;&gt;Implement Custom Business Roles in Django Admin&lt;/h2&gt;
12214 &lt;p&gt;One of the most vulnerable places in every app is the authentication system. In Django apps, this is the &lt;code&gt;User&lt;/code&gt; model. So, to better protect your app, you are going to start with the &lt;code&gt;User&lt;/code&gt; model.&lt;/p&gt;
12215 &lt;p&gt;First, you need to take control over the &lt;code&gt;User&lt;/code&gt; model admin page. Django already comes with a very nice admin page to manage users. To take advantage of that great work, you are going to extend the built-in &lt;code&gt;User&lt;/code&gt; admin model.&lt;/p&gt;
12216 &lt;h3 id=&quot;setup-a-custom-user-admin&quot;&gt;Setup: A Custom User Admin&lt;/h3&gt;
12217 &lt;p&gt;To provide a custom admin for the &lt;code&gt;User&lt;/code&gt; model, you need to unregister the existing model admin provided by Django, and register one of your own:&lt;/p&gt;
12218 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;admin&lt;/span&gt;
12219 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.models&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;
12220 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.admin&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UserAdmin&lt;/span&gt;
12221 
12222 &lt;span class=&quot;c1&quot;&gt;# Unregister the provided model admin&lt;/span&gt;
12223 &lt;span class=&quot;n&quot;&gt;admin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;site&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unregister&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12224 
12225 &lt;span class=&quot;c1&quot;&gt;# Register out own model admin, based on the default UserAdmin&lt;/span&gt;
12226 &lt;span class=&quot;nd&quot;&gt;@admin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;register&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12227 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CustomUserAdmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UserAdmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12228     &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
12229 &lt;/pre&gt;&lt;/div&gt;
12230 
12231 &lt;p&gt;Your &lt;code&gt;CustomUserAdmin&lt;/code&gt; is extending Django&amp;rsquo;s &lt;code&gt;UserAdmin&lt;/code&gt;. You did that so you can take advantage of all the work already done by the Django developers.&lt;/p&gt;
12232 &lt;p&gt;At this point, if you log into your Django admin at &lt;code&gt;http://127.0.0.1:8000/admin/auth/user&lt;/code&gt;, you should see the user admin unchanged:&lt;/p&gt;
12233 &lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/django-bare-boned-user-admin.4ac55297d529.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/django-bare-boned-user-admin.4ac55297d529.png&quot; width=&quot;861&quot; height=&quot;671&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/django-bare-boned-user-admin.4ac55297d529.png&amp;amp;w=215&amp;amp;sig=602db70adb2b1b9f4747c90ae4ba0c6e4860f4ad 215w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/django-bare-boned-user-admin.4ac55297d529.png&amp;amp;w=430&amp;amp;sig=c425295f2d8e0abb534dfe50a5f84dc40876726b 430w, https://files.realpython.com/media/django-bare-boned-user-admin.4ac55297d529.png 861w&quot; sizes=&quot;75vw&quot; alt=&quot;Django bare boned user admin&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
12234 &lt;p&gt;By extending &lt;code&gt;UserAdmin&lt;/code&gt;, you are able to use all the built-in features provided by Django admin.&lt;/p&gt;
12235 &lt;h3 id=&quot;prevent-update-of-fields&quot;&gt;Prevent Update of Fields&lt;/h3&gt;
12236 &lt;p&gt;Unattended admin forms are a prime candidate for horrible mistakes. A staff user can easily update a model instance through the admin in a way the app does not expect. Most of the time, the user won&amp;rsquo;t even notice something is wrong. Such mistakes are usually very hard to track down and fix.&lt;/p&gt;
12237 &lt;p&gt;To prevent such mistakes from happening, you can prevent admin users from modifying certain fields in the model.&lt;/p&gt;
12238 &lt;p&gt;If you want to prevent any user, including superusers, from updating a field, you can mark the field as read only. For example, the field &lt;code&gt;date_joined&lt;/code&gt; is set when a user registers. This information should never be changed by any user, so you mark it as read only:&lt;/p&gt;
12239 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;admin&lt;/span&gt;
12240 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.models&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;
12241 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.admin&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UserAdmin&lt;/span&gt;
12242 
12243 &lt;span class=&quot;nd&quot;&gt;@admin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;register&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12244 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CustomUserAdmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UserAdmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12245     &lt;span class=&quot;n&quot;&gt;readonly_fields&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
12246         &lt;span class=&quot;s1&quot;&gt;&amp;#39;date_joined&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
12247     &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
12248 &lt;/pre&gt;&lt;/div&gt;
12249 
12250 &lt;p&gt;When a field is added to &lt;code&gt;readonly_fields&lt;/code&gt;, it will not be editable in the admin default change form. When a field is marked as read only, Django will render the input element as disabled.&lt;/p&gt;
12251 &lt;p&gt;But, what if you want to prevent only some users from updating a field?&lt;/p&gt;
12252 &lt;h3 id=&quot;conditionally-prevent-update-of-fields&quot;&gt;Conditionally Prevent Update of Fields&lt;/h3&gt;
12253 &lt;p&gt;Sometimes it&amp;rsquo;s useful to update fields directly in the admin. But you don&amp;rsquo;t want to let any user do it: you want to allow only superusers to do it.&lt;/p&gt;
12254 &lt;p&gt;Let&amp;rsquo;s say you want to prevent non-superusers from changing a user&amp;rsquo;s username. To do that, you need to modify the change form generated by Django, and disable the username field based on the current user:&lt;/p&gt;
12255 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;admin&lt;/span&gt;
12256 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.models&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;
12257 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.admin&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UserAdmin&lt;/span&gt;
12258 
12259 &lt;span class=&quot;nd&quot;&gt;@admin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;register&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12260 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CustomUserAdmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UserAdmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12261     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12262         &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12263         &lt;span class=&quot;n&quot;&gt;is_superuser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_superuser&lt;/span&gt;
12264 
12265 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_superuser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
12266 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;base_fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;username&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;disabled&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;
12267 &lt;/span&gt;
12268         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt;
12269 &lt;/pre&gt;&lt;/div&gt;
12270 
12271 &lt;p&gt;Let&amp;rsquo;s break it down:&lt;/p&gt;
12272 &lt;ul&gt;
12273 &lt;li&gt;To make adjustments to the form, you override &lt;a href=&quot;https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_form&quot;&gt;&lt;code&gt;get_form()&lt;/code&gt;&lt;/a&gt;. This function is used by Django to generate a default change form for a model.&lt;/li&gt;
12274 &lt;li&gt;To conditionally disable the field, you first fetch the default form generated by Django, and then if the user is not a superuser, disable the username field.&lt;/li&gt;
12275 &lt;/ul&gt;
12276 &lt;p&gt;Now, when a non-superuser tries to edit a user, the username field will be disabled. Any attempt to modify the username through Django Admin will fail. When a superuser tries to edit the user, the username field will be editable and behave as expected.&lt;/p&gt;
12277 &lt;h3 id=&quot;prevent-non-superusers-from-granting-superuser-rights&quot;&gt;Prevent Non-Superusers From Granting Superuser Rights&lt;/h3&gt;
12278 &lt;p&gt;Superuser is a very strong permission that should not be granted lightly. However, any user with a change permission on the &lt;code&gt;User&lt;/code&gt; model can make any user a superuser, including themselves. This goes against the whole purpose of the permission system, so you want to close this hole.&lt;/p&gt;
12279 &lt;p&gt;Based on the previous example, to prevent non-superusers from making themselves superusers, you add the following restriction:&lt;/p&gt;
12280 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;typing&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Set&lt;/span&gt;
12281 
12282 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;admin&lt;/span&gt;
12283 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.models&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;
12284 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.admin&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UserAdmin&lt;/span&gt;
12285 
12286 &lt;span class=&quot;nd&quot;&gt;@admin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;register&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12287 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CustomUserAdmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UserAdmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12288     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12289         &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12290         &lt;span class=&quot;n&quot;&gt;is_superuser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_superuser&lt;/span&gt;
12291         &lt;span class=&quot;n&quot;&gt;disabled_fields&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# type: Set[str]&lt;/span&gt;
12292 
12293         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_superuser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
12294             &lt;span class=&quot;n&quot;&gt;disabled_fields&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
12295                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;username&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
12296 &lt;span class=&quot;hll&quot;&gt;                &lt;span class=&quot;s1&quot;&gt;&amp;#39;is_superuser&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
12297 &lt;/span&gt;            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
12298 
12299         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;disabled_fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
12300             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;base_fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
12301                 &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;base_fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;disabled&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;
12302 
12303         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt;
12304 &lt;/pre&gt;&lt;/div&gt;
12305 
12306 &lt;p&gt;In addition to the previous example, you made the following additions:&lt;/p&gt;
12307 &lt;ol&gt;
12308 &lt;li&gt;
12309 &lt;p&gt;You initialized an empty set &lt;code&gt;disabled_fields&lt;/code&gt; that will hold the fields to disable. &lt;code&gt;set&lt;/code&gt; is a data structure that holds unique values. It makes sense to use a set in this case, because you only need to disable a field once. The operator &lt;code&gt;|=&lt;/code&gt; is used to perform an in-place &lt;code&gt;OR&lt;/code&gt; update. For more information about sets, check out &lt;a href=&quot;https://realpython.com/python-sets/&quot;&gt;Sets in Python&lt;/a&gt;.&lt;/p&gt;
12310 &lt;/li&gt;
12311 &lt;li&gt;
12312 &lt;p&gt;Next, if the user is a superuser, you add two fields to the set (&lt;code&gt;username&lt;/code&gt; from the previous example, and &lt;code&gt;is_superuser&lt;/code&gt;). They will prevent non-superusers from making themselves superusers.&lt;/p&gt;
12313 &lt;/li&gt;
12314 &lt;li&gt;
12315 &lt;p&gt;Lastly, you iterate over the fields in the set, mark all of them as disabled, and return the form.&lt;/p&gt;
12316 &lt;/li&gt;
12317 &lt;/ol&gt;
12318 &lt;p&gt;&lt;strong&gt;Django User Admin Two-Step Form&lt;/strong&gt;&lt;/p&gt;
12319 &lt;p&gt;When you create a new user in Django admin, you go through a two-step form. In the first form, you fill in the username and password. In the second form, you update the rest of the fields.&lt;/p&gt;
12320 &lt;p&gt;This two-step process is unique to the &lt;code&gt;User&lt;/code&gt; model. To accommodate this unique process, you must verify that the field exists before you try to disable it. Otherwise, you might get a &lt;code&gt;KeyError&lt;/code&gt;. This is not necessary if you customize other model admins.&lt;/p&gt;
12321 &lt;p&gt;For more information about &lt;code&gt;KeyError&lt;/code&gt;, check out &lt;a href=&quot;https://realpython.com/python-keyerror/&quot;&gt;Python KeyError Exceptions and How to Handle Them&lt;/a&gt;.&lt;/p&gt;
12322 &lt;h3 id=&quot;grant-permissions-only-using-groups&quot;&gt;Grant Permissions Only Using Groups&lt;/h3&gt;
12323 &lt;p&gt;The way permissions are managed is very specific to each team, product, and company. I found that it&amp;rsquo;s easier to manage permissions in groups. In my own projects, I create groups for support, content editors, analysts, and so on. I found that managing permissions at the user level can be a real hassle. When new models are added, or when business requirements change, it&amp;rsquo;s tedious to update each individual user.&lt;/p&gt;
12324 &lt;p&gt;To manage permissions only using groups, you need to prevent users from granting permissions to specific users. Instead, you want to only allow associating users to groups. To do that, disable the field &lt;code&gt;user_permissions&lt;/code&gt; for all non-superusers:&lt;/p&gt;
12325 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;typing&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Set&lt;/span&gt;
12326 
12327 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;admin&lt;/span&gt;
12328 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.models&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;
12329 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.admin&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UserAdmin&lt;/span&gt;
12330 
12331 &lt;span class=&quot;nd&quot;&gt;@admin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;register&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12332 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CustomUserAdmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UserAdmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12333     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12334         &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12335         &lt;span class=&quot;n&quot;&gt;is_superuser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_superuser&lt;/span&gt;
12336         &lt;span class=&quot;n&quot;&gt;disabled_fields&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# type: Set[str]&lt;/span&gt;
12337 
12338         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_superuser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
12339             &lt;span class=&quot;n&quot;&gt;disabled_fields&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
12340                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;username&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
12341                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;is_superuser&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
12342 &lt;span class=&quot;hll&quot;&gt;                &lt;span class=&quot;s1&quot;&gt;&amp;#39;user_permissions&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
12343 &lt;/span&gt;            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
12344 
12345         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;disabled_fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
12346             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;base_fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
12347                 &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;base_fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;disabled&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;
12348 
12349         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt;
12350 &lt;/pre&gt;&lt;/div&gt;
12351 
12352 &lt;p&gt;You used the exact same technique as in the previous sections to implement another business rule. In the next sections, you&amp;rsquo;re going to implement more complex business rules to protect your system.&lt;/p&gt;
12353 &lt;h3 id=&quot;prevent-non-superusers-from-editing-their-own-permissions&quot;&gt;Prevent Non-Superusers From Editing Their Own Permissions&lt;/h3&gt;
12354 &lt;p&gt;Strong users are often a weak spot. They possess strong permissions, and the potential damage they can cause is significant. To prevent permission escalation in case of intrusion, you can prevent users from editing their own permissions:&lt;/p&gt;
12355 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;typing&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Set&lt;/span&gt;
12356 
12357 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;admin&lt;/span&gt;
12358 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.models&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;
12359 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.admin&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UserAdmin&lt;/span&gt;
12360 
12361 &lt;span class=&quot;nd&quot;&gt;@admin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;register&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12362 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CustomUserAdmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UserAdmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12363     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12364         &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12365         &lt;span class=&quot;n&quot;&gt;is_superuser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_superuser&lt;/span&gt;
12366         &lt;span class=&quot;n&quot;&gt;disabled_fields&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# type: Set[str]&lt;/span&gt;
12367 
12368         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_superuser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
12369             &lt;span class=&quot;n&quot;&gt;disabled_fields&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
12370                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;username&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
12371                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;is_superuser&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
12372                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;user_permissions&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
12373             &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
12374 
12375         &lt;span class=&quot;c1&quot;&gt;# Prevent non-superusers from editing their own permissions&lt;/span&gt;
12376         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
12377 &lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_superuser&lt;/span&gt;
12378 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;
12379 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;
12380 &lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12381             &lt;span class=&quot;n&quot;&gt;disabled_fields&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
12382                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;is_staff&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
12383                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;is_superuser&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
12384                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;groups&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
12385                 &lt;span class=&quot;s1&quot;&gt;&amp;#39;user_permissions&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
12386             &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
12387 
12388         &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;disabled_fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
12389             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;base_fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
12390                 &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;base_fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;disabled&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;
12391 
12392         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;form&lt;/span&gt;
12393 &lt;/pre&gt;&lt;/div&gt;
12394 
12395 &lt;p&gt;The argument &lt;code&gt;obj&lt;/code&gt; is the instance of the object you are currently operating on:&lt;/p&gt;
12396 &lt;ul&gt;
12397 &lt;li&gt;&lt;strong&gt;When &lt;code&gt;obj&lt;/code&gt; is None&lt;/strong&gt;, the form is used to create a new user.&lt;/li&gt;
12398 &lt;li&gt;&lt;strong&gt;When &lt;code&gt;obj&lt;/code&gt; is not &lt;code&gt;None&lt;/code&gt;&lt;/strong&gt;, the form is used to edit an existing user.&lt;/li&gt;
12399 &lt;/ul&gt;
12400 &lt;p&gt;To check if the user making the request is operating on themselves, you compare &lt;code&gt;request.user&lt;/code&gt; with &lt;code&gt;obj&lt;/code&gt;. Because this is the user admin, &lt;code&gt;obj&lt;/code&gt; is either an instance of &lt;code&gt;User&lt;/code&gt;, or &lt;code&gt;None&lt;/code&gt;. When the user making the request, &lt;code&gt;request.user&lt;/code&gt;, is equal to &lt;code&gt;obj&lt;/code&gt;, then it means that the user is updating themselves. In this case, you disable all sensitive fields that can be used to gain permissions.&lt;/p&gt;
12401 &lt;p&gt;The ability to customize the form based on the object is very useful. It can be used to implement elaborate business roles.&lt;/p&gt;
12402 &lt;h3 id=&quot;override-permissions&quot;&gt;Override Permissions&lt;/h3&gt;
12403 &lt;p&gt;It can sometimes be useful to completely override the permissions in Django admin. A common scenario is when you use permissions in other places, and you don&amp;rsquo;t want staff users to make changes in the admin.&lt;/p&gt;
12404 &lt;p&gt;Django uses &lt;a href=&quot;https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.has_view_permission&quot;&gt;hooks for the four built-in permissions&lt;/a&gt;. Internally, the hooks use the current user&amp;rsquo;s permissions to make a decision. You can override these hooks, and provide a different decision.&lt;/p&gt;
12405 &lt;p&gt;To prevent staff users from deleting a model instance, regardless of their permissions, you can do the following:&lt;/p&gt;
12406 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;admin&lt;/span&gt;
12407 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.models&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;
12408 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.admin&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UserAdmin&lt;/span&gt;
12409 
12410 &lt;span class=&quot;nd&quot;&gt;@admin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;register&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12411 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CustomUserAdmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UserAdmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12412 &lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;has_delete_permission&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12413 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
12414 &lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
12415 
12416 &lt;p&gt;Just like with &lt;code&gt;get_form()&lt;/code&gt;, &lt;code&gt;obj&lt;/code&gt; is the instance you currently operate on:&lt;/p&gt;
12417 &lt;ul&gt;
12418 &lt;li&gt;&lt;strong&gt;When &lt;code&gt;obj&lt;/code&gt; is &lt;code&gt;None&lt;/code&gt;&lt;/strong&gt;, the user requested the list view.&lt;/li&gt;
12419 &lt;li&gt;&lt;strong&gt;When &lt;code&gt;obj&lt;/code&gt; is not &lt;code&gt;None&lt;/code&gt;&lt;/strong&gt;, the user requested the change view of a specific instance.&lt;/li&gt;
12420 &lt;/ul&gt;
12421 &lt;p&gt;Having the instance of the object in this hook is very useful for implementing object-level permissions for different types of actions. Here are other use cases:&lt;/p&gt;
12422 &lt;ul&gt;
12423 &lt;li&gt;Preventing changes during business hours&lt;/li&gt;
12424 &lt;li&gt;Implementing object-level permissions&lt;/li&gt;
12425 &lt;/ul&gt;
12426 &lt;h3 id=&quot;restrict-access-to-custom-actions&quot;&gt;Restrict Access to Custom Actions&lt;/h3&gt;
12427 &lt;p&gt;&lt;a href=&quot;https://docs.djangoproject.com/en/2.2/ref/contrib/admin/actions/#adding-actions-to-the-modeladmin&quot;&gt;Custom admin actions&lt;/a&gt; require special attention. Django is not familiar with them, so it can&amp;rsquo;t restrict access to them by default. A custom action will be accessible to any admin user with any permission on the model.&lt;/p&gt;
12428 &lt;p&gt;To illustrate, add a handy admin action to mark multiple users as active:&lt;/p&gt;
12429 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;admin&lt;/span&gt;
12430 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.models&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;
12431 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.admin&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UserAdmin&lt;/span&gt;
12432 
12433 &lt;span class=&quot;nd&quot;&gt;@admin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;register&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12434 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CustomUserAdmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UserAdmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12435     &lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
12436         &lt;span class=&quot;s1&quot;&gt;&amp;#39;activate_users&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
12437     &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
12438 
12439     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;activate_users&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queryset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12440         &lt;span class=&quot;n&quot;&gt;cnt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queryset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_active&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_active&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12441         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Activated &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; users.&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cnt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
12442     &lt;span class=&quot;n&quot;&gt;activate_users&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;short_description&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Activate Users&amp;#39;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# type: ignore&lt;/span&gt;
12443 &lt;/pre&gt;&lt;/div&gt;
12444 
12445 &lt;p&gt;Using this action, a staff user can mark one or more users, and activate them all at once. This is useful in all sorts of cases, such as if you had a bug in the registration process and needed to activate users in bulk.&lt;/p&gt;
12446 &lt;p&gt;This action updates user information, so you want only users with change permissions to be able to use it.&lt;/p&gt;
12447 &lt;p&gt;Django admin uses an internal function to get actions. To hide &lt;code&gt;activate_users()&lt;/code&gt; from users without change permission, override &lt;code&gt;get_actions()&lt;/code&gt;:&lt;/p&gt;
12448 &lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;admin&lt;/span&gt;
12449 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.models&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;
12450 &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;django.contrib.auth.admin&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UserAdmin&lt;/span&gt;
12451 
12452 &lt;span class=&quot;nd&quot;&gt;@admin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;register&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12453 &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CustomUserAdmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UserAdmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12454     &lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
12455         &lt;span class=&quot;s1&quot;&gt;&amp;#39;activate_users&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
12456     &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
12457 
12458     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;activate_users&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queryset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12459         &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;has_perm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;auth.change_user&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12460         &lt;span class=&quot;n&quot;&gt;cnt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queryset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_active&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_active&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12461         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Activated &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; users.&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cnt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
12462     &lt;span class=&quot;n&quot;&gt;activate_users&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;short_description&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Activate Users&amp;#39;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# type: ignore&lt;/span&gt;
12463 
12464     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12465         &lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
12466 &lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;has_perm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;auth.change_user&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
12467 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;activate_users&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
12468 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;
12469 &lt;/pre&gt;&lt;/div&gt;
12470 
12471 &lt;p&gt;&lt;code&gt;get_actions()&lt;/code&gt; returns an &lt;code&gt;OrderedDict&lt;/code&gt;. The key is the name of the action, and the value is the action function. To adjust the return value, you override the function, fetch the original value, and depending on the user permissions, remove the custom action &lt;code&gt;activate_users&lt;/code&gt; from the &lt;code&gt;dict&lt;/code&gt;. To be on the safe side, you assert the user permission in the action as well.&lt;/p&gt;
12472 &lt;p&gt;For staff users without &lt;code&gt;change_user()&lt;/code&gt; permissions, the action &lt;code&gt;activate_users&lt;/code&gt; will not appear in the actions dropdown.&lt;/p&gt;
12473 &lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
12474 &lt;p&gt;Django admin is a great tool for managing a Django project. Many teams rely on it to stay productive in managing day-to-day operations. If you use Django admin to perform operations on models, then it&amp;rsquo;s important to be aware of permissions. The techniques described in this article are useful for any model admin, not just the &lt;code&gt;User&lt;/code&gt; model.&lt;/p&gt;
12475 &lt;p&gt;In this tutorial, you protected your system by making the following adjustments in Django Admin:&lt;/p&gt;
12476 &lt;ul&gt;
12477 &lt;li&gt;You &lt;strong&gt;protected against permission escalation&lt;/strong&gt; by preventing users from editing their own permissions.&lt;/li&gt;
12478 &lt;li&gt;You &lt;strong&gt;kept permissions tidy and maintainable&lt;/strong&gt; by only forcing users to manage permissions only using groups.&lt;/li&gt;
12479 &lt;li&gt;You &lt;strong&gt;prevented permissions from leaking through custom actions&lt;/strong&gt; by explicitly enforcing the necessary permissions.&lt;/li&gt;
12480 &lt;/ul&gt;
12481 &lt;p&gt;Your &lt;code&gt;User&lt;/code&gt; model admin is now much safer than when you started!&lt;/p&gt;
12482         &lt;hr /&gt;
12483         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
12484       </content>
12485     </entry>
12486   
12487     <entry>
12488       <title>Dictionaries in Python</title>
12489       <id>https://realpython.com/courses/dictionaries-python/</id>
12490       <link href="https://realpython.com/courses/dictionaries-python/"/>
12491       <updated>2019-07-30T14:00:00+00:00</updated>
12492       <summary>In this course on Python dictionaries, you&#39;ll cover the basic characteristics of dictionaries and learn how to access and manage dictionary data. Once you&#39;ve finished this course, you&#39;ll have a good sense of when a dictionary is the appropriate data type to use and know how to use it.</summary>
12493       <content type="html">
12494         &lt;p&gt;Python provides a composite &lt;a href=&quot;https://realpython.com/python-data-types/&quot;&gt;data type&lt;/a&gt; called a &lt;strong&gt;dictionary&lt;/strong&gt;, which is similar to a &lt;a href=&quot;https://realpython.com/python-lists-tuples/&quot;&gt;list&lt;/a&gt; in that it is a collection of objects.&lt;/p&gt;
12495 &lt;p&gt;&lt;strong&gt;Here&amp;rsquo;s what you&amp;rsquo;ll learn in this course:&lt;/strong&gt; You&amp;rsquo;ll cover the basic characteristics of Python dictionaries and learn how to access and manage dictionary data.  Once you&amp;rsquo;ve finished this course, you&amp;rsquo;ll have a good sense of when a dictionary is the appropriate data type to use and know how to use it.&lt;/p&gt;
12496 &lt;p&gt;Dictionaries and lists share the following characteristics:&lt;/p&gt;
12497 &lt;ul&gt;
12498 &lt;li&gt;Both are mutable.&lt;/li&gt;
12499 &lt;li&gt;Both are dynamic.  They can grow and shrink as needed.&lt;/li&gt;
12500 &lt;li&gt;Both can be nested. A list can contain another list. A dictionary can contain another dictionary.  A dictionary can also contain a list, and vice versa.&lt;/li&gt;
12501 &lt;/ul&gt;
12502 &lt;p&gt;Dictionaries differ from lists primarily in how elements are accessed:&lt;/p&gt;
12503 &lt;ul&gt;
12504 &lt;li&gt;List elements are accessed by their position in the list, via indexing.&lt;/li&gt;
12505 &lt;li&gt;Dictionary elements are accessed via keys.&lt;/li&gt;
12506 &lt;/ul&gt;
12507         &lt;hr /&gt;
12508         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
12509       </content>
12510     </entry>
12511   
12512     <entry>
12513       <title>Logging in Python</title>
12514       <id>https://realpython.com/courses/logging-python/</id>
12515       <link href="https://realpython.com/courses/logging-python/"/>
12516       <updated>2019-07-23T14:00:00+00:00</updated>
12517       <summary>In this video course, you&#39;ll learn why and how to get started with Python&#39;s powerful logging module to meet the needs of beginners and enterprise teams alike.</summary>
12518       <content type="html">
12519         &lt;p&gt;Logging is a very useful tool in a programmerโ€™s toolbox. It can help you develop a better understanding of the flow of a program and discover scenarios that you might not even have thought of while developing.&lt;/p&gt;
12520 &lt;p&gt;Logs provide developers with an extra set of eyes that are constantly looking at the flow that an application is going through. They can store information, like which user or IP accessed the application. If an error occurs, then they can provide more insights than a stack trace by telling you what the state of the program was before it arrived at the line of code where the error occurred.&lt;/p&gt;
12521 &lt;p&gt;By logging useful data from the right places, you can not only debug errors easily but also use the data to analyze the performance of the application to plan for scaling or look at usage patterns to plan for marketing.&lt;/p&gt;
12522 &lt;p&gt;Python provides a logging system as a part of its standard library, so you can quickly add logging to your application. In this course, you&amp;rsquo;ll learn why using this module is the best way to add logging to your application as well as how to get started quickly, and you will get an introduction to some of the advanced features available.&lt;/p&gt;
12523         &lt;hr /&gt;
12524         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
12525       </content>
12526     </entry>
12527   
12528     <entry>
12529       <title>How to Write Pythonic Loops</title>
12530       <id>https://realpython.com/courses/how-to-write-pythonic-loops/</id>
12531       <link href="https://realpython.com/courses/how-to-write-pythonic-loops/"/>
12532       <updated>2019-07-16T14:00:00+00:00</updated>
12533       <summary>In this course, you&#39;ll see how you can make your loops more Pythonic if you&#39;re coming to Python from a C-style language. You&#39;ll learn how you can get the most out of using range(), xrange(), and enumerate(). You&#39;ll also see how you can avoid having to keep track of loop indexes manually.</summary>
12534       <content type="html">
12535         &lt;p&gt;One of the easiest ways to spot a developer who has a background in C-style languages and only recently picked up Python is to look at how they loop through a list. In this course, you&amp;rsquo;ll learn how to take a C-style (Java, PHP, C, C++) loop and turn it into the sort of loop a Python developer would write.&lt;/p&gt;
12536 &lt;p&gt;You can use these techniques to refactor your existing Python &lt;a href=&quot;https://realpython.com/python-for-loop/&quot;&gt;&lt;code&gt;for&lt;/code&gt; loops&lt;/a&gt; and &lt;a href=&quot;https://realpython.com/python-while-loop/&quot;&gt;&lt;code&gt;while&lt;/code&gt; loops&lt;/a&gt; in order to make them easier to read and more maintainable. You&amp;rsquo;ll learn how to use Python&amp;rsquo;s &lt;a href=&quot;https://realpython.com/python-range/&quot;&gt;&lt;code&gt;range()&lt;/code&gt;, &lt;code&gt;xrange()&lt;/code&gt;&lt;/a&gt;, and &lt;code&gt;enumerate()&lt;/code&gt; built-ins to refactor your loops and how to avoid having to keep track of loop indexes manually.&lt;/p&gt;
12537 &lt;p&gt;The main takeaways in this tutorial are that:&lt;/p&gt;
12538 &lt;ol&gt;
12539 &lt;li&gt;
12540 &lt;p&gt;Writing C-style loops in Python is considered not &lt;a href=&quot;https://realpython.com/learning-paths/writing-pythonic-code/&quot;&gt;Pythonic&lt;/a&gt;. Avoid managing loop indexes and stop conditions manually if possible.&lt;/p&gt;
12541 &lt;/li&gt;
12542 &lt;li&gt;
12543 &lt;p&gt;Pythonโ€™s &lt;code&gt;for&lt;/code&gt; loops are really โ€œfor eachโ€ loops that can iterate over items from a container or sequence directly.&lt;/p&gt;
12544 &lt;/li&gt;
12545 &lt;/ol&gt;
12546         &lt;hr /&gt;
12547         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
12548       </content>
12549     </entry>
12550   
12551     <entry>
12552       <title>Reading and Writing Files in Python</title>
12553       <id>https://realpython.com/courses/reading-and-writing-files-python/</id>
12554       <link href="https://realpython.com/courses/reading-and-writing-files-python/"/>
12555       <updated>2019-07-09T14:00:00+00:00</updated>
12556       <summary>In this course, you&#39;ll learn about reading and writing files in Python. You&#39;ll cover everything from what a file is made up of to which libraries can help you along that way. You&#39;ll also take a look at some basic scenarios of file usage as well as some advanced techniques.</summary>
12557       <content type="html">
12558         &lt;p&gt;In this course, you&amp;rsquo;ll learn about reading and writing files in Python. You&amp;rsquo;ll cover everything from what a file is made up of to which libraries can help you along that way. You&amp;rsquo;ll also take a look at some basic scenarios of file usage as well as some advanced techniques.&lt;/p&gt;
12559 &lt;p&gt;One of the most common tasks that you can do with Python is reading and writing files. Whether itโ€™s writing to a simple text file, reading a complicated server log, or even analyzing raw byte data, all of these situations require reading or writing a file.&lt;/p&gt;
12560 &lt;p&gt;By the end of this course, youโ€™ll know:&lt;/p&gt;
12561 &lt;ul&gt;
12562 &lt;li&gt;What makes up a file and why thatโ€™s important in Python&lt;/li&gt;
12563 &lt;li&gt;The basics of reading and writing files in Python&lt;/li&gt;
12564 &lt;li&gt;Some basic scenarios of reading and writing files&lt;/li&gt;
12565 &lt;/ul&gt;
12566 &lt;p&gt;This tutorial is mainly for beginner to intermediate Pythonistas, but there are some tips in here that more advanced programmers may appreciate as well.&lt;/p&gt;
12567         &lt;hr /&gt;
12568         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
12569       </content>
12570     </entry>
12571   
12572     <entry>
12573       <title>Functional Programming in Python</title>
12574       <id>https://realpython.com/courses/functional-programming-python/</id>
12575       <link href="https://realpython.com/courses/functional-programming-python/"/>
12576       <updated>2019-07-02T14:00:00+00:00</updated>
12577       <summary>In this course, you&#39;ll learn how to approach functional programming in Python. You&#39;ll cover what functional programming is, how you can use immutable data structures to represent your data, as well as how to use filter(), map(), and reduce().</summary>
12578       <content type="html">
12579         &lt;p&gt;In this course, you&amp;rsquo;ll learn how to approach functional programming in Python. You&amp;rsquo;ll start with the absolute basics of Functional Programming (FP). After that, you&amp;rsquo;ll see hands-on examples for common FP patterns available, like using immutable data structures and the &lt;code&gt;filter()&lt;/code&gt;, &lt;code&gt;map()&lt;/code&gt;, and &lt;code&gt;reduce()&lt;/code&gt; functions. You&amp;rsquo;ll end the course with actionable tips for parallelizing your code to make it run faster.&lt;/p&gt;
12580 &lt;p&gt;You&amp;rsquo;ll cover:&lt;/p&gt;
12581 &lt;ol&gt;
12582 &lt;li&gt;What functional programming is&lt;/li&gt;
12583 &lt;li&gt;How you can use immutable data structures to represent your data&lt;/li&gt;
12584 &lt;li&gt;How to use &lt;code&gt;filter()&lt;/code&gt;, &lt;code&gt;map()&lt;/code&gt;, and &lt;code&gt;reduce()&lt;/code&gt;&lt;/li&gt;
12585 &lt;li&gt;How to do parallel processing with &lt;code&gt;multiprocessing&lt;/code&gt; and &lt;code&gt;concurrent.futures&lt;/code&gt;&lt;/li&gt;
12586 &lt;/ol&gt;
12587         &lt;hr /&gt;
12588         &lt;p&gt;&lt;em&gt;[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ โ€“ Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
12589       </content>
12590     </entry>
12591   
12592 
12593 </feed>