<?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 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>
    <item>
      <title>Quark's Outlines: Python Emulating Callable Objects</title>
      <dc:creator>Mike Vincent</dc:creator>
      <pubDate>Sat, 07 Mar 2026 12:13:38 +0000</pubDate>
      <link>https://dev.to/mike-vincent/quarks-outlines-python-emulating-callable-objects-188d</link>
      <guid>https://dev.to/mike-vincent/quarks-outlines-python-emulating-callable-objects-188d</guid>
      <description>&lt;h1&gt;
  
  
  Quark’s Outlines: Python Emulating Callable 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 Emulating Callable Objects
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What does it mean to make a Python object callable?
&lt;/h3&gt;

&lt;p&gt;When you use parentheses in Python, you are usually calling a function. But you can also make your own objects behave like functions. If you define a method called &lt;code&gt;__call__&lt;/code&gt; inside your class, then Python will let you “call” an instance of that class as if it were a function.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;Python callable object&lt;/strong&gt; is any object that can be followed by parentheses and arguments. This includes functions, methods, and any object that defines a &lt;code&gt;__call__()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you make your own objects callable by adding &lt;code&gt;__call__&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;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;__call__&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;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;Hello, &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="s"&gt;!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="n"&gt;greet&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="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;Ada&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, Ada!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though &lt;code&gt;greet&lt;/code&gt; is not a function, calling it with &lt;code&gt;("Ada")&lt;/code&gt; runs its &lt;code&gt;__call__&lt;/code&gt; method.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why would you make a Python object callable?
&lt;/h3&gt;

&lt;p&gt;You make objects callable when you want them to act like functions but also hold some state. This is helpful when you want an object that remembers past input, has configuration settings, or behaves differently based on how it was set up.&lt;/p&gt;

&lt;p&gt;A callable object can replace a function when you need more control or context.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python callable objects can hold state and act like 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;class&lt;/span&gt; &lt;span class="nc"&gt;Counter&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;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total&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;__call__&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;amount&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;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;amount&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;total&lt;/span&gt;

&lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Counter&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;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="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="c1"&gt;# prints:
# 5
# 15
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the object &lt;code&gt;add&lt;/code&gt; behaves like a function but also remembers its internal total.&lt;/p&gt;




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

&lt;p&gt;&lt;strong&gt;Where does Python’s callable object behavior come from?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python's idea of treating anything with a &lt;code&gt;__call__()&lt;/code&gt; method as a function follows from object-oriented systems that treat behavior as part of the object. It lets you combine data and logic into one thing.&lt;/p&gt;




&lt;h3&gt;
  
  
  People invented the idea of treating objects as functions
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1970s —&lt;/strong&gt; &lt;strong&gt;Message-passing in Smalltalk&lt;/strong&gt; inspired the idea that calling a function is just sending a message to an object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1980 —&lt;/strong&gt; &lt;strong&gt;Function objects in Lisp and Scheme&lt;/strong&gt; let closures carry behavior and memory together.&lt;/p&gt;




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

&lt;p&gt;&lt;strong&gt;1991 —&lt;/strong&gt; &lt;strong&gt;Python introduced &lt;code&gt;__call__()&lt;/code&gt;&lt;/strong&gt; in version 0.9.0, making objects with &lt;code&gt;__call__&lt;/code&gt; act like functions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2001 —&lt;/strong&gt; &lt;strong&gt;Callable check added&lt;/strong&gt; with the built-in &lt;code&gt;callable()&lt;/code&gt; function to test whether an object can be called.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2025 —&lt;/strong&gt; &lt;strong&gt;Callable objects used in decorators and factories&lt;/strong&gt; in most modern Python programs for extensibility.&lt;/p&gt;




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

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

&lt;p&gt;Python callable objects help you build tools that act like functions but do more. You can store state, wrap behavior, or customize how things respond to input. These problems show when and how to define &lt;code&gt;__call__()&lt;/code&gt; in Python classes.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you make an object behave like a function in Python?
&lt;/h3&gt;

&lt;p&gt;You are building a tool that should be used like a function, but it also needs to store settings. You want the object to accept input with parentheses, but you also want to keep track of how it was set up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want an object to respond to calls like a function.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Define a &lt;code&gt;__call__()&lt;/code&gt; method inside the class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you make objects callable by defining &lt;code&gt;__call__&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;Adder&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;n&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;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__call__&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;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="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;

&lt;span class="n"&gt;add_five&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Adder&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="nf"&gt;add_five&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="c1"&gt;# prints:
# 15
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The object &lt;code&gt;add_five&lt;/code&gt; now works like a function. It uses the stored number when called.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you store state between calls in Python?
&lt;/h3&gt;

&lt;p&gt;You want to create an object that counts how many times it is used. You want it to return the count each time it runs. A regular function does not keep track of state unless you use global variables or extra code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want your callable to remember past values.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Store state in attributes, and update them in &lt;code&gt;__call__&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you build stateful callables using class 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;class&lt;/span&gt; &lt;span class="nc"&gt;Tracker&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;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;calls&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;__call__&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;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;calls&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;calls&lt;/span&gt;

&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Tracker&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;count&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;count&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 1
# 2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This object remembers how many times it has been called.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you replace a function with a class in Python?
&lt;/h3&gt;

&lt;p&gt;You are refactoring your code. A simple function is growing too complex. You want to turn it into a class that behaves the same way, so old code still works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You need an object that acts like the function it replaced.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Define a class with &lt;code&gt;__call__()&lt;/code&gt; and use it in place of the old function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you swap functions for callables with the same signature.&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;Square&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;__call__&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;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="n"&gt;x&lt;/span&gt;

&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Square&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;f&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="c1"&gt;# prints:
# 36
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The object &lt;code&gt;f&lt;/code&gt; works like a function and can be passed anywhere a function is expected.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you customize function behavior with setup in Python?
&lt;/h3&gt;

&lt;p&gt;You want to build a tool that behaves differently based on how it was created. Each instance should run the same kind of logic but with different settings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to control the behavior of each callable object.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Pass setup data to &lt;code&gt;__init__()&lt;/code&gt; and use it in &lt;code&gt;__call__()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you configure callable behavior using instance state.&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;__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;greeting&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;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;greeting&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__call__&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;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="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;greeting&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, &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="s"&gt;!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="n"&gt;hi&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="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="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;hello&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="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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;hi&lt;/span&gt;&lt;span class="p"&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="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;hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Bob&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, Ada!
# Hello, Bob!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each object behaves like a different function but follows the same class pattern.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you test if something is callable in Python?
&lt;/h3&gt;

&lt;p&gt;You have a mix of objects. Some are functions. Some are numbers. You want to run the ones that can be called, but skip the others. You need a safe way to check.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to test if an object can be used with parentheses.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Use Python’s built-in &lt;code&gt;callable()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you check if an object is callable.&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;f&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;42&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;C&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;callable&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;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;callable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# True
# False
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the object defines &lt;code&gt;__call__&lt;/code&gt;, &lt;code&gt;callable()&lt;/code&gt; returns &lt;code&gt;True&lt;/code&gt;. If not, it returns &lt;code&gt;False&lt;/code&gt;.&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 Customizing Attribute Access</title>
      <dc:creator>Mike Vincent</dc:creator>
      <pubDate>Sat, 28 Feb 2026 12:13:13 +0000</pubDate>
      <link>https://dev.to/mike-vincent/quarks-outlines-python-customizing-attribute-access-l</link>
      <guid>https://dev.to/mike-vincent/quarks-outlines-python-customizing-attribute-access-l</guid>
      <description>&lt;h1&gt;
  
  
  Quark’s Outlines: Python Customizing Attribute Access
&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 Customizing Attribute Access
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What does it mean to customize attribute access in Python?
&lt;/h3&gt;

&lt;p&gt;When you use &lt;code&gt;x.name&lt;/code&gt; in Python, the language looks for that name in the object’s data. This is called &lt;strong&gt;attribute access&lt;/strong&gt;. You can &lt;strong&gt;customize attribute access&lt;/strong&gt; in Python by defining special methods in a class.&lt;/p&gt;

&lt;p&gt;You can tell Python what to do when someone tries to get, set, or delete an attribute. These methods are &lt;code&gt;__getattr__&lt;/code&gt;, &lt;code&gt;__setattr__&lt;/code&gt;, and &lt;code&gt;__delattr__&lt;/code&gt;. They let you control how attributes behave at runtime.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you change how attributes work using special methods.&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;Example&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;__getattr__&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;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;Attribute &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="s"&gt; not found&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;Example&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="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Attribute foo not found
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python did not find &lt;code&gt;foo&lt;/code&gt;, so it called &lt;code&gt;__getattr__&lt;/code&gt;. The method returned a message instead of raising an error.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does &lt;code&gt;__getattr__&lt;/code&gt; do in Python?
&lt;/h3&gt;

&lt;p&gt;Python calls &lt;code&gt;__getattr__&lt;/code&gt; only when it cannot find the attribute in the usual places. That means the attribute is not in the instance and not in its class.&lt;/p&gt;

&lt;p&gt;You use &lt;code&gt;__getattr__&lt;/code&gt; to compute or simulate values that are not stored directly. This is useful when building proxy objects, virtual fields, or fallbacks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python uses &lt;code&gt;__getattr__&lt;/code&gt; when a normal lookup fails.&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;Example&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;__getattr__&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;name&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;42&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;Example&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="n"&gt;anything&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 42
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This class returns &lt;code&gt;42&lt;/code&gt; for any missing attribute.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does &lt;code&gt;__setattr__&lt;/code&gt; do in Python?
&lt;/h3&gt;

&lt;p&gt;Python calls &lt;code&gt;__setattr__&lt;/code&gt; every time you assign to an attribute. This method overrides the normal behavior. If you do not store the value in the object’s dictionary directly, you will get a recursive call.&lt;/p&gt;

&lt;p&gt;To avoid this, use &lt;code&gt;self.__dict__[name] = value&lt;/code&gt;. This changes the data without calling &lt;code&gt;__setattr__&lt;/code&gt; again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you intercept assignments with &lt;code&gt;__setattr__&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;Track&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;__setattr__&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;name&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="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Setting &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="s"&gt; = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&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;__dict__&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="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&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;Track&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;speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Setting speed = 100
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;__setattr__&lt;/code&gt; method gives you full control over how attributes are stored.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does &lt;code&gt;__delattr__&lt;/code&gt; do in Python?
&lt;/h3&gt;

&lt;p&gt;Python calls &lt;code&gt;__delattr__&lt;/code&gt; when you delete an attribute using &lt;code&gt;del x.name&lt;/code&gt;. If you define &lt;code&gt;__delattr__&lt;/code&gt;, you can log or change what happens when data is removed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you customize deletion with &lt;code&gt;__delattr__&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;Cleanup&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;__delattr__&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;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="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Deleting &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="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;del&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;__dict__&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="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cleanup&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="k"&gt;del&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;a&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Deleting a
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example shows how Python routes deletion through the special method.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Historical Timeline of Python Customizing Attribute Access
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Where do Python’s attribute access hooks come from?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python’s model for attribute access comes from object-oriented systems in older languages. Over time, Python added methods that let you override default behavior. These methods help you build frameworks, wrappers, and new object systems.&lt;/p&gt;




&lt;h3&gt;
  
  
  People built object models that respond to attribute access
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1980 —&lt;/strong&gt; &lt;strong&gt;Smalltalk and Lisp OOP&lt;/strong&gt; used dynamic dispatch and method lookup to model behavior flexibly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1983 —&lt;/strong&gt; &lt;strong&gt;C++ introduced operator overloading&lt;/strong&gt; that included overloading for member access and assignment.&lt;/p&gt;




&lt;h3&gt;
  
  
  People gave Python custom hooks for attributes
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1991 —&lt;/strong&gt; &lt;strong&gt;Python 0.9.0 supported attribute lookup via dictionaries&lt;/strong&gt; for object state and access.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2000 —&lt;/strong&gt; &lt;strong&gt;&lt;code&gt;__getattr__&lt;/code&gt;, &lt;code&gt;__setattr__&lt;/code&gt;, and &lt;code&gt;__delattr__&lt;/code&gt; formalized&lt;/strong&gt; in Python 2.0 to override standard behavior.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2001 —&lt;/strong&gt; &lt;strong&gt;Python descriptor protocol introduced&lt;/strong&gt; to offer deeper control for classes and fields.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2015 —&lt;/strong&gt; &lt;strong&gt;Property decorators and metaclass support expanded&lt;/strong&gt; for class-level attribute logic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2025 —&lt;/strong&gt; &lt;strong&gt;Python access model remains stable&lt;/strong&gt; with these hooks as the base for more advanced tools.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problems &amp;amp; Solutions with Python Customizing Attribute Access
&lt;/h2&gt;

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

&lt;p&gt;Python gives you special methods to take control of what happens when an attribute is read, written, or removed. These problems show how to use that control to add features and avoid common mistakes.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you provide fallback values for missing attributes in Python?
&lt;/h3&gt;

&lt;p&gt;You are building a lightweight object. Some attributes might not exist yet, but you do not want the code to fail when they are missing. You want to return a default value instead of raising an error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Python calls &lt;code&gt;__getattr__&lt;/code&gt; when an attribute does not exist.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you return fallback values using &lt;code&gt;__getattr__&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;Safe&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;__getattr__&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;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="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="s"&gt; not set&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;Safe&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="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# color not set
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This object returns a message instead of crashing when an attribute is missing.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you log changes to an object's fields in Python?
&lt;/h3&gt;

&lt;p&gt;You are tracking a program’s state. You want to print a message every time a value changes, so you can see how your object is used. You want to do this without repeating print statements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Python calls &lt;code&gt;__setattr__&lt;/code&gt; every time a field is updated.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you log updates using &lt;code&gt;__setattr__&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;Watch&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;__setattr__&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;name&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="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&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="s"&gt; set to &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&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;__dict__&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="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;

&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Watch&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;level&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# level set to 5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This helps you understand when and how your data changes.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you keep some attributes read-only in Python?
&lt;/h3&gt;

&lt;p&gt;You are building a configuration object. Some values should never change after the object is created. You want to raise an error if someone tries to update those fields.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to stop certain fields from being reassigned.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python &lt;code&gt;__setattr__&lt;/code&gt; can reject changes based on the attribute name.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you prevent changes using rules in &lt;code&gt;__setattr__&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;Locked&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;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__dict__&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;locked&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__setattr__&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;name&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;if&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;locked&lt;/span&gt;&lt;span class="sh"&gt;"&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;AttributeError&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 modify &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;locked&lt;/span&gt;&lt;span class="sh"&gt;'"&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;__dict__&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="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&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;Locked&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;mode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;test&lt;/span&gt;&lt;span class="sh"&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;locked&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Traceback (most recent call last):
# AttributeError: Cannot modify 'locked'
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The class refuses to change protected values after they are set.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you respond when someone deletes an attribute in Python?
&lt;/h3&gt;

&lt;p&gt;You have an object with important fields. If someone deletes a field, you want to log the deletion or stop it. You do not want to let data disappear silently.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to handle or block attribute deletion.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python calls &lt;code&gt;__delattr__&lt;/code&gt; when &lt;code&gt;del&lt;/code&gt; is used on a field.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you respond to deletion with &lt;code&gt;__delattr__&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;Guarded&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;__delattr__&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;name&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;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;key&lt;/span&gt;&lt;span class="sh"&gt;"&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;AttributeError&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 delete key&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="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Deleted &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="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;del&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;__dict__&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="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Guarded&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;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;value&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;del&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;key&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Traceback (most recent call last):
# AttributeError: Cannot delete key
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You control what happens when someone tries to remove part of your object.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you simulate virtual attributes in Python?
&lt;/h3&gt;

&lt;p&gt;You are building a class that holds computed values. These values are not stored in the object, but you want users to access them as if they were attributes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to define an attribute without storing it.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you return a value using &lt;code&gt;__getattr__&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you simulate fields with &lt;code&gt;__getattr__&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;Virtual&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;__getattr__&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;name&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;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;area&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;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&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;height&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;AttributeError&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="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Virtual&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&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;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;area&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 12
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;area&lt;/code&gt; is not stored, but Python calculates it when asked.&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 Coercion Rules</title>
      <dc:creator>Mike Vincent</dc:creator>
      <pubDate>Sat, 21 Feb 2026 12:13:24 +0000</pubDate>
      <link>https://dev.to/mike-vincent/quarks-outlines-python-coercion-rules-4fe7</link>
      <guid>https://dev.to/mike-vincent/quarks-outlines-python-coercion-rules-4fe7</guid>
      <description>&lt;h1&gt;
  
  
  Quark’s Outlines: Python Coercion Rules
&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 Coercion Rules
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What are Python coercion rules?
&lt;/h3&gt;

&lt;p&gt;When you use a binary operator like &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, or &lt;code&gt;*&lt;/code&gt; in Python, Python must decide how to combine two values. The values may come from different types. The process Python uses to find a common type or method is called &lt;strong&gt;coercion&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You can think of Python coercion rules as a way to keep the program moving when two types meet. Python checks if one object knows how to handle the operation. If not, it checks if the other object knows. In special cases, Python lets objects transform themselves to work better with others.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets objects work together using coercion rules.&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;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;5.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 8.0
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here Python turns the integer &lt;code&gt;3&lt;/code&gt; into a float so it can add it to &lt;code&gt;5.0&lt;/code&gt;. This is automatic coercion.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do Python coercion rules handle special cases?
&lt;/h3&gt;

&lt;p&gt;When you mix user-defined objects, Python gives your class a chance to control how it combines with others. If your object defines a method like &lt;code&gt;__add__&lt;/code&gt;, Python will call that method first. If that does not work, Python will try the other object’s &lt;code&gt;__radd__&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;In older versions of Python, you could define a method called &lt;code&gt;__coerce__&lt;/code&gt; to suggest how two objects should be made compatible. This method is now gone in Python 3, but the pattern still teaches how Python thinks about mixing types.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python checks method order to combine two 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;class&lt;/span&gt; &lt;span class="nc"&gt;MyNumber&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;__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="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Called with &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;other&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="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;MyNumber&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Called with 5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python tried to call &lt;code&gt;5.__add__()&lt;/code&gt; first, which failed. Then it called &lt;code&gt;MyNumber().__radd__(5)&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Historical Timeline of Python Coercion Rules
&lt;/h2&gt;

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

&lt;p&gt;Python’s coercion model came from older languages that needed to combine types. Over time, Python moved from automatic type mixing to giving each object control over how it responds to operations. This helped avoid errors and made programs easier to read and test.&lt;/p&gt;




&lt;h3&gt;
  
  
  People invented type conversion models
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1960 —&lt;/strong&gt; &lt;strong&gt;Type promotion in FORTRAN&lt;/strong&gt; allowed combining integers and floats by converting smaller types.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1972 —&lt;/strong&gt; &lt;strong&gt;Operator overloading in C++&lt;/strong&gt; gave user-defined types control over how operators behaved.&lt;/p&gt;




&lt;h3&gt;
  
  
  People added coercion to Python
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1991 —&lt;/strong&gt; &lt;strong&gt;Python 0.9.0&lt;/strong&gt; supported &lt;code&gt;__coerce__&lt;/code&gt; as a way for objects to convert themselves before operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2000 —&lt;/strong&gt; &lt;strong&gt;Rich comparison and binary methods&lt;/strong&gt; like &lt;code&gt;__add__&lt;/code&gt; and &lt;code&gt;__radd__&lt;/code&gt; replaced &lt;code&gt;__coerce__&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2008 —&lt;/strong&gt; &lt;strong&gt;Python 3.0&lt;/strong&gt; removed &lt;code&gt;__coerce__&lt;/code&gt;, leaving only method-based coercion and implicit numeric promotion.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2025 —&lt;/strong&gt; &lt;strong&gt;Operator behavior stable&lt;/strong&gt; with &lt;code&gt;__op__&lt;/code&gt; and &lt;code&gt;__rop__&lt;/code&gt; methods used for custom types and mixed operations.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problems &amp;amp; Solutions with Python Coercion Rules
&lt;/h2&gt;

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

&lt;p&gt;Python lets you combine different values using arithmetic and other operators. Sometimes these values are built-in types like &lt;code&gt;int&lt;/code&gt; and &lt;code&gt;float&lt;/code&gt;. Sometimes they are user-defined objects. Python’s coercion rules help these values work together without errors. These problems show how coercion works in practice.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you add two numbers of different types in Python?
&lt;/h3&gt;

&lt;p&gt;You want to add an integer and a float. You do not want to convert them yourself. You want Python to handle the types and give a proper result.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You are mixing numeric types and want the result to work.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python promotes the smaller type (int) to match the larger type (float).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you add mixed numeric types using coercion.&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;7&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;2.5&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:
# 9.5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python turned &lt;code&gt;7&lt;/code&gt; into &lt;code&gt;7.0&lt;/code&gt; and then added &lt;code&gt;7.0 + 2.5&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you control how your object reacts to a number in Python?
&lt;/h3&gt;

&lt;p&gt;You write a class and want to support &lt;code&gt;number + object&lt;/code&gt;. You want Python to call your method when the number does not know what to do.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You need to define how your object responds when used on the right side.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Define the &lt;code&gt;__radd__&lt;/code&gt; method to let your object respond to &lt;code&gt;+&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets objects define &lt;code&gt;__radd__&lt;/code&gt; to handle left-side failure.&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;Adder&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;__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="n"&gt;other&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Adder&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="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# 15
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python tried &lt;code&gt;5.__add__(a)&lt;/code&gt; but failed. Then it used &lt;code&gt;a.__radd__(5)&lt;/code&gt; which worked.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you combine two user-defined objects in Python?
&lt;/h3&gt;

&lt;p&gt;You have two custom classes. You want them to work together with &lt;code&gt;+&lt;/code&gt;. You want the first object to try the operation. If that fails, the second object should try instead.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want two objects to cooperate in a binary operation.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python tries &lt;code&gt;__add__&lt;/code&gt; first, then &lt;code&gt;__radd__&lt;/code&gt; if needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets two objects try methods in order: left then right.&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;A&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;__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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;A handles it&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&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;__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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;B handles it&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;A&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# A handles it
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;A.__add__&lt;/code&gt; worked, so Python did not call &lt;code&gt;B.__radd__&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you support &lt;code&gt;%&lt;/code&gt; with strings in Python?
&lt;/h3&gt;

&lt;p&gt;You want to use the &lt;code&gt;%&lt;/code&gt; operator to format strings with values. You want to avoid custom formatting logic and use Python’s built-in behavior.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to insert a value into a string using &lt;code&gt;%&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python uses special rules for string formatting with &lt;code&gt;%&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets strings format values using the &lt;code&gt;%&lt;/code&gt; operator.&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="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, %s!&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="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;If the left side of &lt;code&gt;%&lt;/code&gt; is a string, Python formats it and skips other coercion logic.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you multiply a list by a number in Python?
&lt;/h3&gt;

&lt;p&gt;You want to repeat a list or string several times. You want Python to handle it without writing a loop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to use multiplication with a sequence and a number.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python defines a special rule for &lt;code&gt;sequence * int&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you repeat a sequence 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="nf"&gt;print&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="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="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;ha&lt;/span&gt;&lt;span class="sh"&gt;"&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="c1"&gt;# prints:
# [1, 2, 1, 2, 1, 2]
# haha
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python knows that &lt;code&gt;*&lt;/code&gt; means repeat if one side is a list or string and the other is a 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 Code Objects</title>
      <dc:creator>Mike Vincent</dc:creator>
      <pubDate>Sat, 14 Feb 2026 12:13:34 +0000</pubDate>
      <link>https://dev.to/mike-vincent/quarks-outlines-python-code-objects-33nb</link>
      <guid>https://dev.to/mike-vincent/quarks-outlines-python-code-objects-33nb</guid>
      <description>&lt;h1&gt;
  
  
  Quark’s Outlines: Python Code 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 Code Objects
&lt;/h2&gt;

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

&lt;p&gt;When you write a Python function or module, Python first turns your code into a special internal object. This is called a &lt;strong&gt;code object&lt;/strong&gt;. A Python code object holds the compiled version of your code before it runs. It includes bytecode, variable names, line numbers, and more.&lt;/p&gt;

&lt;p&gt;You do not create a code object by hand. Python makes one when you define a function or compile a block of code. This object does not run by itself. It needs a frame or a function to run it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python stores compiled bytecode in a code 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;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="n"&gt;add&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_code&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 compiled bytecode instructions for the &lt;code&gt;add&lt;/code&gt; function.&lt;/p&gt;

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

&lt;p&gt;A code object contains details about the function’s arguments, local variables, constants, and instructions. It also stores metadata like the file name, line numbers, and the original source string. These parts help Python run the code correctly.&lt;/p&gt;

&lt;p&gt;The code object does not store global variables or default values. Those are held by the function object itself. A code object is fixed. You cannot change it after Python makes it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python uses code objects to store compiled instructions and metadata.&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;Hello, &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;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_consts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# ('Hello, ', None)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code object tells you which variables and constants the function uses.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem: How do you access a Python code object in Python?
&lt;/h3&gt;

&lt;p&gt;When you define a function, Python adds a &lt;code&gt;__code__&lt;/code&gt; attribute to it. This attribute points to the function’s code object. You can inspect the object’s parts by using attributes like &lt;code&gt;co_argcount&lt;/code&gt;, &lt;code&gt;co_code&lt;/code&gt;, or &lt;code&gt;co_names&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Python gives you this access to support tools like debuggers, disassemblers, or linters. Most programs never need to read a code object, but tools that study Python behavior often do.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python gives every function a code object in &lt;code&gt;__code__&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;f&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;f&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_argcount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# 1
&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;f&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_names&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;# ()
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can explore how Python sees your function behind the scenes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why are Python code objects important?
&lt;/h3&gt;

&lt;p&gt;Python uses code objects to run your code. A code object gives Python all the parts it needs to execute a function or a module. It keeps the logic fixed and stored in bytecode.&lt;/p&gt;

&lt;p&gt;Tools that inspect, analyze, or modify Python code often use code objects. They let Python work efficiently. They also help when you need to trace errors or debug how something runs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python depends on code objects to run your code step by step.&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;n&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;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;n&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&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;# 16
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Behind this line, Python is using the code object to run bytecode that multiplies the value.&lt;/p&gt;




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

&lt;p&gt;&lt;strong&gt;Where do Python code objects come from?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python code objects follow from early efforts to separate code from data. These objects hold compiled instructions and support tools that analyze, trace, or transform code. This timeline shows how Python shaped the idea of code objects over time.&lt;/p&gt;




&lt;h3&gt;
  
  
  People separated compiled code from runtime state.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1958 —&lt;/strong&gt; &lt;strong&gt;Early bytecode formats&lt;/strong&gt;, Lisp and FORTRAN separated compiled code from runtime data.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;1970 —&lt;/strong&gt; &lt;strong&gt;Stack frame models&lt;/strong&gt;, early VMs introduced stack frames to store function execution states.&lt;/p&gt;
&lt;h3&gt;
  
  
  People designed Python’s first code object format.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1991 —&lt;/strong&gt; &lt;strong&gt;Function and code split&lt;/strong&gt;, Python 0.9.0 separated function logic into immutable code objects.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;1994 —&lt;/strong&gt; &lt;strong&gt;Dis module introduced&lt;/strong&gt;, Python added tools to inspect and disassemble code objects.&lt;/p&gt;
&lt;h3&gt;
  
  
  People expanded code object metadata and control.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;2001 —&lt;/strong&gt; &lt;strong&gt;Metadata introspection&lt;/strong&gt;, Python 2.2 added flags and structure to describe argument kinds.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2010 —&lt;/strong&gt; &lt;strong&gt;Bytecode trace tools&lt;/strong&gt;, Python 3.x improved support for debugging using code object internals.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;2019 —&lt;/strong&gt; &lt;strong&gt;Precise line tracking&lt;/strong&gt;, Python 3.8 added better line number support and instruction offsets.  &lt;/p&gt;


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

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

&lt;p&gt;Python code objects are internal, but you still use them when you write functions or inspect behavior. These problems show how understanding code objects helps you explore and understand how Python runs your code.&lt;/p&gt;


&lt;h3&gt;
  
  
  Problem: How do you find out how many arguments a function takes in Python?
&lt;/h3&gt;

&lt;p&gt;You are reading a function and want to know how many arguments it requires. The function is used many times in your program. You want to avoid errors from calling it with the wrong number of arguments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; How do you check the number of required arguments without reading all the source code?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Python stores this number in the function’s code object using the &lt;code&gt;co_argcount&lt;/code&gt; attribute.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python shows function argument counts 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;divide&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="n"&gt;divide&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_argcount&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 &lt;code&gt;2&lt;/code&gt;, showing the function expects two arguments.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you inspect what names a function uses in Python?
&lt;/h3&gt;

&lt;p&gt;You are working with a helper function from another file. You want to see which variables it uses so you can reuse it safely in a new context. You do not want to miss any required inputs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; How do you list the variable names used in the function body?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Python keeps variable names in &lt;code&gt;co_varnames&lt;/code&gt; and external names in &lt;code&gt;co_names&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lists variable and symbol names inside 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;calc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&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;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;pi&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;calc&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;# ('a', '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;calc&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_names&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;# ('pi',)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This shows that &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are local, while &lt;code&gt;pi&lt;/code&gt; is expected from the global scope.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you debug line numbers in an error in Python?
&lt;/h3&gt;

&lt;p&gt;You run a program that fails inside a function. The traceback says there is an error on a line, but you are not sure which line the function started on. You want to know where to look.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; How do you find the first line number in the original source?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Python stores this in the &lt;code&gt;co_firstlineno&lt;/code&gt; attribute of the code object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python stores line numbers to help locate function 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;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="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;__code__&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_firstlineno&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# prints the line number
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells you where in the file the function starts, useful when scanning tracebacks.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you compare two functions' bytecode in Python?
&lt;/h3&gt;

&lt;p&gt;You are optimizing two functions. They do similar things, but you want to know if Python compiles them to the same instructions. You want to see the exact bytecode each one uses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; How do you view and compare the compiled bytecode?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Python stores bytecode in the &lt;code&gt;co_code&lt;/code&gt; attribute as a sequence of bytes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python gives you raw bytecode using the code 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;def&lt;/span&gt; &lt;span class="nf"&gt;f&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;1&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;g&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;f&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_code&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="n"&gt;__code__&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;co_code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The bytecode will differ based on the constants and operations. This lets you compare how Python compiles logic.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you find out what constants a function uses in Python?
&lt;/h3&gt;

&lt;p&gt;You are trying to extract all hardcoded values from your code. You want to scan each function and find the constant strings, numbers, or None values it uses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; How do you get the list of constants a function includes?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Python stores all literals in the &lt;code&gt;co_consts&lt;/code&gt; tuple inside the code object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lists all constant values inside 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;hello&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;Hi&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&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;hello&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_consts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# ('Hi', None)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This shows every literal in the function. You can use this to search for fixed values.&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 Code Blocks</title>
      <dc:creator>Mike Vincent</dc:creator>
      <pubDate>Sat, 07 Feb 2026 12:13:50 +0000</pubDate>
      <link>https://dev.to/mike-vincent/quarks-outlines-python-code-blocks-j0n</link>
      <guid>https://dev.to/mike-vincent/quarks-outlines-python-code-blocks-j0n</guid>
      <description>&lt;h1&gt;
  
  
  Quark’s Outlines: Python Code Blocks
&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 Code Blocks
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is a Python code block?
&lt;/h3&gt;

&lt;p&gt;When you write a Python program, you group statements together. This group is called a &lt;strong&gt;Python code block&lt;/strong&gt;. A code block is a section of code that Python can run as one unit. It can be as short as one line or as long as a full module.&lt;/p&gt;

&lt;p&gt;You use code blocks in many places. A function body is a code block. A class definition is a code block. Each module you import is also a code block. Python reads these blocks and sets up the right space to run them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you group statements into code blocks that run together.&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="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="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="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;The function body is a code block. Python runs it when you call &lt;code&gt;greet()&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where do Python code blocks appear?
&lt;/h3&gt;

&lt;p&gt;You use code blocks in many parts of Python. Some blocks run once, like a module. Some blocks run many times, like a function. Python sees each block as its own unit, even if it lives inside another.&lt;/p&gt;

&lt;p&gt;You also write code blocks in the Python shell, in scripts, or by passing strings to &lt;code&gt;eval()&lt;/code&gt; or &lt;code&gt;exec()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python sees each script, function, class, or command as a 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="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="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="c1"&gt;# prints:
# 7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you type this into a shell or script, each line is part of the same code block.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Historical Timeline of Python Code Blocks
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Where do Python’s code blocks come from?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The idea of code blocks comes from early structured programming. Over time, blocks became the way to group logic and manage scope. Python kept this model but used indentation, not braces or symbols, to mark blocks.&lt;/p&gt;




&lt;h3&gt;
  
  
  People invented structured code blocks
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1960 —&lt;/strong&gt; &lt;strong&gt;Structured code flow&lt;/strong&gt; in ALGOL introduced the idea of blocks with their own scope and control flow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1972 —&lt;/strong&gt; &lt;strong&gt;Braced blocks in C&lt;/strong&gt; grouped statements using &lt;code&gt;{}&lt;/code&gt; and used semicolons to end each line.&lt;/p&gt;




&lt;h3&gt;
  
  
  People designed Python’s code block system
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1991 —&lt;/strong&gt; &lt;strong&gt;Indented blocks&lt;/strong&gt; in Python 0.9.0 used colons and whitespace to group code without braces.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1994 —&lt;/strong&gt; &lt;strong&gt;Interactive code blocks&lt;/strong&gt; in Python shell treated each command as a block with its own scope.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2000 —&lt;/strong&gt; &lt;strong&gt;String-based blocks&lt;/strong&gt; added support for &lt;code&gt;exec()&lt;/code&gt;, &lt;code&gt;eval()&lt;/code&gt;, and &lt;code&gt;input()&lt;/code&gt; as ways to run blocks from strings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2010 —&lt;/strong&gt; &lt;strong&gt;Function scopes improved&lt;/strong&gt; with clear separation between local and global name spaces.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2025 —&lt;/strong&gt; &lt;strong&gt;Code block rules stable&lt;/strong&gt; and remain central to how Python runs structured code.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problems &amp;amp; Solutions with Python Code Blocks
&lt;/h2&gt;

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

&lt;p&gt;Python code blocks let you group logic, run sections of code, and control how names are stored. These problems show how Python blocks behave in real scripts, shells, and function calls.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you group several lines to run together in Python?
&lt;/h3&gt;

&lt;p&gt;You are writing a function to perform a task. You want to run a few lines together every time you call the function. You need Python to treat these lines as one block.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You need a way to run several statements together.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you define a code block using indentation under a &lt;code&gt;def&lt;/code&gt; or &lt;code&gt;class&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you create a code block inside a function using indentation.&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;compute&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="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&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;Result:&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="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;compute&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Result: 5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All indented lines under &lt;code&gt;def&lt;/code&gt; belong to the same code block. Python runs them together.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you define a block that runs once when imported in Python?
&lt;/h3&gt;

&lt;p&gt;You are writing a module that holds helper functions. You want it to run a small block of code the first time it is imported. This block should not run again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to define a code block that runs once.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python treats the module file itself as a code block.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets each module act as a single code block that runs when imported.&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;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 my_module.py&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:
# Running my_module.py (on first import only)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When Python loads a module, it runs its code block once. Later imports skip it.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you run a block from a string in Python?
&lt;/h3&gt;

&lt;p&gt;You are building a tool that takes user input and runs it as Python code. You want to run full expressions or statements from strings, not just from files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to turn a string into a running code block.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you use &lt;code&gt;exec()&lt;/code&gt; to treat a string as a code block.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you run code blocks from strings using exec().&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 = 7&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;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 7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The string is treated like its own code block. Python runs it in the current scope.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you run each line in the Python shell as a block in Python?
&lt;/h3&gt;

&lt;p&gt;You are typing commands into the Python shell. Each line you type runs by itself, but some lines must be grouped (like an &lt;code&gt;if&lt;/code&gt; block). You want Python to know when the block ends.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to run multi-line blocks in the shell.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python treats each top-level command or group as its own code block.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python runs each group of typed lines as its own 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="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&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;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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Big&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:
# Big
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python uses indentation to know when the block is complete before it runs it.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you tell where a block ends in Python?
&lt;/h3&gt;

&lt;p&gt;You are reading or editing code. You want to know where a block begins and ends. You want to avoid mistakes from wrong spacing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You need to see code block boundaries clearly.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python uses colons and indentation to mark code blocks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python uses indentation after colons to define the start 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="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="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="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="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Inside block
# Outside block
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only the indented line belongs to the &lt;code&gt;show()&lt;/code&gt; block. The next line with no indent is outside.&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>
