<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mike Vincent</title>
    <description>The latest articles on DEV Community by Mike Vincent (@mike-vincent).</description>
    <link>https://dev.to/mike-vincent</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2239361%2Fb139bf47-9146-4e3e-bd3e-dc8c488010bd.jpeg</url>
      <title>DEV Community: Mike Vincent</title>
      <link>https://dev.to/mike-vincent</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mike-vincent"/>
    <language>en</language>
    <item>
      <title>Quark's Outlines: Python User-defined Functions</title>
      <dc:creator>Mike Vincent</dc:creator>
      <pubDate>Sat, 30 May 2026 12:33:50 +0000</pubDate>
      <link>https://dev.to/mike-vincent/quarks-outlines-python-user-defined-functions-4j72</link>
      <guid>https://dev.to/mike-vincent/quarks-outlines-python-user-defined-functions-4j72</guid>
      <description>&lt;h1&gt;
  
  
  Quark’s Outlines: Python User-Defined Functions
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Overview, Historical Timeline, Problems &amp;amp; Solutions&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  An Overview of Python User-Defined Functions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is a Python user-defined function?
&lt;/h3&gt;

&lt;p&gt;You can define your own function in Python using the &lt;code&gt;def&lt;/code&gt; keyword. A &lt;strong&gt;Python user-defined function&lt;/strong&gt; is made when you write a &lt;code&gt;def&lt;/code&gt; block in your code. When Python runs this block, it creates a function object.&lt;/p&gt;

&lt;p&gt;A function object has special parts. These include its name, its list of default values, and its code. It also keeps a link to the global names from the file where it was made. You can use this object to call the function later with any valid input.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you create named function objects with the &lt;code&gt;def&lt;/code&gt; keyword.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;friend&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello, &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Mike&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Hello, friend
# Hello, Mike
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function &lt;code&gt;greet&lt;/code&gt; is now a user-defined function. Python stores its name, code, and default values for later use.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are the special parts of a Python function?
&lt;/h3&gt;

&lt;p&gt;A Python function holds many facts about itself. These are called attributes. For example, &lt;code&gt;__name__&lt;/code&gt; holds the function name. &lt;code&gt;__defaults__&lt;/code&gt; is a tuple of default values. &lt;code&gt;__globals__&lt;/code&gt; holds the global names it can see. &lt;code&gt;__code__&lt;/code&gt; is a special object that stores the function’s bytecode.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python functions store their details in special attributes.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__defaults__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__code__&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_varnames&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# square
# (2,)
# ('x',)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These parts let Python understand and run your function later.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Historical Timeline of Python User-Defined Functions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Where do Python’s user-defined functions come from?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;User-defined functions in Python build on ideas from earlier languages. They let you name blocks of code and reuse them. Over time, Python added new parts to function objects—like closures, default values, and metadata—to make them more powerful.&lt;/p&gt;




&lt;h3&gt;
  
  
  People designed ways to name and reuse logic
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1958 —&lt;/strong&gt; &lt;strong&gt;Functions in LISP&lt;/strong&gt; let users define reusable logic blocks for the first time.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;1972 —&lt;/strong&gt; &lt;strong&gt;Procedures in C&lt;/strong&gt; gave a fixed way to declare and call code blocks with arguments.  &lt;/p&gt;


&lt;h3&gt;
  
  
  People added first-class functions to Python
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1991 —&lt;/strong&gt; &lt;strong&gt;Python adds &lt;code&gt;def&lt;/code&gt; keyword&lt;/strong&gt;, making user-defined function objects.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;1994 —&lt;/strong&gt; &lt;strong&gt;Python functions gain &lt;code&gt;__name__&lt;/code&gt; and &lt;code&gt;__doc__&lt;/code&gt;&lt;/strong&gt; attributes.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2000 —&lt;/strong&gt; &lt;strong&gt;Nested functions and closures added&lt;/strong&gt; with lexical scoping in Python 2.1.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2004 —&lt;/strong&gt; &lt;strong&gt;&lt;code&gt;__code__&lt;/code&gt; object exposed&lt;/strong&gt; to let functions be inspected.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2015 —&lt;/strong&gt; &lt;strong&gt;Function annotations added&lt;/strong&gt; for argument hints and return types.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2025 —&lt;/strong&gt; &lt;strong&gt;Function structure stable&lt;/strong&gt;, with introspection, closures, and metadata all supported.&lt;/p&gt;


&lt;h2&gt;
  
  
  Problems &amp;amp; Solutions with Python User-Defined Functions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;How do you use Python user-defined functions the right way?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python functions are blocks of code you can name and reuse. These examples show how functions are stored, how their defaults work, how you can inspect them, and how to understand what happens when they are created and called.&lt;/p&gt;


&lt;h3&gt;
  
  
  Problem: How do you reuse the same logic with different inputs in Python?
&lt;/h3&gt;

&lt;p&gt;You want to avoid repeating the same code. You want to give it a name, reuse it, and pass in different values each time. You need to define a function and call it when needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to make code reusable and easy to change.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you define a function using &lt;code&gt;def&lt;/code&gt; and call it with arguments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you reuse logic by defining and calling a function.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 5
# 5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;add&lt;/code&gt; function gives a simple way to reuse addition logic with any numbers.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you define a function with default values in Python?
&lt;/h3&gt;

&lt;p&gt;You want to make a function that works with or without an input. If no value is given, it should still run using a fallback value. You want a safe default when nothing is passed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want a function to work even if no input is given.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you set default argument values in your function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you define functions with default argument values.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;friend&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello, &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Sam&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Hello, friend
# Hello, Sam
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function uses the default value &lt;code&gt;"friend"&lt;/code&gt; unless another name is given.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you check what a function holds in Python?
&lt;/h3&gt;

&lt;p&gt;You want to look inside a function and see what Python remembers. You want to see its name, its default values, and the code it will run. You want to inspect the function object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You need to understand what a function knows about itself.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python gives function objects special attributes you can inspect.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you inspect functions using built-in attributes.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__defaults__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__code__&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_varnames&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# square
# (3,)
# ('x',)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These attributes show the function name, default values, and variable names.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you store a function for later use in Python?
&lt;/h3&gt;

&lt;p&gt;You want to save a function inside a variable or pass it to another function. You do not want to run it yet. You only want to store the logic until later.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to treat functions as values and pass them around.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you store function objects in variables or pass them to others.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you assign function objects to names and pass them around.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;shout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;upper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;shout&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;s&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# HELLO!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can treat the &lt;code&gt;shout&lt;/code&gt; function like any value and use it under another name.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you check a function's global names in Python?
&lt;/h3&gt;

&lt;p&gt;You are inside a function and want to know which global names it can see. You want to inspect where it was defined and what global values it uses. This helps when debugging or writing tools.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to trace which global names a function depends on.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python stores the global dictionary in the function’s &lt;code&gt;__globals__&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you inspect the global scope used by a function.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hi&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;say&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;say&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__globals__&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;message&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Hi
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;__globals__&lt;/code&gt; attribute shows the dictionary of names from the module.&lt;/p&gt;




&lt;h2&gt;
  
  
  Like, Comment, Share, and Subscribe
&lt;/h2&gt;

&lt;p&gt;Did you find this helpful? Let me know by clicking the like button below. I'd love to hear your thoughts in the comments, too! If you want to see more content like this, don't forget to subscribe. Thanks for reading!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;Mike Vincent&lt;/strong&gt;&lt;/a&gt; is an American software engineer and app developer from Los Angeles, California. &lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;More about Mike Vincent&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Quark's Outlines: Python Traceback Objects</title>
      <dc:creator>Mike Vincent</dc:creator>
      <pubDate>Sat, 23 May 2026 12:29:01 +0000</pubDate>
      <link>https://dev.to/mike-vincent/quarks-outlines-python-traceback-objects-3970</link>
      <guid>https://dev.to/mike-vincent/quarks-outlines-python-traceback-objects-3970</guid>
      <description>&lt;h1&gt;
  
  
  Quark’s Outlines: Python Traceback Objects
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Overview, Historical Timeline, Problems &amp;amp; Solutions&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  An Overview of Python Traceback Objects
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is a Python traceback object?
&lt;/h3&gt;

&lt;p&gt;When you run a Python program and an error happens, Python shows you a stack trace. A stack trace shows where the error came from. It tells you what function failed and which line caused it. A &lt;strong&gt;Python traceback object&lt;/strong&gt; holds that information.&lt;/p&gt;

&lt;p&gt;Each traceback object shows one step in the chain of function calls that led to the error. Python builds this chain as it unwinds the stack. The traceback includes the function’s frame, the line number, and the bytecode instruction that failed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you examine what went wrong using traceback objects.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;traceback&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ZeroDivisionError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;tb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__traceback__&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Line:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tb_lineno&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This traceback shows the line where the error happened. You can follow it to see how Python reached that point.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does a Python traceback object contain?
&lt;/h3&gt;

&lt;p&gt;A traceback object stores the frame that was active at the time of the error. It also stores the line number and the last instruction run in that frame. If there were more calls before the error, the traceback links to the next earlier traceback using &lt;code&gt;tb_next&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can think of a traceback as a trail of breadcrumbs that shows how the program moved from one function to the next before it failed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python traceback objects form a chain of frames leading to the error.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;b&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;b&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ZeroDivisionError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;tb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__traceback__&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Error in:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tb_frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f_code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prints the name of the function where the error happened. You can also walk backward using &lt;code&gt;tb_next&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Historical Timeline of Python Traceback Objects
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Where do Python’s traceback objects come from?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python traceback objects follow the idea of stack traces from earlier programming tools. They allow you to see what went wrong and where. This timeline shows how traceback support in Python grew over time.&lt;/p&gt;




&lt;h3&gt;
  
  
  People invented ways to trace error locations
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1960 —&lt;/strong&gt; &lt;strong&gt;Stack trace messages&lt;/strong&gt; first appeared in debugging tools for compiled languages.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;1970s —&lt;/strong&gt; &lt;strong&gt;Structured error reporting&lt;/strong&gt; became common in tools for ALGOL and Pascal.  &lt;/p&gt;


&lt;h3&gt;
  
  
  People designed traceback support in Python
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1991 —&lt;/strong&gt; &lt;strong&gt;Python added exceptions&lt;/strong&gt; and built-in stack traces for all uncaught errors.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;1994 —&lt;/strong&gt; &lt;strong&gt;Traceback module&lt;/strong&gt; added to let users walk through exceptions by hand.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2000 —&lt;/strong&gt; &lt;strong&gt;&lt;code&gt;__traceback__&lt;/code&gt; attribute&lt;/strong&gt; added to exceptions to hold traceback objects.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2010 —&lt;/strong&gt; &lt;strong&gt;Chained exceptions&lt;/strong&gt; allowed Python to preserve traceback links across &lt;code&gt;raise from&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2020 —&lt;/strong&gt; &lt;strong&gt;Traceback access improved&lt;/strong&gt; in tools like &lt;code&gt;traceback.TracebackException&lt;/code&gt; for structured error analysis.&lt;/p&gt;


&lt;h2&gt;
  
  
  Problems &amp;amp; Solutions with Python Traceback Objects
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;How do you use Python traceback objects the right way?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python traceback objects help you see where an error started, where it happened, and what Python was doing. When an error happens, Python builds a traceback. This traceback helps you trace the flow of execution and fix the problem. Each of these problems shows how to use traceback objects in real code.&lt;/p&gt;


&lt;h3&gt;
  
  
  Problem: How do you get the line where an error occurred in Python?
&lt;/h3&gt;

&lt;p&gt;You are running some code that raises an error. You want to find the exact line number where the problem happened. You want to get that number from Python, not by reading the stack trace on screen.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You need to get the line number of the error programmatically.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python traceback objects give the line number using &lt;code&gt;tb_lineno&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you read the error line number from a traceback object.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;nope&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ValueError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;tb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__traceback__&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Line number:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tb_lineno&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Line number: 2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;tb_lineno&lt;/code&gt; attribute gives you the exact line where Python stopped with an error.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you find the function where the error happened in Python?
&lt;/h3&gt;

&lt;p&gt;You are debugging a complex program. You want to know which function caused the error without reading the full traceback printout.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want the name of the function where the error occurred.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python traceback objects include the frame, which has the code object and its name.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you get the function name from the traceback’s frame.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;crash&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;crash&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ZeroDivisionError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;tb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__traceback__&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Function:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tb_frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f_code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Function: crash
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The frame tells you which function was running when the error happened.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you follow all the steps that led to an error in Python?
&lt;/h3&gt;

&lt;p&gt;You are debugging a failure that goes through several functions. You want to walk back through all of them and see which path Python took.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You need to trace the full call stack leading to an error.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python traceback objects form a chain using &lt;code&gt;tb_next&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you walk the full traceback using &lt;code&gt;tb_next&lt;/code&gt;.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;b&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;b&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;c&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;c&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ZeroDivisionError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;tb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__traceback__&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;tb&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Function:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tb_frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f_code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;tb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tb_next&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Function: c
# Function: b
# Function: a
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can walk the call chain backwards from the point of error using the &lt;code&gt;tb_next&lt;/code&gt; link.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you print a traceback yourself in Python?
&lt;/h3&gt;

&lt;p&gt;You are writing a tool that logs errors to a file. You want to include the full stack trace, not just the error message.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to format and print the traceback manually.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python has a &lt;code&gt;traceback&lt;/code&gt; module that can format tracebacks from objects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you print full tracebacks using the &lt;code&gt;traceback&lt;/code&gt; module.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;traceback&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/this/does/not/exist.txt&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;traceback&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print_tb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__traceback__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
#   File "example.py", line 3, in &amp;lt;module&amp;gt;
#     open("/this/does/not/exist.txt")
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives you a clean traceback without exiting the program.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you access traceback information from outside an &lt;code&gt;except&lt;/code&gt; block in Python?
&lt;/h3&gt;

&lt;p&gt;You are running code in a notebook or tool. You want to access the last traceback after the program stops. You did not save the error at the time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to read the last exception traceback after the program ends.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python keeps the last traceback in &lt;code&gt;sys.last_traceback&lt;/code&gt; when run interactively.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you access the last traceback using the &lt;code&gt;sys&lt;/code&gt; module.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="c1"&gt;# Only works in interactive mode:
# sys.last_traceback
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This lets you recover traceback details even if the program already stopped. It only works if Python is running in interactive mode and the error was uncaught.&lt;/p&gt;




&lt;h2&gt;
  
  
  Like, Comment, Share, and Subscribe
&lt;/h2&gt;

&lt;p&gt;Did you find this helpful? Let me know by clicking the like button below. I'd love to hear your thoughts in the comments, too! If you want to see more content like this, don't forget to subscribe. Thanks for reading!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;Mike Vincent&lt;/strong&gt;&lt;/a&gt; is an American software engineer and app developer from Los Angeles, California. &lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;More about Mike Vincent&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Quark's Outlines: Python Special Method Names</title>
      <dc:creator>Mike Vincent</dc:creator>
      <pubDate>Sat, 16 May 2026 12:29:40 +0000</pubDate>
      <link>https://dev.to/mike-vincent/quarks-outlines-python-special-method-names-3470</link>
      <guid>https://dev.to/mike-vincent/quarks-outlines-python-special-method-names-3470</guid>
      <description>&lt;h1&gt;
  
  
  Quark’s Outlines: Python Special Method Names
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Overview, Historical Timeline, Problems &amp;amp; Solutions&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  An Overview of Python Special Method Names
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What are Python special method names?
&lt;/h3&gt;

&lt;p&gt;When you use built-in syntax in Python, like &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;[]&lt;/code&gt;, or &lt;code&gt;len()&lt;/code&gt;, Python checks if the object has a matching method with a special name. These are called &lt;strong&gt;Python special method names&lt;/strong&gt;. They let your class behave like other built-in types.&lt;/p&gt;

&lt;p&gt;Each special method has a name with two underscores before and after it. For example, &lt;code&gt;__add__&lt;/code&gt; lets your object respond to &lt;code&gt;+&lt;/code&gt;, and &lt;code&gt;__getitem__&lt;/code&gt; lets your object respond to indexing like &lt;code&gt;obj[2]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets classes use special method names to behave like built-in types.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__getitem__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;

&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you use &lt;code&gt;x[3]&lt;/code&gt;, Python runs &lt;code&gt;x.__getitem__(3)&lt;/code&gt; because the class defined that method.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why use Python special method names?
&lt;/h3&gt;

&lt;p&gt;You use special method names to give your object custom behavior when used in common expressions. You can define how your object adds, subtracts, prints, or compares. These names give you control without writing extra code for every case.&lt;/p&gt;

&lt;p&gt;You do not call special methods directly in normal code. Python calls them for you when your object is used with special syntax.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python calls special method names automatically when using common syntax.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Greeter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__str__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Greeter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Hello!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;print()&lt;/code&gt; function uses &lt;code&gt;str(g)&lt;/code&gt;, which calls &lt;code&gt;g.__str__()&lt;/code&gt; to get the string value.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Historical Timeline of Python Special Method Names
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Where do Python’s special method names come from?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python special method names come from earlier object models that used hidden function calls to support operators and custom syntax. Python exposed those methods with a clear naming pattern so users could build objects that act like built-ins.&lt;/p&gt;




&lt;h3&gt;
  
  
  People created ways to override behavior in custom types
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1970 —&lt;/strong&gt; &lt;strong&gt;Simula and Smalltalk&lt;/strong&gt; introduced object methods to control print, math, and access.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1983 —&lt;/strong&gt; &lt;strong&gt;C++ operator overloading&lt;/strong&gt; let programmers define how &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;==&lt;/code&gt;, and &lt;code&gt;[]&lt;/code&gt; work for their own classes.&lt;/p&gt;




&lt;h3&gt;
  
  
  People added readable hooks to Python objects
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1991 —&lt;/strong&gt; &lt;strong&gt;Python object model&lt;/strong&gt; introduced dunder methods (like &lt;code&gt;__getitem__&lt;/code&gt;) to let user-defined classes support special syntax.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2000 —&lt;/strong&gt; &lt;strong&gt;New-style classes in Python 2.2&lt;/strong&gt; unified the object model, making all types use special methods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2013 —&lt;/strong&gt; &lt;strong&gt;Function annotations and class introspection&lt;/strong&gt; used &lt;code&gt;__annotations__&lt;/code&gt;, &lt;code&gt;__class__&lt;/code&gt;, and other internal names.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2025 —&lt;/strong&gt; &lt;strong&gt;Stable core dunder names&lt;/strong&gt; became essential for all custom types and data classes, with no new names added in recent releases.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problems &amp;amp; Solutions with Python Special Method Names
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;How do you use Python special method names the right way?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python special method names let your class support syntax that feels natural. These problems show how to use them to add math, indexing, string conversion, length, and iteration to your custom classes. Each solution shows how Python lets you control behavior using the correct special method.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you support &lt;code&gt;+&lt;/code&gt; for your object in Python?
&lt;/h3&gt;

&lt;p&gt;You are making a custom class that holds a number. You want to let users add two of your objects with the &lt;code&gt;+&lt;/code&gt; symbol, the same way they add numbers. But Python does not know how to add your objects unless you tell it how.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want your class to support &lt;code&gt;+&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python calls &lt;code&gt;__add__()&lt;/code&gt; when two objects are added.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you define &lt;code&gt;__add__()&lt;/code&gt; to support &lt;code&gt;+&lt;/code&gt;.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__add__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__str__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 8
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The method &lt;code&gt;__add__()&lt;/code&gt; lets your class handle &lt;code&gt;+&lt;/code&gt;. The result is another &lt;code&gt;Box&lt;/code&gt; with the new total.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you make your object show custom text with &lt;code&gt;print()&lt;/code&gt; in Python?
&lt;/h3&gt;

&lt;p&gt;You want your object to show a friendly message when someone prints it. By default, Python shows the memory address. You want it to show a message instead.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want your object to return a message when printed.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python uses &lt;code&gt;__str__()&lt;/code&gt; to get the string for &lt;code&gt;print()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you define &lt;code&gt;__str__()&lt;/code&gt; to control how objects print.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Pet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__str__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;This is your pet.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Pet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# This is your pet.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;__str__()&lt;/code&gt; method lets you return a string that &lt;code&gt;print()&lt;/code&gt; will show.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you let your object be used like a list in Python?
&lt;/h3&gt;

&lt;p&gt;You made a custom data class. You want people to use square brackets &lt;code&gt;[]&lt;/code&gt; to get values, just like with lists. But Python does not know how unless you define a method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want your object to support indexing.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python calls &lt;code&gt;__getitem__()&lt;/code&gt; when using brackets.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you define &lt;code&gt;__getitem__()&lt;/code&gt; to allow square bracket access.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Pair&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__getitem__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;left&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;right&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Pair&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# right
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;__getitem__()&lt;/code&gt; method lets your object handle &lt;code&gt;p[1]&lt;/code&gt; like a list or string.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you let your object work with &lt;code&gt;len()&lt;/code&gt; in Python?
&lt;/h3&gt;

&lt;p&gt;You want your class to tell how many items it holds. You want to use &lt;code&gt;len(obj)&lt;/code&gt;, but Python cannot do that unless your object supports it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to let &lt;code&gt;len(obj)&lt;/code&gt; work with your object.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python calls &lt;code&gt;__len__()&lt;/code&gt; when you use &lt;code&gt;len()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you define &lt;code&gt;__len__()&lt;/code&gt; to support length checks.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Trio&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__len__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;

&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Trio&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method makes &lt;code&gt;len(t)&lt;/code&gt; return &lt;code&gt;3&lt;/code&gt; because you defined it that way.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you make your object work in a for-loop in Python?
&lt;/h3&gt;

&lt;p&gt;You want users to loop over your object with &lt;code&gt;for x in obj:&lt;/code&gt;. Right now Python does not know how. You want to return one value at a time from inside your object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want your object to be used in a loop.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python uses &lt;code&gt;__iter__()&lt;/code&gt; and &lt;code&gt;__next__()&lt;/code&gt; to support loops.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you define &lt;code&gt;__iter__()&lt;/code&gt; to support iteration.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CountDown&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__iter__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__next__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nb"&gt;StopIteration&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nc"&gt;CountDown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 3
# 2
# 1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When used in a loop, Python runs &lt;code&gt;__iter__()&lt;/code&gt; to get the object and calls &lt;code&gt;__next__()&lt;/code&gt; to get each value.&lt;/p&gt;




&lt;h2&gt;
  
  
  Like, Comment, Share, and Subscribe
&lt;/h2&gt;

&lt;p&gt;Did you find this helpful? Let me know by clicking the like button below. I'd love to hear your thoughts in the comments, too! If you want to see more content like this, don't forget to subscribe. Thanks for reading!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;Mike Vincent&lt;/strong&gt;&lt;/a&gt; is an American software engineer and app developer from Los Angeles, California. &lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;More about Mike Vincent&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Quark's Outlines: Python Slice Objects</title>
      <dc:creator>Mike Vincent</dc:creator>
      <pubDate>Sat, 09 May 2026 12:27:23 +0000</pubDate>
      <link>https://dev.to/mike-vincent/quarks-outlines-python-slice-objects-2dea</link>
      <guid>https://dev.to/mike-vincent/quarks-outlines-python-slice-objects-2dea</guid>
      <description>&lt;h1&gt;
  
  
  Quark’s Outlines: Python Slice Objects
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Overview, Historical Timeline, Problems &amp;amp; Solutions&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  An Overview of Python Slice Objects
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is a Python slice object?
&lt;/h3&gt;

&lt;p&gt;When you want to take part of a list or string in Python, you use a slice. A &lt;strong&gt;Python slice object&lt;/strong&gt; is what Python uses behind the scenes when you use slice notation like &lt;code&gt;a[1:4]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can also create a slice object yourself using the built-in &lt;code&gt;slice()&lt;/code&gt; function. A slice object has three parts: a start, a stop, and a step. These tell Python where to begin, where to end, and how far to jump between values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you describe parts of a sequence using slice objects.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 1 5 2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can pass this object to any indexable type that accepts slices, such as lists, strings, or tuples.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem: How do you use Python slice objects with lists in Python?
&lt;/h3&gt;

&lt;p&gt;You can apply a slice object directly to a list. This gives you a way to reuse the same slice pattern across different lists or strings. A slice object is used the same way as writing &lt;code&gt;a[start:stop:step]&lt;/code&gt; in normal slice notation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python slice objects can be used as reusable slicing tools.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;c&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;d&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;e&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;part&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;part&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# ['b', 'c', 'd']
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This helps when you want to define the slice once and use it many times.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are extended Python slice objects?
&lt;/h3&gt;

&lt;p&gt;Python slice objects can express more than just start and stop. They can also use a step. This allows you to skip values, move backwards, or extract patterns in regular steps.&lt;/p&gt;

&lt;p&gt;Slice objects with steps are often called &lt;strong&gt;extended slices&lt;/strong&gt;. You can write them with double colons like &lt;code&gt;a[::2]&lt;/code&gt; or make them with &lt;code&gt;slice(start, stop, step)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you use slice steps to skip or reverse items.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[::&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# [0, 2, 4, 6]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The step of &lt;code&gt;2&lt;/code&gt; means every other item is included. A negative step moves backwards.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Historical Timeline of Python Slice Objects
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Where do Python’s slice objects come from?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python slice objects were created to make sequence access flexible and readable. Python built on older ideas from languages like ALGOL and C but added rich slicing as a first-class concept. The timeline shows how slices grew from syntax into objects.&lt;/p&gt;




&lt;h3&gt;
  
  
  People built tools to slice sequences in math and code
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1960 —&lt;/strong&gt; &lt;strong&gt;Array slicing&lt;/strong&gt; in ALGOL 60 let programmers work with parts of arrays using range syntax.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1972 —&lt;/strong&gt; &lt;strong&gt;Pointer arithmetic&lt;/strong&gt; in C let programmers access memory in flexible steps but with more risk.&lt;/p&gt;




&lt;h3&gt;
  
  
  People designed Python slice syntax and objects
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1991 —&lt;/strong&gt; &lt;strong&gt;Slice notation added&lt;/strong&gt; in Python 0.9.0 using &lt;code&gt;a[start:stop]&lt;/code&gt; for lists and strings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2001 —&lt;/strong&gt; &lt;strong&gt;Slice objects created&lt;/strong&gt; in Python 2.2 with the &lt;code&gt;slice()&lt;/code&gt; function and support for &lt;code&gt;start&lt;/code&gt;, &lt;code&gt;stop&lt;/code&gt;, and &lt;code&gt;step&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2006 —&lt;/strong&gt; &lt;strong&gt;Ellipsis added&lt;/strong&gt; to slicing, allowing &lt;code&gt;a[..., 1:3]&lt;/code&gt; in advanced data types like NumPy arrays.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2015 —&lt;/strong&gt; &lt;strong&gt;Slicing with custom classes&lt;/strong&gt; supported when classes defined &lt;code&gt;__getitem__&lt;/code&gt; with slice detection.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2025 —&lt;/strong&gt; &lt;strong&gt;Slice behavior stable&lt;/strong&gt; with built-in support for slicing strings, bytes, ranges, and user-defined types.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problems &amp;amp; Solutions with Python Slice Objects
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;How do you use Python slice objects the right way?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python slice objects give you a way to describe how to take part of a sequence. These problems show how slice objects can help you break up data, skip items, move backward, and reuse slice logic across different parts of your program.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you reuse the same slice across different data in Python?
&lt;/h3&gt;

&lt;p&gt;You are working with two lists and a string. You want to take the same part of each one. You do not want to write the same &lt;code&gt;start:stop&lt;/code&gt; slice over and over. You want a way to define the slice once and use it many times.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to reuse slice logic across different sequences.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you create a slice object with &lt;code&gt;slice()&lt;/code&gt; and use it like &lt;code&gt;obj[slice_obj]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you apply the same slice object to many values.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;part&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;words&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;zero&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;one&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;two&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;three&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;four&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;five&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;abcdefg&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;part&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;part&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# ['two', 'three', 'four']
# cde
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The slice object works the same way for strings and lists. You define the slice once and reuse it.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you extract every other item in Python?
&lt;/h3&gt;

&lt;p&gt;You have a list with many items. You only want every second item. You could use a loop, but that takes more lines and more thought. You want a quick way to skip items while slicing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You need to select values in regular steps.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you set the &lt;code&gt;step&lt;/code&gt; part of a slice to skip items.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you use steps in slices to skip values.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[::&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# [10, 30, 50]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The slice &lt;code&gt;::2&lt;/code&gt; means "start at the beginning and take every second item."&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you move through a list backwards in Python?
&lt;/h3&gt;

&lt;p&gt;You want to read a list from end to start. You do not want to reverse it in place or make a copy. You want to get the reversed values using slice logic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You need to move through a list in reverse order.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python slices accept negative step values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you move backwards using a slice step of -1.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[::&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# [5, 4, 3, 2, 1]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The slice &lt;code&gt;[::-1]&lt;/code&gt; means "start at the end and move backward one step at a time."&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you pass a slice into a function in Python?
&lt;/h3&gt;

&lt;p&gt;You have a utility function that should return a portion of any list. You want to pass the slice as a parameter instead of hard-coding it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to pass slice behavior as a value.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you pass a slice object as an argument.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you pass slice objects into functions.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_part&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;c&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;d&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;e&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get_part&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# ['b', 'c', 'd']
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can change the slice logic outside the function and keep the function simple.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you describe a slice without knowing the type in Python?
&lt;/h3&gt;

&lt;p&gt;You are working with custom objects that support slicing. You want to describe a slice without using normal &lt;code&gt;[:]&lt;/code&gt; syntax. You want a way to create slices even if the data type is not a list or string.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want a general way to describe a slice in code.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you make a slice object using the built-in &lt;code&gt;slice()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you build slice objects without using &lt;code&gt;[]&lt;/code&gt;.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 0 3 None
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This object can be used on any sequence-like type that accepts slices, including user-defined classes with special methods.&lt;/p&gt;




&lt;h2&gt;
  
  
  Like, Comment, Share, and Subscribe
&lt;/h2&gt;

&lt;p&gt;Did you find this helpful? Let me know by clicking the like button below. I'd love to hear your thoughts in the comments, too! If you want to see more content like this, don't forget to subscribe. Thanks for reading!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;Mike Vincent&lt;/strong&gt;&lt;/a&gt; is an American software engineer and app developer from Los Angeles, California. &lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;More about Mike Vincent&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Quark's Outlines: Python Name Spaces</title>
      <dc:creator>Mike Vincent</dc:creator>
      <pubDate>Sat, 02 May 2026 12:25:50 +0000</pubDate>
      <link>https://dev.to/mike-vincent/quarks-outlines-python-name-spaces-3a18</link>
      <guid>https://dev.to/mike-vincent/quarks-outlines-python-name-spaces-3a18</guid>
      <description>&lt;h1&gt;
  
  
  Quark’s Outlines: Python Name Spaces
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Overview, Historical Timeline, Problems &amp;amp; Solutions&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  An Overview of Python Name Spaces
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is a Python name space?
&lt;/h3&gt;

&lt;p&gt;When you write code in Python, you give names to values. A &lt;strong&gt;Python name space&lt;/strong&gt; is the place where those names are stored. It is like a labeled shelf that holds objects. When you ask for a name, Python looks in the shelf to find it.&lt;/p&gt;

&lt;p&gt;A name space is a mapping between names and objects. In Python, that mapping is usually a dictionary. Each name space stores the current known names and their values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you manage names by storing them in name spaces.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;globals&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;x&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;globals()&lt;/code&gt; function returns the current global name space. You can look up a name and find its value.&lt;/p&gt;

&lt;h3&gt;
  
  
  How many name spaces does Python use?
&lt;/h3&gt;

&lt;p&gt;Python uses three main name spaces when running code: local, global, and built-in. Each one is searched in order when Python tries to find a name. Local is the first shelf. Global is the next shelf. Built-in is the last shelf.&lt;/p&gt;

&lt;p&gt;Each function or block of code runs in its own execution frame. The frame sets the local and global name spaces.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python searches for names in a fixed order: local, global, built-in.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;demo&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;locals&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;x&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="nf"&gt;demo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 10
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;locals()&lt;/code&gt; returns the local name space of the &lt;code&gt;demo()&lt;/code&gt; function. Python found the name &lt;code&gt;x&lt;/code&gt; there.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do names become local or global?
&lt;/h3&gt;

&lt;p&gt;When Python sees a name, it decides if that name is local or global by looking at how you use it. If you assign a value to the name in a code block, Python makes it local. If you use &lt;code&gt;global&lt;/code&gt;, Python treats it as global. The same rule applies to &lt;code&gt;nonlocal&lt;/code&gt; for nested functions.&lt;/p&gt;

&lt;p&gt;You cannot assign to a global name without saying so. You must declare it first with &lt;code&gt;global&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you control where a name lives using &lt;code&gt;global&lt;/code&gt; or &lt;code&gt;nonlocal&lt;/code&gt;.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;global&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;
    &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without the &lt;code&gt;global&lt;/code&gt; statement, the name &lt;code&gt;count&lt;/code&gt; would be treated as a new local name and not affect the global value.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Historical Timeline of Python Name Spaces
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Where do Python’s name space rules come from?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python name spaces follow from long traditions in structured programming. They help manage how values are stored and found. This timeline shows how Python shaped its name space model by building on earlier systems and simplifying control.&lt;/p&gt;




&lt;h3&gt;
  
  
  People designed scope and name systems in programming
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1960 —&lt;/strong&gt; &lt;strong&gt;Block scope&lt;/strong&gt; in ALGOL introduced local variables and nested name visibility.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1972 —&lt;/strong&gt; &lt;strong&gt;Global and local stacks&lt;/strong&gt; in C separated function-level and file-level scope.&lt;/p&gt;




&lt;h3&gt;
  
  
  People built Python’s three-level name space model
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1991 —&lt;/strong&gt; &lt;strong&gt;Python 0.9.0&lt;/strong&gt; introduced global, local, and built-in name spaces tied to execution frames.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2000 —&lt;/strong&gt; &lt;strong&gt;Dynamic features&lt;/strong&gt; like &lt;code&gt;exec&lt;/code&gt; and &lt;code&gt;eval&lt;/code&gt; worked with customizable name space dictionaries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2003 —&lt;/strong&gt; &lt;strong&gt;Global and nonlocal statements&lt;/strong&gt; became required to rebind outer names safely.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2010 —&lt;/strong&gt; &lt;strong&gt;Function scoping stabilized&lt;/strong&gt; with improved shadowing rules and traceability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2025 —&lt;/strong&gt; &lt;strong&gt;Stable execution model&lt;/strong&gt; kept the name space structure clear and unchanged.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problems &amp;amp; Solutions with Python Name Spaces
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;How do you use Python name spaces the right way?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python uses name spaces to keep values organized while code runs. Each code block runs inside a frame, and each frame has local and global name spaces. These problems show how name spaces affect what Python sees and what it cannot see.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you modify a name in the outer scope in Python?
&lt;/h3&gt;

&lt;p&gt;You define a value and use it inside a function. But when you run the function, Python says the name does not exist. You expected Python to use your global variable, but it made a new local one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to share a name between a function and the outer scope.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you mark the name as global to make it refer to the same value everywhere.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you bind names to global space using &lt;code&gt;global&lt;/code&gt;.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;global&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;
    &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without the &lt;code&gt;global&lt;/code&gt; line, Python would raise an error because &lt;code&gt;counter += 1&lt;/code&gt; would try to read an uninitialized local name.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you see what names are in use in Python?
&lt;/h3&gt;

&lt;p&gt;You are writing a function that changes many variables. You want to see what names are active inside the function as it runs. You also want to inspect the global names.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to examine the current local and global name spaces.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python provides &lt;code&gt;locals()&lt;/code&gt; and &lt;code&gt;globals()&lt;/code&gt; to return the current mappings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you see active names using built-in inspection tools.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;report&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Local:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;locals&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Global:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;x =&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;globals&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;x&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="nf"&gt;report&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Local: {'y': 200}
# Global: x = 100
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python shows the two separate name spaces so you can know which values are in each.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you avoid accidental name changes in Python?
&lt;/h3&gt;

&lt;p&gt;You write a helper function that uses the same name as one of your main program variables. Now your main value is changed unexpectedly after calling the helper.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to keep helper names separate from program names.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python keeps local name spaces separate by default. Avoid global names inside functions unless needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python keeps function names isolated using local name spaces.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;main&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;helper&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;helper&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Inside:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;helper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Outside:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Inside: helper
# Outside: main
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each block has its own space. Reusing a name locally does not affect the global value unless you declare it as global.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you use &lt;code&gt;eval()&lt;/code&gt; safely in Python?
&lt;/h3&gt;

&lt;p&gt;You want to evaluate a string as code. You want to control what names the string sees when it runs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to give custom name spaces to code run with &lt;code&gt;eval()&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you pass dictionaries to control global and local name spaces in &lt;code&gt;eval()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you set name space mappings for dynamic code.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;x&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;y&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;eval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;x * y&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The string &lt;code&gt;"x * y"&lt;/code&gt; is evaluated using the custom scope dictionary. Python does not use the default globals or locals.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: handle name lookup when a name is missing
&lt;/h3&gt;

&lt;p&gt;You write code that refers to a name, but you forget to define it. When Python runs, it stops with an error. You want to understand what error this is and how Python looks for names.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to know what happens when a name is not found.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python looks in local, then global, then built-in name spaces. If no match is found, it raises a &lt;code&gt;NameError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python raises an error when name lookup fails in all name spaces.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;missing_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;NameError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Caught error:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Caught error: name 'missing_name' is not defined
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python checks each shelf of names in order. If the name is not on any shelf, it stops and reports the problem.&lt;/p&gt;




&lt;h2&gt;
  
  
  Like, Comment, Share, and Subscribe
&lt;/h2&gt;

&lt;p&gt;Did you find this helpful? Let me know by clicking the like button below. I'd love to hear your thoughts in the comments, too! If you want to see more content like this, don't forget to subscribe. Thanks for reading!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;Mike Vincent&lt;/strong&gt;&lt;/a&gt; is an American software engineer and app developer from Los Angeles, California. &lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;More about Mike Vincent&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Quark's Outlines: Python Internal Types</title>
      <dc:creator>Mike Vincent</dc:creator>
      <pubDate>Sat, 25 Apr 2026 12:21:23 +0000</pubDate>
      <link>https://dev.to/mike-vincent/quarks-outlines-python-internal-types-5581</link>
      <guid>https://dev.to/mike-vincent/quarks-outlines-python-internal-types-5581</guid>
      <description>&lt;h1&gt;
  
  
  Quark’s Outlines: Python Internal Types
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Overview, Historical Timeline, Problems &amp;amp; Solutions&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  An Overview of Python Internal Types
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What are Python internal types?
&lt;/h3&gt;

&lt;p&gt;You use Python every day to write programs, but some parts of Python stay hidden. These are called &lt;strong&gt;internal types&lt;/strong&gt;. They work behind the scenes to help your code run. You do not make these objects directly, but Python creates them while it runs your program.&lt;/p&gt;

&lt;p&gt;Some internal types include &lt;strong&gt;code objects&lt;/strong&gt;, &lt;strong&gt;frame objects&lt;/strong&gt;, &lt;strong&gt;traceback objects&lt;/strong&gt;, and &lt;strong&gt;slice objects&lt;/strong&gt;. These objects store details about your code, the stack, and how Python handles errors or slices. You can inspect them using built-in tools or system modules.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python uses internal types to manage execution, errors, and slicing.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# &amp;lt;class 'slice'&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line shows that &lt;code&gt;slice(1, 5, 2)&lt;/code&gt; creates a slice object. Python builds this object when you write slicing code.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does Python use code objects?
&lt;/h3&gt;

&lt;p&gt;A code object is a block of Python instructions. Python creates a code object when you define a function. The code object holds the function's bytecode, argument names, and other parts. It does not know the function's global variables.&lt;/p&gt;

&lt;p&gt;You can get a function's code object using the &lt;code&gt;__code__&lt;/code&gt; attribute.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python stores instructions using code objects.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hi, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__code__&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_varnames&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# ('name',)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prints the list of variable names used in the function. Code objects are read-only and do not change after creation.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are Python frame objects?
&lt;/h3&gt;

&lt;p&gt;A frame object shows the state of Python when it runs a line of code. Each time Python calls a function, it builds a frame. That frame stores the current code object, the local and global variables, and the current line number.&lt;/p&gt;

&lt;p&gt;Frames are stacked. When one function calls another, Python adds a new frame on top of the stack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python uses frame objects to track function calls.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;inspect&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inspect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;currentframe&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;f_code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# check
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prints the name of the function running in the current frame. Frames help tools like debuggers and tracers understand what your code is doing.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does Python use traceback objects?
&lt;/h3&gt;

&lt;p&gt;When your code has an error, Python creates a traceback object. The traceback shows the call stack at the time of the error. Each level of the stack gets one traceback object.&lt;/p&gt;

&lt;p&gt;Traceback objects help Python explain where the error came from. The last one in the chain shows the line where the error happened.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python uses traceback objects to explain errors.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exc_info&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;tb_lineno&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prints the line number where the error happened. You can use traceback objects to write logs or format error messages.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a Python slice object?
&lt;/h3&gt;

&lt;p&gt;A slice object tells Python how to cut a list or a string. It stores a start, stop, and step value. Python makes a slice object when you use a colon &lt;code&gt;:&lt;/code&gt; in brackets.&lt;/p&gt;

&lt;p&gt;You can also create one directly using &lt;code&gt;slice(start, stop, step)&lt;/code&gt;. The values may be numbers or &lt;code&gt;None&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python uses slice objects to slice sequences.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c1"&gt;# range(2, 10, 2)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prints the values at index 2, 4, 6, and 8. Python reads the slice object and uses it to cut the data.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Historical Timeline of Python Internal Types
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;How did Python internal types develop?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python internal types grew from the need to manage code execution and error handling. These types are not part of daily Python use, but they support every Python program behind the scenes.&lt;/p&gt;




&lt;h3&gt;
  
  
  People invented stack frames and bytecode
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1959 — Stack frame tracking&lt;/strong&gt;, IBM 1401, made it easier to debug and resume execution.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;1970 — Bytecode instructions&lt;/strong&gt;, Pascal P-code, created a way to compile programs to simple portable steps.  &lt;/p&gt;
&lt;h3&gt;
  
  
  People designed Python’s execution system
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1991 — Code and frame types&lt;/strong&gt;, Python 0.9.0, added code objects and frame objects for functions and the call stack.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2000 — Traceback objects&lt;/strong&gt;, Python 2.0, exposed tracebacks to help with exception handling and logging.&lt;/p&gt;
&lt;h3&gt;
  
  
  People improved slice handling
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;2001 — Slice object support&lt;/strong&gt;, Python 2.2, added the slice type and allowed extended slicing syntax.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2008 — Full Unicode traceback&lt;/strong&gt;, Python 3.0, improved traceback readability for all users.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2025 — Internal types frozen&lt;/strong&gt;, Python core team, made internal object formats stable for tooling.&lt;/p&gt;


&lt;h2&gt;
  
  
  Problems &amp;amp; Solutions with Python Internal Types
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;How do Python internal types help your program run?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python creates internal types to hold bytecode, manage stack frames, store errors, and handle slices. These objects are not part of your script, but they shape how Python works. Each problem shows how an internal type helps solve a task that Python must handle behind the scenes.&lt;/p&gt;


&lt;h3&gt;
  
  
  Problem: How do you understand what your function does at runtime in Python?
&lt;/h3&gt;

&lt;p&gt;You write a function that behaves strangely. You want to inspect what it does under the hood without changing its code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; How can you view the internal steps or structure of a function?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Python stores details in a code object. You can access it using &lt;code&gt;__code__&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you inspect bytecode and arguments with code objects.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__code__&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_varnames&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# ('x',)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This shows that the function takes one variable named &lt;code&gt;x&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you follow what happens when a program runs in Python?
&lt;/h3&gt;

&lt;p&gt;You call one function, which calls another, and so on. Something breaks, and you want to trace which part of the code is running right now.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; How can you follow the stack of function calls?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Python builds frame objects to track each function call.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you view the call stack with frame objects.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;inspect&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;trace&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inspect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;currentframe&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;f_back&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f_code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nf"&gt;trace&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# outer
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prints the name of the function that called &lt;code&gt;trace&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you locate where an error happened in Python?
&lt;/h3&gt;

&lt;p&gt;You run a block of code and it crashes. You want to know exactly which line caused the crash.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; How can you find the error line programmatically?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Python creates a traceback object that stores the error location.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python gives you line numbers from traceback objects.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exc_info&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;tb_lineno&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prints the line where the error happened, not where the traceback is printed.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you use a slice with three parts in Python?
&lt;/h3&gt;

&lt;p&gt;You want to extract every third value from a list between two positions. The usual slice &lt;code&gt;a:b&lt;/code&gt; is not enough.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; How can you express a slice with a step value?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Python creates a slice object with start, stop, and step.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you define steps with slice objects.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c1"&gt;# [1, 4, 7]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The slice picks index 1, then 4, then 7.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you log what happened after an error in Python?
&lt;/h3&gt;

&lt;p&gt;You run a function that fails. You want to keep a clean log that includes the path of calls and the code line where it failed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; How do you log errors with full context?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Python keeps a chain of traceback objects for the whole stack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you trace errors step-by-step with traceback objects.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;traceback&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;tb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exc_info&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;tb&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tb_frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f_code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tb_lineno&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;tb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tb_next&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;lt;module&amp;gt; 2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prints the stack trace with function names and line numbers in order.&lt;/p&gt;




&lt;h2&gt;
  
  
  Like, Comment, Share, and Subscribe
&lt;/h2&gt;

&lt;p&gt;Did you find this helpful? Let me know by clicking the like button below. I'd love to hear your thoughts in the comments, too! If you want to see more content like this, don't forget to subscribe. Thanks for reading!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;Mike Vincent&lt;/strong&gt;&lt;/a&gt; is an American software engineer and app developer from Los Angeles, California. &lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;More about Mike Vincent&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Quark's Outlines: Python Frame Objects</title>
      <dc:creator>Mike Vincent</dc:creator>
      <pubDate>Sat, 18 Apr 2026 12:19:10 +0000</pubDate>
      <link>https://dev.to/mike-vincent/quarks-outlines-python-frame-objects-1gh0</link>
      <guid>https://dev.to/mike-vincent/quarks-outlines-python-frame-objects-1gh0</guid>
      <description>&lt;h1&gt;
  
  
  Quark’s Outlines: Python Frame Objects
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Overview, Historical Timeline, Problems &amp;amp; Solutions&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  An Overview of Python Frame Objects
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is a Python frame object?
&lt;/h3&gt;

&lt;p&gt;When you run a Python function, Python creates a &lt;strong&gt;frame object&lt;/strong&gt;. A frame object holds everything Python needs to track that function. It stores local variables, global names, and the exact point where Python is running in the code. If a function calls another function, Python adds a new frame on top of the last one.&lt;/p&gt;

&lt;p&gt;You can think of Python frame objects like stack cards. Each time you call a function, Python adds a new card to the stack. When the function ends, Python removes the card and goes back to the one before it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you see the current frame using sys._getframe().&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;example&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;frame&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_getframe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Now running:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f_code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;example&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Now running: example
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The frame object stores the function’s name, code, and variables.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does a Python frame object contain?
&lt;/h3&gt;

&lt;p&gt;A Python frame holds these things: the function’s code object (&lt;code&gt;f_code&lt;/code&gt;), a link to the previous frame (&lt;code&gt;f_back&lt;/code&gt;), local variables (&lt;code&gt;f_locals&lt;/code&gt;), global variables (&lt;code&gt;f_globals&lt;/code&gt;), built-in names (&lt;code&gt;f_builtins&lt;/code&gt;), the line number (&lt;code&gt;f_lineno&lt;/code&gt;), and the last instruction run (&lt;code&gt;f_lasti&lt;/code&gt;). The &lt;code&gt;f_trace&lt;/code&gt; value can be used by a debugger to run a function every time a line runs.&lt;/p&gt;

&lt;p&gt;Each frame is tied to one place in the program. You can follow &lt;code&gt;f_back&lt;/code&gt; to move up the call stack and see where Python came from.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you follow the call stack using frame.f_back.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;frame&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_getframe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Called from:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f_back&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f_code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Called from: outer
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The inner frame links back to the outer frame.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Historical Timeline of Python Frame Objects
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Where do Python’s frame objects come from?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python’s frame system follows the tradition of stack-based execution in early languages. These languages built a trace of function calls to keep track of state, then exposed the call stack to the user in rare cases like errors or debugging. Python makes this visible as part of normal execution.&lt;/p&gt;




&lt;h3&gt;
  
  
  People invented ways to trace running code.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1960 — Call stack frames&lt;/strong&gt; ALGOL introduced function call stacks to track nested execution.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;1972 — Execution state saved per call&lt;/strong&gt; C stored state for each function using the system stack.  &lt;/p&gt;
&lt;h3&gt;
  
  
  People designed Python’s internal stack.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1991 — Python 0.9.0 introduced frame objects&lt;/strong&gt; Each function call was backed by a Python frame.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;1994 — Tracebacks linked to frames&lt;/strong&gt; Python exposed the call stack during exceptions.  &lt;/p&gt;
&lt;h3&gt;
  
  
  People made Python frame objects visible.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1995 — sys._getframe() added&lt;/strong&gt; Python gave users a way to see the current frame object.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2000 — Debuggers used f_trace&lt;/strong&gt; Python 2.0 let debuggers follow execution line by line.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2025 — Python frame object structure stable&lt;/strong&gt; Frame objects remain key to inspection, tracing, and errors.&lt;/p&gt;


&lt;h2&gt;
  
  
  Problems &amp;amp; Solutions with Python Frame Objects
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;How do you use Python frame objects the right way?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python frame objects show where the code is running, what values are stored, and what called what. These problems show how you can use frame objects to understand and manage the flow of your program.&lt;/p&gt;


&lt;h3&gt;
  
  
  Problem: How do you find what function is running in Python?
&lt;/h3&gt;

&lt;p&gt;You are writing a log system and want each function to print its name when it runs. You do not want to write the name by hand. You want Python to find it on its own.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You need to find the current function name without writing it yourself.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Use a frame object and read &lt;code&gt;f_code.co_name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you inspect the function name using a frame.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;log_me&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;frame&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_getframe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;This function is:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f_code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;log_me&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# This function is: log_me
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Problem: How do you trace who called your function in Python?
&lt;/h3&gt;

&lt;p&gt;You want to know what part of your code called this function. You are trying to debug a case where the same function is used in many places. You want to print the name of the caller.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You do not know who called your function.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Use the frame’s &lt;code&gt;f_back&lt;/code&gt; link to read the caller’s name.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you trace the caller using frame.f_back.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;frame&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_getframe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Caller was:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f_back&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f_code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Caller was: outer
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Problem: How do you inspect local variables inside a frame in Python?
&lt;/h3&gt;

&lt;p&gt;You are writing a tool that shows all local variables at a certain point in your code. You want to print what names exist and what values they hold.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to get all current local variables from running code.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Use &lt;code&gt;f_locals&lt;/code&gt; to read the local name-value pairs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you list local variables using frame.f_locals.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;check_vars&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
    &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
    &lt;span class="n"&gt;frame&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_getframe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Local vars:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f_locals&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;check_vars&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Local vars: {'x': 10, 'y': 20, 'frame': &amp;lt;frame object at ...&amp;gt;}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Problem: How do you get the exact line Python is on in Python?
&lt;/h3&gt;

&lt;p&gt;You are debugging and want to show the line number where Python is running. You want to print this each time a function runs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to show the current line number from code.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Use the &lt;code&gt;f_lineno&lt;/code&gt; value in the frame.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you print the current line using frame.f_lineno.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show_line&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;frame&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_getframe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Line number:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f_lineno&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;show_line&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Line number: (this line’s number)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Problem: How do you run code on every line while debugging in Python?
&lt;/h3&gt;

&lt;p&gt;You are building a custom debugger. You want to run a function each time Python moves to a new line in the code. You need to hook into Python’s execution engine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You need to run a custom action on each source line.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Use the frame’s &lt;code&gt;f_trace&lt;/code&gt; hook.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you trace line steps using frame.f_trace.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;trace_func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Trace event:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Line:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f_lineno&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;trace_func&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
    &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;

&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;settrace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trace_func&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;settrace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Trace event: call Line: (start)
# Trace event: line Line: (x = 1)
# Trace event: line Line: (y = 2)
# Trace event: line Line: (z = x + y)
# ...
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Like, Comment, Share, and Subscribe
&lt;/h2&gt;

&lt;p&gt;Did you find this helpful? Let me know by clicking the like button below. I'd love to hear your thoughts in the comments, too! If you want to see more content like this, don't forget to subscribe. Thanks for reading!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;Mike Vincent&lt;/strong&gt;&lt;/a&gt; is an American software engineer and app developer from Los Angeles, California. &lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;More about Mike Vincent&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Quark's Outlines: Python Expressions</title>
      <dc:creator>Mike Vincent</dc:creator>
      <pubDate>Sat, 11 Apr 2026 12:17:45 +0000</pubDate>
      <link>https://dev.to/mike-vincent/quarks-outlines-python-expressions-51ai</link>
      <guid>https://dev.to/mike-vincent/quarks-outlines-python-expressions-51ai</guid>
      <description>&lt;h1&gt;
  
  
  Quark’s Outlines: Python Expressions
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Overview, Historical Timeline, Problems &amp;amp; Solutions&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  An Overview of Python Expressions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is a Python expression?
&lt;/h3&gt;

&lt;p&gt;When you write a line in Python that gives a value, that line is called an &lt;strong&gt;expression&lt;/strong&gt;. A &lt;strong&gt;Python expression&lt;/strong&gt; is any piece of code that Python can run and produce a value. An expression can be a number, a string, a function call, or even a full equation.&lt;/p&gt;

&lt;p&gt;When Python sees an expression, it runs it and gives the result. You can use expressions in assignments, in function arguments, and in control structures like &lt;code&gt;if&lt;/code&gt; or &lt;code&gt;while&lt;/code&gt;. Every expression produces a value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you write expressions to compute values.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 14
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This expression adds &lt;code&gt;2&lt;/code&gt; to &lt;code&gt;3 * 4&lt;/code&gt;. Python runs the expression and assigns the result to &lt;code&gt;x&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What kinds of Python expressions are there?
&lt;/h3&gt;

&lt;p&gt;Python has many kinds of expressions. These include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Arithmetic expressions&lt;/strong&gt;, like &lt;code&gt;3 + 5&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;String expressions&lt;/strong&gt;, like &lt;code&gt;'Hello' + 'World'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Comparison expressions&lt;/strong&gt;, like &lt;code&gt;x &amp;gt; 5&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function calls&lt;/strong&gt;, like &lt;code&gt;len(data)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Comprehensions&lt;/strong&gt;, like &lt;code&gt;[x*x for x in range(3)]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Boolean expressions&lt;/strong&gt;, like &lt;code&gt;x &amp;gt; 5 and y &amp;lt; 10&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of these return values. You can use expressions by themselves, or as part of a larger statement.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python expressions include math, logic, and function calls.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Ada&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello, &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Hello, Ada
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the string expression &lt;code&gt;"Hello, " + name&lt;/code&gt; creates a new string and assigns it to &lt;code&gt;greeting&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What happens when Python evaluates an expression?
&lt;/h3&gt;

&lt;p&gt;Python reads an expression from left to right. It uses rules called &lt;strong&gt;precedence&lt;/strong&gt; and &lt;strong&gt;associativity&lt;/strong&gt; to decide the order of steps. If you use parentheses, you can control that order.&lt;/p&gt;

&lt;p&gt;When Python runs the expression, it creates objects, combines values, and calls functions. The result is stored or returned depending on the context.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python expressions are evaluated based on rules of order and grouping.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 20
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The parentheses make Python add first, then multiply.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Historical Timeline of Python Expressions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Where do Python’s expression rules come from?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python expressions are shaped by rules from math, logic, and earlier programming languages. The design combines simplicity with flexibility, so you can write small pieces that build up to full programs.&lt;/p&gt;




&lt;h3&gt;
  
  
  People defined expressions as a way to show meaning in code
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1956 —&lt;/strong&gt; &lt;strong&gt;Infix notation&lt;/strong&gt; used in early math languages to show operations like &lt;code&gt;a + b&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;1960 —&lt;/strong&gt; &lt;strong&gt;ALGOL 60&lt;/strong&gt; introduced structured expressions with nested function calls and operators.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;1972 —&lt;/strong&gt; &lt;strong&gt;C language&lt;/strong&gt; added typed expressions, function calls, and rich operator behavior.&lt;/p&gt;


&lt;h3&gt;
  
  
  People designed Python expressions for clarity and structure
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1991 —&lt;/strong&gt; &lt;strong&gt;Python 0.9.0&lt;/strong&gt; supported arithmetic, comparison, logic, function calls, and indexing in expressions.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2000 —&lt;/strong&gt; &lt;strong&gt;List comprehensions&lt;/strong&gt; allowed expressions inside loops for fast value creation.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2006 —&lt;/strong&gt; &lt;strong&gt;Generator expressions&lt;/strong&gt; added lazy evaluation to Python expressions using parentheses.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2018 —&lt;/strong&gt; &lt;strong&gt;Assignment expressions&lt;/strong&gt; using &lt;code&gt;:=&lt;/code&gt; allowed values to be assigned as part of expressions.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2025 —&lt;/strong&gt; &lt;strong&gt;Python expressions&lt;/strong&gt; now include pattern match expressions, comprehension blocks, and more — all still readable and simple.&lt;/p&gt;


&lt;h2&gt;
  
  
  Problems &amp;amp; Solutions with Python Expressions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;How do you use Python expressions the right way?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python expressions let you build results from simple rules. They work with numbers, text, logic, and many more types. These problems show how Python expressions help you get useful results with just one line.&lt;/p&gt;


&lt;h3&gt;
  
  
  Problem: How do you combine values into one result in Python?
&lt;/h3&gt;

&lt;p&gt;You are writing a program that calculates prices. You have a quantity and a unit price. You want to multiply them and show the result, but you do not want to write many lines of code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to join values into one result using math.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python expressions let you combine numbers with operators.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you use arithmetic expressions to compute values.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="n"&gt;quantity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 30
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The expression &lt;code&gt;price * quantity&lt;/code&gt; computes the value and stores it in &lt;code&gt;total&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you check a condition in one line in Python?
&lt;/h3&gt;

&lt;p&gt;You are checking if someone is allowed to enter. You want to know if their age is 18 or older. You want the check to be clear and simple.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to test a value using logic.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python expressions let you compare values and get a result.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you write comparison expressions for checks.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# True
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This expression returns &lt;code&gt;True&lt;/code&gt; if the age is 18 or more. You can use it in an &lt;code&gt;if&lt;/code&gt; block or print it directly.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you call a function and use the result in Python?
&lt;/h3&gt;

&lt;p&gt;You have a string and you want to know how many letters it has. You do not want to write a separate function or loop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to call a built-in function inside an expression.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python expressions can include function calls.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you call a function and use its return value.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;len(word)&lt;/code&gt; expression returns a number, and Python assigns that value to &lt;code&gt;length&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you create a list with one line of logic in Python?
&lt;/h3&gt;

&lt;p&gt;You need a list of the first five square numbers. You do not want to write a loop. You want to write the logic in one step.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to build a new list from a pattern.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you use expressions inside a list comprehension.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you use list comprehensions as expressions.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;squares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;squares&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# [0, 1, 4, 9, 16]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The expression inside the brackets runs for each number in the range and builds the list in one line.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you assign and test a value at the same time in Python?
&lt;/h3&gt;

&lt;p&gt;You want to get input and test it in one line. You want to avoid writing the assignment and the &lt;code&gt;if&lt;/code&gt; check on separate lines.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to assign a value while checking it.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python expressions support the &lt;code&gt;:=&lt;/code&gt; assignment expression.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you assign a value and use it at once.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello world&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="nf"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Length is&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Length is 11
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The expression &lt;code&gt;(n := len(text))&lt;/code&gt; sets &lt;code&gt;n&lt;/code&gt; and returns the value. This lets you write less code and keep things clear.&lt;/p&gt;




&lt;h2&gt;
  
  
  Like, Comment, Share, and Subscribe
&lt;/h2&gt;

&lt;p&gt;Did you find this helpful? Let me know by clicking the like button below. I'd love to hear your thoughts in the comments, too! If you want to see more content like this, don't forget to subscribe. Thanks for reading!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;Mike Vincent&lt;/strong&gt;&lt;/a&gt; is an American software engineer and app developer from Los Angeles, California. &lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;More about Mike Vincent&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Quark's Outlines: Python Execution Model</title>
      <dc:creator>Mike Vincent</dc:creator>
      <pubDate>Sat, 04 Apr 2026 12:17:30 +0000</pubDate>
      <link>https://dev.to/mike-vincent/quarks-outlines-python-execution-model-46p5</link>
      <guid>https://dev.to/mike-vincent/quarks-outlines-python-execution-model-46p5</guid>
      <description>&lt;h1&gt;
  
  
  Quark’s Outlines: Python Execution Model
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Overview, Historical Timeline, Problems &amp;amp; Solutions&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  An Overview of the Python Execution Model
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is the Python execution model?
&lt;/h3&gt;

&lt;p&gt;When you run a Python program, Python follows a process to decide what happens and in what order. This process is called the &lt;strong&gt;Python execution model&lt;/strong&gt;. It controls how code is grouped, how values are stored, and what happens when things go wrong.&lt;/p&gt;

&lt;p&gt;Python organizes your code into &lt;strong&gt;code blocks&lt;/strong&gt;. Each block runs in an &lt;strong&gt;execution frame&lt;/strong&gt;, which stores the block’s state and connects to the next block. Each frame uses one or more &lt;strong&gt;name spaces&lt;/strong&gt; to hold names and values. When something goes wrong, Python raises an &lt;strong&gt;exception&lt;/strong&gt;. These four ideas — code blocks, execution frames, name spaces, and exceptions — make up the execution model.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you run code using a frame that manages blocks, names, and errors.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Ada&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello,&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Hello, Ada
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;greet()&lt;/code&gt; runs inside a new code block and a new execution frame. The name &lt;code&gt;name&lt;/code&gt; is stored in the local name space for that frame.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Historical Timeline of the Python Execution Model
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Where did Python’s execution model come from?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python’s execution model follows ideas from earlier languages like ALGOL and Lisp, but adds features for safe name handling and flexible error control. The timeline below shows how code blocks, execution frames, name spaces, and exceptions evolved in Python.&lt;/p&gt;




&lt;h3&gt;
  
  
  People created ways to group and control program steps
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1960 —&lt;/strong&gt; &lt;strong&gt;Code blocks and scopes&lt;/strong&gt; in ALGOL introduced nested blocks and clear rules for local vs global names.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;1970s —&lt;/strong&gt; &lt;strong&gt;Stack-based frames&lt;/strong&gt; in Lisp and C made call stacks and local scope rules common.  &lt;/p&gt;


&lt;h3&gt;
  
  
  People shaped Python’s execution model
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1991 —&lt;/strong&gt; &lt;strong&gt;Code blocks and frames&lt;/strong&gt; added in Python 0.9.0 to support safe function calls and modular design.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;1995 —&lt;/strong&gt; &lt;strong&gt;Exceptions and try blocks&lt;/strong&gt; supported with &lt;code&gt;try&lt;/code&gt;, &lt;code&gt;except&lt;/code&gt;, and &lt;code&gt;raise&lt;/code&gt; keywords.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2001 —&lt;/strong&gt; &lt;strong&gt;Dynamic name space access&lt;/strong&gt; added using &lt;code&gt;globals()&lt;/code&gt; and &lt;code&gt;locals()&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2006 —&lt;/strong&gt; &lt;strong&gt;&lt;code&gt;exec&lt;/code&gt; and &lt;code&gt;eval&lt;/code&gt; enhancements&lt;/strong&gt; added optional name space arguments.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2025 —&lt;/strong&gt; &lt;strong&gt;Execution model stable&lt;/strong&gt; with strong support for interactive, script, and module-based code.&lt;/p&gt;


&lt;h2&gt;
  
  
  Problems &amp;amp; Solutions with the Python Execution Model
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;How do you use the Python execution model the right way?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python runs code in steps. Each step follows the execution model. It creates code blocks, builds frames, uses name spaces, and raises exceptions. These problems show how that works and how you can use the model to read, write, and fix your code.&lt;/p&gt;


&lt;h3&gt;
  
  
  Problem: How do you see where Python runs your code?
&lt;/h3&gt;

&lt;p&gt;You write a function and call it from another function. You want to understand where Python is running the code. You want to see what blocks are active and how they relate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to trace how Python enters and exits each code block.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python uses execution frames to track what block is running now.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you inspect the call stack using code blocks and frames.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="n"&gt;frame&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_getframe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Now running:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f_code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Now running: inner
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each time Python enters a new block, it creates a new frame that holds the code object and name spaces for that block.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you separate local and global names in Python?
&lt;/h3&gt;

&lt;p&gt;You write a function with a variable called &lt;code&gt;value&lt;/code&gt;. You also have another &lt;code&gt;value&lt;/code&gt; in the module. You want to understand which one Python uses when the function runs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You need to know where Python looks for names inside a block.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python uses two name spaces in each frame — local and global.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you use locals for function names and globals for module names.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;global&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;local&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Value is:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Value is: local
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside a function, Python uses the local name space first. Outside, it uses the global one.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you reuse a block of code with its own scope in Python?
&lt;/h3&gt;

&lt;p&gt;You want to run a set of lines many times. You do not want the names in that code to change values outside it. You want the names to stay inside.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You need to run a block of code in its own local space.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python uses a new name space for each function body.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you keep values private to a block using local name spaces.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;block&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Inside block:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;block&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Outside block:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Inside block: 2
# Outside block: 1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function creates a local name space. Its names do not affect names outside the block.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you handle an error without stopping the program in Python?
&lt;/h3&gt;

&lt;p&gt;You are running code that might fail. You want to try it and move on if it breaks. You do not want the whole program to stop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to catch a problem and keep the program running.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python uses exceptions to manage errors during execution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you catch errors using try and except blocks.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ZeroDivisionError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Cannot divide by zero.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Cannot divide by zero.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python raised an exception. The code block caught it and handled the error without stopping the program.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you inspect the current global and local names in Python?
&lt;/h3&gt;

&lt;p&gt;You want to see what values Python knows about in your code. You want to look inside the current name spaces and print the names and their values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to view the contents of the global and local name space.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python gives you built-in functions &lt;code&gt;globals()&lt;/code&gt; and &lt;code&gt;locals()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you read name spaces using built-in lookup tools.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Ada&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Globals:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;globals&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Locals:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;locals&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;

&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Globals: ['__name__', '__doc__', ..., 'x']
# Locals: ['y']
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use these tools to see what names are defined in each scope.&lt;/p&gt;




&lt;h2&gt;
  
  
  Like, Comment, Share, and Subscribe
&lt;/h2&gt;

&lt;p&gt;Did you find this helpful? Let me know by clicking the like button below. I'd love to hear your thoughts in the comments, too! If you want to see more content like this, don't forget to subscribe. Thanks for reading!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;Mike Vincent&lt;/strong&gt;&lt;/a&gt; is an American software engineer and app developer from Los Angeles, California. &lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;More about Mike Vincent&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Quark's Outlines: Python Execution Frames</title>
      <dc:creator>Mike Vincent</dc:creator>
      <pubDate>Sat, 28 Mar 2026 12:16:57 +0000</pubDate>
      <link>https://dev.to/mike-vincent/quarks-outlines-python-execution-frames-4fce</link>
      <guid>https://dev.to/mike-vincent/quarks-outlines-python-execution-frames-4fce</guid>
      <description>&lt;h1&gt;
  
  
  Quark’s Outlines: Python Execution Frames
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Overview, Historical Timeline, Problems &amp;amp; Solutions&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  An Overview of Python Execution Frames
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is a Python execution frame?
&lt;/h3&gt;

&lt;p&gt;When you run a Python program, Python keeps track of each piece of code as it runs. Python does this using a structure called an &lt;strong&gt;execution frame&lt;/strong&gt;. A Python execution frame is a record of one piece of code running at one moment.&lt;/p&gt;

&lt;p&gt;You can think of it like a single page in a notebook that shows what code is running, what names are known, and what will happen next. Each time Python starts running a new code block, it creates a new execution frame. When that code finishes, the frame is removed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python uses an execution frame to hold the state of each code block.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;frame&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_getframe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Running in frame for:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f_code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Running in frame for: show
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The frame tracks which function is running, the variables in use, and where to go next.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does a Python execution frame include?
&lt;/h3&gt;

&lt;p&gt;A Python execution frame includes the code block that is running, the local and global name spaces, and pointers to other frames before it. It also contains details for debugging, like the current line number and the file name.&lt;/p&gt;

&lt;p&gt;Each frame runs one code block. This can be a module, function, class, string passed to &lt;code&gt;eval()&lt;/code&gt;, a file passed to &lt;code&gt;execfile()&lt;/code&gt;, or input from the interpreter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python uses a new frame for each block of code.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_getframe&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;f_back&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f_code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# outer
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each frame links to the one before it. This lets Python trace how your program got to the current point.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Historical Timeline of Python Execution Frames
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Where do Python’s execution frames come from?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python frames are part of its stack-based execution model. Python follows the structure of older programming languages that used call stacks and execution records. Over time, frames in Python became a core part of debugging, tracing, and dynamic scope handling.&lt;/p&gt;




&lt;h3&gt;
  
  
  People invented ways to trace function calls and scope
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1960 —&lt;/strong&gt; &lt;strong&gt;Stack records&lt;/strong&gt; used in ALGOL and early structured languages to store function data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1972 —&lt;/strong&gt; &lt;strong&gt;Frame-based execution&lt;/strong&gt; in C supported nested function calls and tracked control flow.&lt;/p&gt;




&lt;h3&gt;
  
  
  People built Python’s execution model on frames
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1991 —&lt;/strong&gt; &lt;strong&gt;Python added frames&lt;/strong&gt; to manage execution and scope during function and module execution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2000 —&lt;/strong&gt; &lt;strong&gt;Dynamic evaluation support&lt;/strong&gt; using &lt;code&gt;exec&lt;/code&gt;, &lt;code&gt;eval&lt;/code&gt;, and &lt;code&gt;input()&lt;/code&gt; created frames from strings and commands.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2001 —&lt;/strong&gt; &lt;strong&gt;Frame introspection&lt;/strong&gt; via &lt;code&gt;sys._getframe()&lt;/code&gt; and traceback support gave users access to internal state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2025 —&lt;/strong&gt; &lt;strong&gt;Stable frame design&lt;/strong&gt; remained in use for debuggers, profilers, and runtime tools.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problems &amp;amp; Solutions with Python Execution Frames
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;How do you use Python execution frames the right way?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Each time a Python function runs, a new execution frame is created. The frame tracks what code is running, which names are defined, and how to continue once the code ends. These problems show how Python frames help you understand, debug, and trace code.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you find out which function is running in Python?
&lt;/h3&gt;

&lt;p&gt;You are writing a logger. You want to show which function is currently running without writing the function name by hand in every place. You need to get this from the running code itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to log the current function name.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python execution frames store the code object being run.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you get the function name from the current frame.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;whoami&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Function:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_getframe&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;f_code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;whoami&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Function: whoami
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The frame’s &lt;code&gt;f_code.co_name&lt;/code&gt; gives the name of the code block currently running.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you trace who called your function in Python?
&lt;/h3&gt;

&lt;p&gt;You want to see which function called your current function. You do not want to pass this as an argument. You want to get it from the call stack itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to find the caller of the current function.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python execution frames link to earlier frames using &lt;code&gt;f_back&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you access the previous frame with &lt;code&gt;f_back&lt;/code&gt;.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;b&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;b&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;caller&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_getframe&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;f_back&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Called by:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;caller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f_code&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Called by: a
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each frame links to the one before it, so you can see how the code was reached.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you read what variables are in use in Python?
&lt;/h3&gt;

&lt;p&gt;You are writing a tool to inspect your program. You want to read the names and values of local variables while the function is running.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to inspect the local name space during execution.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python execution frames include a dictionary of local names.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you read local variables from &lt;code&gt;f_locals&lt;/code&gt;.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;inspect&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;
    &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ok&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Locals:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_getframe&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;f_locals&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;inspect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Locals: {'x': 7, 'y': 'ok'}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The frame gives access to local names and their current values.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you run code from a string and keep track of it in Python?
&lt;/h3&gt;

&lt;p&gt;You are building a sandbox or dynamic runner. You want to run code from a string and keep track of what it is doing. You need Python to treat the string as a full code block.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to execute a string as Python code in a real frame.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python makes a new frame when running &lt;code&gt;exec()&lt;/code&gt; or &lt;code&gt;eval()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python makes an execution frame for string-based code.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;x = 5; print(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;X is&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;, x)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# X is 5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even string-based execution creates a frame with its own name spaces and control.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you debug by stepping through lines in Python?
&lt;/h3&gt;

&lt;p&gt;You want to make a tool that watches a function line by line. You need a way to trace which line is running and keep track of changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to watch Python code as it runs.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you set a trace function on the frame using &lt;code&gt;f_trace&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you trace each line of code using a frame hook.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;trace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;line&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Line&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f_lineno&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;trace&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
    &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;

&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;settrace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trace&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Line 6
# Line 7
# Line 8
# Line 9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This shows each line as it runs using the frame’s line number.&lt;/p&gt;




&lt;h2&gt;
  
  
  Like, Comment, Share, and Subscribe
&lt;/h2&gt;

&lt;p&gt;Did you find this helpful? Let me know by clicking the like button below. I'd love to hear your thoughts in the comments, too! If you want to see more content like this, don't forget to subscribe. Thanks for reading!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;Mike Vincent&lt;/strong&gt;&lt;/a&gt; is an American software engineer and app developer from Los Angeles, California. &lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;More about Mike Vincent&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Quark's Outlines: Python Exceptions</title>
      <dc:creator>Mike Vincent</dc:creator>
      <pubDate>Sat, 21 Mar 2026 12:13:46 +0000</pubDate>
      <link>https://dev.to/mike-vincent/quarks-outlines-python-exceptions-260m</link>
      <guid>https://dev.to/mike-vincent/quarks-outlines-python-exceptions-260m</guid>
      <description>&lt;h1&gt;
  
  
  Quark’s Outlines: Python Exceptions
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Overview, Historical Timeline, Problems &amp;amp; Solutions&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  An Overview of Python Exceptions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is a Python exception?
&lt;/h3&gt;

&lt;p&gt;When you run a Python program, you may hit a problem. You may divide by zero or try to open a file that does not exist. A &lt;strong&gt;Python exception&lt;/strong&gt; is a signal that something went wrong. Python stops normal work and looks for a way to handle the error.&lt;/p&gt;

&lt;p&gt;Python lets you catch the exception and run other code instead. This helps you control what happens when there is a problem. You can raise your own exceptions. You can also handle built-in ones like &lt;code&gt;ZeroDivisionError&lt;/code&gt; or &lt;code&gt;FileNotFoundError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you raise and handle exceptions to control errors.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ZeroDivisionError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;You cannot divide by zero.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# You cannot divide by zero.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;try&lt;/code&gt; block runs code. If it fails, Python jumps to the &lt;code&gt;except&lt;/code&gt; block.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do Python exceptions work?
&lt;/h3&gt;

&lt;p&gt;When a Python error happens, Python raises an exception. The exception can be caught in the same block, or it can go up to the caller. This continues until Python finds a matching &lt;code&gt;except&lt;/code&gt; block. If there is none, Python prints a backtrace and stops the program.&lt;/p&gt;

&lt;p&gt;You can also raise exceptions yourself using the &lt;code&gt;raise&lt;/code&gt; statement. You can raise built-in errors or your own custom ones.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python exceptions can be raised by code or caught with try-except.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;x must be non-negative&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ValueError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Error:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Error: x must be non-negative
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;raise&lt;/code&gt; line creates an error. The &lt;code&gt;except&lt;/code&gt; line handles it.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does Python do when no one catches the exception?
&lt;/h3&gt;

&lt;p&gt;If Python cannot find a handler, it stops your program. It prints the name of the exception, a message, and a stack trace. This helps you find out where the problem happened.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python prints a backtrace when no handler is found.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# ZeroDivisionError: division by zero
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To stop this, you must use &lt;code&gt;try...except&lt;/code&gt; to catch the error.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can Python run code even if there is an error?
&lt;/h3&gt;

&lt;p&gt;Yes. If you use &lt;code&gt;try...finally&lt;/code&gt;, Python will run the &lt;code&gt;finally&lt;/code&gt; block no matter what. You use this to clean up, close files, or undo steps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python always runs the finally block even on error.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Start&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Done&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Start
# Done
# ZeroDivisionError: division by zero
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;finally&lt;/code&gt; block runs even though the &lt;code&gt;try&lt;/code&gt; block fails.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Historical Timeline of Python Exceptions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Where do Python’s exception rules come from?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python uses exceptions to separate normal steps from error steps. This idea came from older languages like Lisp, C++, and Java. Python made it simple: raise and handle. This timeline shows how Python built its error model.&lt;/p&gt;




&lt;h3&gt;
  
  
  People invented ways to signal and handle errors
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1958 —&lt;/strong&gt; &lt;strong&gt;Interrupt and break mechanisms&lt;/strong&gt; in LISP let code jump away from failures.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;1972 —&lt;/strong&gt; &lt;strong&gt;&lt;code&gt;setjmp&lt;/code&gt; and &lt;code&gt;longjmp&lt;/code&gt; in C&lt;/strong&gt; gave programmers low-level ways to leave functions on error.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;1983 —&lt;/strong&gt; &lt;strong&gt;Exception classes in C++&lt;/strong&gt; let programs raise and catch structured errors.&lt;/p&gt;


&lt;h3&gt;
  
  
  People built Python’s exception model
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1991 —&lt;/strong&gt; &lt;strong&gt;Python 0.9.0 added exceptions&lt;/strong&gt; with &lt;code&gt;try&lt;/code&gt;, &lt;code&gt;except&lt;/code&gt;, &lt;code&gt;raise&lt;/code&gt;, and string-based identifiers.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2001 —&lt;/strong&gt; &lt;strong&gt;Class-based exceptions&lt;/strong&gt; became the standard in Python 2.2 for better clarity.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2005 —&lt;/strong&gt; &lt;strong&gt;&lt;code&gt;finally&lt;/code&gt; and cleanup rules&lt;/strong&gt; were made more reliable.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2010 —&lt;/strong&gt; &lt;strong&gt;Exception chaining&lt;/strong&gt; was added to show related errors using &lt;code&gt;__cause__&lt;/code&gt; and &lt;code&gt;__context__&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2018 —&lt;/strong&gt; &lt;strong&gt;Simplified exception hierarchy&lt;/strong&gt; organized common error types under &lt;code&gt;Exception&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2025 —&lt;/strong&gt; &lt;strong&gt;No retry-on-exception rule kept&lt;/strong&gt; to keep control simple and clear.&lt;/p&gt;


&lt;h2&gt;
  
  
  Problems &amp;amp; Solutions with Python Exceptions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;How do you use Python exceptions the right way?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python exceptions let you skip normal steps when something goes wrong. You use &lt;code&gt;try&lt;/code&gt; to test risky code. You use &lt;code&gt;except&lt;/code&gt; to react to problems. These problems show how Python exceptions help you manage error cases clearly and safely.&lt;/p&gt;


&lt;h3&gt;
  
  
  Problem: How do you stop a program crash when an error happens in Python?
&lt;/h3&gt;

&lt;p&gt;You are dividing numbers. One day the number is zero. Your program crashes. You want to stop the crash and print a friendly message instead.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to handle an error instead of stopping the program.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you catch the exception with &lt;code&gt;try&lt;/code&gt; and &lt;code&gt;except&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you catch exceptions and run backup code.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ZeroDivisionError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Cannot divide by zero.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Cannot divide by zero.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;except&lt;/code&gt; line runs when there is a &lt;code&gt;ZeroDivisionError&lt;/code&gt;, so your program stays alive.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you raise your own error when data is wrong in Python?
&lt;/h3&gt;

&lt;p&gt;You are checking input. If the value is wrong, you want to raise an error yourself. You want the program to stop unless someone handles that error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to raise an exception when something is wrong.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you use &lt;code&gt;raise&lt;/code&gt; to send an error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you raise custom exceptions when needed.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;square_root&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;x must be non-negative&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;square_root&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ValueError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Error:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Error: x must be non-negative
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;raise&lt;/code&gt; command signals the error. The &lt;code&gt;except&lt;/code&gt; command handles it.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you ensure cleanup runs even if Python fails in Python?
&lt;/h3&gt;

&lt;p&gt;You are writing to a file. If the write fails, you want to make sure the file still closes. You do not want to leave the file open forever.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want cleanup to run even when there is an error.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python runs the &lt;code&gt;finally&lt;/code&gt; block no matter what happens.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you always run final steps with &lt;code&gt;finally&lt;/code&gt;.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;data.txt&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;w&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;File closed.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# File closed.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;finally&lt;/code&gt; part runs whether the write works or not.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you catch multiple kinds of errors in Python?
&lt;/h3&gt;

&lt;p&gt;You are trying something that may fail in more than one way. Sometimes a number is bad. Sometimes the file is missing. You want to catch both.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to catch different errors in the same place.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you use many &lt;code&gt;except&lt;/code&gt; blocks or group errors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you handle different exceptions with separate logic.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;x&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;missing.txt&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;That was not a number.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;FileNotFoundError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The file does not exist.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# That was not a number.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python checks each &lt;code&gt;except&lt;/code&gt; block until it finds one that matches.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: see what caused the error
&lt;/h3&gt;

&lt;p&gt;You are debugging. An error happened, but you do not know where or why. You want to see the traceback to understand what failed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to see the error and trace without stopping the program.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python gives access to the traceback when an error is caught.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you print the full traceback using the &lt;code&gt;traceback&lt;/code&gt; module.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;traceback&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ZeroDivisionError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;traceback&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;print_exc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Traceback (most recent call last):
#   ...
# ZeroDivisionError: division by zero
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prints the full stack trace so you can see where the error happened.&lt;/p&gt;




&lt;h2&gt;
  
  
  Like, Comment, Share, and Subscribe
&lt;/h2&gt;

&lt;p&gt;Did you find this helpful? Let me know by clicking the like button below. I'd love to hear your thoughts in the comments, too! If you want to see more content like this, don't forget to subscribe. Thanks for reading!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;Mike Vincent&lt;/strong&gt;&lt;/a&gt; is an American software engineer and app developer from Los Angeles, California. &lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;More about Mike Vincent&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Quark's Outlines: Python Emulating Numeric Types</title>
      <dc:creator>Mike Vincent</dc:creator>
      <pubDate>Sat, 14 Mar 2026 12:14:41 +0000</pubDate>
      <link>https://dev.to/mike-vincent/quarks-outlines-python-emulating-numeric-types-5dem</link>
      <guid>https://dev.to/mike-vincent/quarks-outlines-python-emulating-numeric-types-5dem</guid>
      <description>&lt;h1&gt;
  
  
  Quark’s Outlines: Python Emulating Numeric Types
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Overview, Historical Timeline, Problems &amp;amp; Solutions&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  An Overview of Python Emulating Numeric Types
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What does it mean to emulate numeric types in Python?
&lt;/h3&gt;

&lt;p&gt;When you build your own Python class, you can make it act like a number. This is called &lt;strong&gt;emulating numeric types&lt;/strong&gt;. Python gives you a way to define how your object behaves with arithmetic symbols like &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, and &lt;code&gt;*&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can do this by writing special methods with names like &lt;code&gt;__add__&lt;/code&gt;, &lt;code&gt;__sub__&lt;/code&gt;, or &lt;code&gt;__mul__&lt;/code&gt;. When Python sees these symbols in your code, it calls your special method to decide what happens.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you make your own objects act like numbers.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__add__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__repr__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Box(&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Box(7)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;__add__&lt;/code&gt; method makes &lt;code&gt;+&lt;/code&gt; work on Box objects. Python sees &lt;code&gt;Box(3) + Box(4)&lt;/code&gt; and calls &lt;code&gt;Box(3).__add__(Box(4))&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What other numeric behaviors can you define in Python?
&lt;/h3&gt;

&lt;p&gt;Python lets you go beyond addition. You can define methods for subtraction (&lt;code&gt;__sub__&lt;/code&gt;), multiplication (&lt;code&gt;__mul__&lt;/code&gt;), division (&lt;code&gt;__truediv__&lt;/code&gt;), modulus (&lt;code&gt;__mod__&lt;/code&gt;), power (&lt;code&gt;__pow__&lt;/code&gt;), and more.&lt;/p&gt;

&lt;p&gt;You can also define &lt;strong&gt;reverse operations&lt;/strong&gt; like &lt;code&gt;__radd__&lt;/code&gt;, which are used when your object appears on the right side of an expression. These help Python work when mixed types are used together.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you control how your object behaves in math expressions.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__radd__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__repr__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Box(&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Box(15)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, Python tries &lt;code&gt;10 + Box(5)&lt;/code&gt;, and since &lt;code&gt;int&lt;/code&gt; does not know how to add &lt;code&gt;Box&lt;/code&gt;, it calls &lt;code&gt;Box(5).__radd__(10)&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What about unary operators and type conversion?
&lt;/h3&gt;

&lt;p&gt;Python supports &lt;strong&gt;unary&lt;/strong&gt; operations like &lt;code&gt;-x&lt;/code&gt;, &lt;code&gt;+x&lt;/code&gt;, and &lt;code&gt;abs(x)&lt;/code&gt; using methods like &lt;code&gt;__neg__&lt;/code&gt;, &lt;code&gt;__pos__&lt;/code&gt;, and &lt;code&gt;__abs__&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can also control how your object converts to &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;float&lt;/code&gt;, &lt;code&gt;oct&lt;/code&gt;, or &lt;code&gt;hex&lt;/code&gt; using &lt;code&gt;__int__&lt;/code&gt;, &lt;code&gt;__float__&lt;/code&gt;, and others.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you define how your object behaves with &lt;code&gt;abs()&lt;/code&gt; and &lt;code&gt;int()&lt;/code&gt;.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__abs__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__int__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.9&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 7
# 3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your object can now respond to Python’s built-in numeric functions.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Historical Timeline of Python Emulating Numeric Types
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Where do Python’s numeric emulation rules come from?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python borrowed the idea of numeric emulation from older object models that let programmers overload operators. These features made custom types easier to use in real math-like settings.&lt;/p&gt;




&lt;h3&gt;
  
  
  People invented operator overloading
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1972 —&lt;/strong&gt; &lt;strong&gt;C introduced binary operators&lt;/strong&gt; as fixed for built-in types, but not user-defined types.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1983 —&lt;/strong&gt; &lt;strong&gt;C++ added operator overloading&lt;/strong&gt; to let classes define how symbols like &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;-&lt;/code&gt; behave.&lt;/p&gt;




&lt;h3&gt;
  
  
  People built Python’s numeric emulation model
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1991 —&lt;/strong&gt; &lt;strong&gt;Python added &lt;code&gt;__add__&lt;/code&gt; and friends&lt;/strong&gt; to allow objects to define their math behavior.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2001 —&lt;/strong&gt; &lt;strong&gt;Python 2.2 added &lt;code&gt;__radd__&lt;/code&gt; and &lt;code&gt;__coerce__&lt;/code&gt;&lt;/strong&gt; for mixed-type operations and backward compatibility.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2008 —&lt;/strong&gt; &lt;strong&gt;Python 3 removed classic division&lt;/strong&gt; and standardized behavior using &lt;code&gt;__truediv__&lt;/code&gt; and &lt;code&gt;__floordiv__&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2012 —&lt;/strong&gt; &lt;strong&gt;Custom classes supported rich emulation&lt;/strong&gt; with clear rules for reverse and unary methods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2025 —&lt;/strong&gt; &lt;strong&gt;Python’s numeric methods remain stable&lt;/strong&gt; for all built-in types and most user-defined numeric objects.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problems &amp;amp; Solutions with Python Emulating Numeric Types
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;How do you use Python numeric emulation the right way?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python lets you build your own data types and control how they behave when used in math. These problems show how to use Python’s special methods to respond to arithmetic and conversion operations.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you let objects use &lt;code&gt;+&lt;/code&gt; in Python?
&lt;/h3&gt;

&lt;p&gt;You made a class that stores numbers, but when you try to add two of them, Python shows an error. You want to write &lt;code&gt;box1 + box2&lt;/code&gt; and get a new box with the total value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to make &lt;code&gt;+&lt;/code&gt; work between two objects of the same class.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python calls the &lt;code&gt;__add__&lt;/code&gt; method when using the &lt;code&gt;+&lt;/code&gt; operator.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you define &lt;code&gt;__add__&lt;/code&gt; to support addition.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__add__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__repr__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Box(&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Box(7)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python sees the &lt;code&gt;+&lt;/code&gt; and runs your &lt;code&gt;__add__&lt;/code&gt; method. The result is a new Box.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you support &lt;code&gt;+&lt;/code&gt; when the object is on the right in Python?
&lt;/h3&gt;

&lt;p&gt;You wrote a class that works with &lt;code&gt;Box(5) + 10&lt;/code&gt;, but &lt;code&gt;10 + Box(5)&lt;/code&gt; gives an error. You want your object to work when used on either side of the &lt;code&gt;+&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want your object to handle reversed addition.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python calls the &lt;code&gt;__radd__&lt;/code&gt; method when your object is on the right side.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you define &lt;code&gt;__radd__&lt;/code&gt; to support mixed-side addition.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__radd__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__repr__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Box(&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Box(17)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python tries the left-hand type first. If that fails, it tries &lt;code&gt;__radd__&lt;/code&gt; on the right-hand object.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you let your object return a real number in Python?
&lt;/h3&gt;

&lt;p&gt;You want your class to work with the built-in &lt;code&gt;int()&lt;/code&gt; or &lt;code&gt;float()&lt;/code&gt; functions. You need to make sure your object knows how to return the right value when these are called.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to support conversion using Python’s built-in numeric types.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python uses &lt;code&gt;__int__&lt;/code&gt;, &lt;code&gt;__float__&lt;/code&gt;, and related methods to perform conversion.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you define conversion behavior for &lt;code&gt;int()&lt;/code&gt; and &lt;code&gt;float()&lt;/code&gt;.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__int__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__float__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.9&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 3
# 7.0
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These methods let your object behave more like a real number in Python code.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you support &lt;code&gt;abs()&lt;/code&gt; for custom types in Python?
&lt;/h3&gt;

&lt;p&gt;You want your object to respond to the &lt;code&gt;abs()&lt;/code&gt; function just like a number. Right now, Python says it cannot do that. You need a way to give your class the same behavior.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to make &lt;code&gt;abs(object)&lt;/code&gt; return a numeric result.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python uses the &lt;code&gt;__abs__&lt;/code&gt; method for this.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you define &lt;code&gt;__abs__&lt;/code&gt; to support the &lt;code&gt;abs()&lt;/code&gt; function.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__abs__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 8
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You control how the object responds to &lt;code&gt;abs()&lt;/code&gt; by returning the correct value inside &lt;code&gt;__abs__&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you emulate both &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;-&lt;/code&gt; with the same logic in Python?
&lt;/h3&gt;

&lt;p&gt;You want your object to handle both &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;-&lt;/code&gt;, and maybe even &lt;code&gt;*&lt;/code&gt;, &lt;code&gt;//&lt;/code&gt;, and more. You do not want to write everything from scratch each time. You want a simple way to support math.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to define multiple arithmetic behaviors on your object.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python uses a set of method names for each symbol. You can define as many as you need.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you define multiple methods for full arithmetic support.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__add__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__sub__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__mul__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__repr__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Box(&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Box(8)
# Box(2)
# Box(15)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your object behaves like a full numeric type, supporting multiple operations as needed.&lt;/p&gt;




&lt;h2&gt;
  
  
  Like, Comment, Share, and Subscribe
&lt;/h2&gt;

&lt;p&gt;Did you find this helpful? Let me know by clicking the like button below. I'd love to hear your thoughts in the comments, too! If you want to see more content like this, don't forget to subscribe. Thanks for reading!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;Mike Vincent&lt;/strong&gt;&lt;/a&gt; is an American software engineer and app developer from Los Angeles, California. &lt;a href="https://mikevincent.dev" rel="noopener noreferrer"&gt;More about Mike Vincent&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
