<?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>Forem: Mike Vincent</title>
    <description>The latest articles on Forem by Mike Vincent (@mike-vincent).</description>
    <link>https://forem.com/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>Forem: Mike Vincent</title>
      <link>https://forem.com/mike-vincent</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mike-vincent"/>
    <language>en</language>
    <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://forem.com/mike-vincent/quarks-outlines-python-expressions-51ai</link>
      <guid>https://forem.com/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://forem.com/mike-vincent/quarks-outlines-python-execution-model-46p5</link>
      <guid>https://forem.com/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://forem.com/mike-vincent/quarks-outlines-python-execution-frames-4fce</link>
      <guid>https://forem.com/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://forem.com/mike-vincent/quarks-outlines-python-exceptions-260m</link>
      <guid>https://forem.com/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://forem.com/mike-vincent/quarks-outlines-python-emulating-numeric-types-5dem</link>
      <guid>https://forem.com/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://forem.com/mike-vincent/quarks-outlines-python-emulating-callable-objects-188d</link>
      <guid>https://forem.com/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://forem.com/mike-vincent/quarks-outlines-python-customizing-attribute-access-l</link>
      <guid>https://forem.com/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://forem.com/mike-vincent/quarks-outlines-python-coercion-rules-4fe7</link>
      <guid>https://forem.com/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://forem.com/mike-vincent/quarks-outlines-python-code-objects-33nb</link>
      <guid>https://forem.com/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://forem.com/mike-vincent/quarks-outlines-python-code-blocks-j0n</link>
      <guid>https://forem.com/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>
    <item>
      <title>Quark's Outlines: Python Built-in Methods</title>
      <dc:creator>Mike Vincent</dc:creator>
      <pubDate>Sat, 31 Jan 2026 12:13:19 +0000</pubDate>
      <link>https://forem.com/mike-vincent/quarks-outlines-python-built-in-methods-6k7</link>
      <guid>https://forem.com/mike-vincent/quarks-outlines-python-built-in-methods-6k7</guid>
      <description>&lt;h1&gt;
  
  
  Quark’s Outlines: Python Built-in Methods
&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 Built-in Methods
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is a Python built-in method?
&lt;/h3&gt;

&lt;p&gt;You may already know Python has built-in functions like &lt;code&gt;len()&lt;/code&gt; and &lt;code&gt;type()&lt;/code&gt;. A &lt;strong&gt;Python built-in method&lt;/strong&gt; is similar but attached to a built-in object. For example, you do not write &lt;code&gt;append(list, x)&lt;/code&gt;—you call &lt;code&gt;list.append(x)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Built-in methods are written in C. They live inside Python’s core types, like &lt;code&gt;list&lt;/code&gt;, &lt;code&gt;dict&lt;/code&gt;, and &lt;code&gt;str&lt;/code&gt;. Each method is tied to the object it changes or reads from. That object is passed to the method automatically.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you use built-in methods to work with built-in types.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&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="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# [1, 2, 3]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The method &lt;code&gt;append()&lt;/code&gt; is built into the list type. It changes the list by adding an item to the end.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do Python built-in methods work?
&lt;/h3&gt;

&lt;p&gt;When you call a built-in method, Python calls a C function. That C function receives the object as its first input, but you do not need to pass it yourself. Python does it for you.&lt;/p&gt;

&lt;p&gt;This only works for built-in objects. For example, when you write &lt;code&gt;s.upper()&lt;/code&gt; on a string, Python knows to pass &lt;code&gt;s&lt;/code&gt; to the C code that changes it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python built-in methods act like special tools tied to each type.&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="nf"&gt;print&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;upper&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# HELLO
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;upper()&lt;/code&gt; is not a free function—it is a method of the string object.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Historical Timeline of Python Built-in Methods
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Where do Python’s built-in methods come from?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python built-in methods came from the need to make object types more useful. In the early years, Python had functions for most tasks. But as Python grew, types like &lt;code&gt;list&lt;/code&gt;, &lt;code&gt;dict&lt;/code&gt;, and &lt;code&gt;str&lt;/code&gt; got their own tools. These tools became methods tied to the object’s shape and purpose.&lt;/p&gt;




&lt;h3&gt;
  
  
  People built tools to change objects directly
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1990 —&lt;/strong&gt; Python used plain functions for most tasks, like &lt;code&gt;len(x)&lt;/code&gt; and &lt;code&gt;type(x)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1991 —&lt;/strong&gt; Python 0.9.1 added built-in types like &lt;code&gt;list&lt;/code&gt;, &lt;code&gt;dict&lt;/code&gt;, and &lt;code&gt;str&lt;/code&gt;, each with methods for changing them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1995 —&lt;/strong&gt; Python 1.3 expanded string methods like &lt;code&gt;find&lt;/code&gt;, &lt;code&gt;replace&lt;/code&gt;, and &lt;code&gt;split&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2001 —&lt;/strong&gt; Python 2.2 made all built-in types into new-style classes, allowing full method support.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2008 —&lt;/strong&gt; Python 3.0 removed old functions and unified the method system under a consistent object model.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2025 —&lt;/strong&gt; Python built-in methods now include dozens of methods across types like set, bytes, and memoryview.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problems &amp;amp; Solutions with Python Built-in Methods
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;How do you use Python built-in methods the right way?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python built-in methods help you work with strings, lists, dictionaries, and other core types. You can call them on the object directly. These examples show how built-in methods solve real needs when working with Python data.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you add an item to a list in Python?
&lt;/h3&gt;

&lt;p&gt;You have a list of items. You want to add one more. You could make a new list, but that takes time and memory. You want to change the list directly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You need to add a new item to a list in place.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python gives you the built-in method &lt;code&gt;append()&lt;/code&gt; for lists.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you grow a list with the append() built-in method.&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;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;apple&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;banana&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;cherry&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="n"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# ['apple', 'banana', 'cherry']
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The method changes the list directly. It adds the item to the end without making a new one.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you update a value in a dictionary in Python?
&lt;/h3&gt;

&lt;p&gt;You are using a dictionary to track counts. You want to set or change a value for a key. You could use square brackets, but that can fail if the key is missing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to set a key’s value safely in a dictionary.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you use the &lt;code&gt;update()&lt;/code&gt; built-in method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you set or change many values with the update() method.&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;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Alice&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&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="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;scores&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# {'Alice': 3, 'Bob': 5}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The method adds the key if it is missing or updates it if it is there.&lt;/p&gt;




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

&lt;p&gt;You have a string from user input. It has extra spaces before or after the text. You want to remove them before using the string.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You need to trim spaces from the ends of a string.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python has the &lt;code&gt;strip()&lt;/code&gt; built-in method on strings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you clean string edges with the strip() method.&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;  Mike  &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;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# Mike
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The method keeps the middle of the string and drops only the outside spaces.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you make a list flat in Python?
&lt;/h3&gt;

&lt;p&gt;You have a list with many repeated items. You want to remove the duplicates while keeping one of each. You also want the result in place if possible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to remove repeated items from a list.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python has the &lt;code&gt;set()&lt;/code&gt; function, but to keep order, use &lt;code&gt;list(dict.fromkeys())&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you flatten a list by combining built-in 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="n"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;flat&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="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromkeys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;flat&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# prints:
# [1, 2, 3]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The method &lt;code&gt;dict.fromkeys()&lt;/code&gt; keeps the first seen of each item. You then convert it back to a list.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you find a letter in a word in Python?
&lt;/h3&gt;

&lt;p&gt;You have a string and want to know if it includes a certain letter. You want the position where the letter first appears, or nothing if it is not there.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to search inside a string for a letter.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python gives you the &lt;code&gt;find()&lt;/code&gt; method on strings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you search strings with the find() built-in method.&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;banana&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;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;n&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:
# 2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The method gives the index of the first match or &lt;code&gt;-1&lt;/code&gt; if not found.&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 Built-in Functions</title>
      <dc:creator>Mike Vincent</dc:creator>
      <pubDate>Sat, 24 Jan 2026 12:12:12 +0000</pubDate>
      <link>https://forem.com/mike-vincent/quarks-outlines-python-built-in-functions-35e7</link>
      <guid>https://forem.com/mike-vincent/quarks-outlines-python-built-in-functions-35e7</guid>
      <description>&lt;h1&gt;
  
  
  Quark’s Outlines: Python Built-in Functions
&lt;/h1&gt;

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

&lt;h2&gt;
  
  
  An Overview of Python Built-in Functions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is a Python built-in function?
&lt;/h3&gt;

&lt;p&gt;You often need to do simple things in your code like find a number’s length or sort a list. Python gives you many built-in functions to help with these tasks. These functions work without importing any modules. They are always ready to use.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;Python built-in function&lt;/strong&gt; is a name like &lt;code&gt;len&lt;/code&gt;, &lt;code&gt;print&lt;/code&gt;, or &lt;code&gt;sum&lt;/code&gt;. It maps to a function written in the C language. These functions are part of the Python core. They run fast and follow strict rules for the number and types of values they accept.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you use fast, core functions like &lt;code&gt;len()&lt;/code&gt; without imports.&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="nf"&gt;len&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="c1"&gt;# prints: 3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The number of letters in the string is returned using &lt;code&gt;len&lt;/code&gt;, a built-in function.&lt;/p&gt;

&lt;h3&gt;
  
  
  How are Python built-in functions different from user-defined ones?
&lt;/h3&gt;

&lt;p&gt;You can make your own functions using the &lt;code&gt;def&lt;/code&gt; keyword. Built-in functions are not made this way. They are written in C and added to Python when it runs. They are stored in a place called the built-in namespace.&lt;/p&gt;

&lt;p&gt;Built-in functions often have special behavior that cannot be done in Python code. Some have limits on the kinds of values they can use. Each built-in function is a wrapper around a C function, not a Python function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python built-in functions are made in C, not with &lt;code&gt;def&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="nf"&gt;callable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&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;The function &lt;code&gt;len&lt;/code&gt; is callable, but not defined in Python code. It is built-in.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Historical Timeline of Python Built-in Functions
&lt;/h2&gt;

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

&lt;p&gt;Built-in functions are part of Python’s core. They reflect both programming needs and the language’s design values. Over time, Python added more of these functions, grouped them for clarity, and made their behavior consistent across data types.&lt;/p&gt;




&lt;h3&gt;
  
  
  People created core functions for math and strings
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1989 —&lt;/strong&gt; Python’s earliest sketches include ideas for functions like &lt;code&gt;len&lt;/code&gt;, &lt;code&gt;range&lt;/code&gt;, and &lt;code&gt;print&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1991 —&lt;/strong&gt; Python 0.9.0 released with early built-in functions: &lt;code&gt;len&lt;/code&gt;, &lt;code&gt;type&lt;/code&gt;, &lt;code&gt;print&lt;/code&gt;, &lt;code&gt;range&lt;/code&gt;, &lt;code&gt;int&lt;/code&gt;, and &lt;code&gt;str&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2000 —&lt;/strong&gt; Python 2.0 introduced &lt;code&gt;zip&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, and &lt;code&gt;map&lt;/code&gt; as built-in functions.&lt;/p&gt;




&lt;h3&gt;
  
  
  People cleaned up and grouped the built-ins
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;2008 —&lt;/strong&gt; Python 3.0 removed &lt;code&gt;apply&lt;/code&gt;, &lt;code&gt;cmp&lt;/code&gt;, and others to make the built-in list shorter and more clear.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2010 —&lt;/strong&gt; &lt;code&gt;input&lt;/code&gt; replaced &lt;code&gt;raw_input&lt;/code&gt;, and &lt;code&gt;print&lt;/code&gt; became a proper function in Python 3.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2018 —&lt;/strong&gt; The list of built-in functions was finalized to about 70 entries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2024 —&lt;/strong&gt; Python’s built-in functions are stable, fast, and documented in one place.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problems &amp;amp; Solutions with Python Built-in Functions
&lt;/h2&gt;

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

&lt;p&gt;Python gives you built-in functions to do everyday tasks like checking types, counting items, or changing data. These problems show how to use built-in functions instead of writing your own logic.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you find the length of a value in Python?
&lt;/h3&gt;

&lt;p&gt;You are working with a list of names. You want to know how many names are in it. You do not want to use a loop or counter. You want the answer with a single command.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to count items in a list without writing your own loop.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you use the built-in function &lt;code&gt;len()&lt;/code&gt; to return the number of items.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you count items with &lt;code&gt;len()&lt;/code&gt;.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Ada&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;Grace&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;Linus&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;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# prints: 3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;len()&lt;/code&gt; function returns the count of items in the list.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you check if a value is a number in Python?
&lt;/h3&gt;

&lt;p&gt;You are reading values from user input. You want to check if each value is an integer. You want to avoid crashes when users type the wrong thing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to know if a value has a certain type.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you use the built-in function &lt;code&gt;isinstance()&lt;/code&gt; to check types.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you test types with &lt;code&gt;isinstance()&lt;/code&gt;.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&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;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;int&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 confirms that &lt;code&gt;age&lt;/code&gt; is an integer before you do math with it.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you turn a string into a number in Python?
&lt;/h3&gt;

&lt;p&gt;You have a string like &lt;code&gt;"42"&lt;/code&gt; and you want to use it as a number. You do not want to write your own parser. You want a simple and safe way to convert it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to convert text to a number.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you use &lt;code&gt;int()&lt;/code&gt; or &lt;code&gt;float()&lt;/code&gt; to turn strings into numbers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you convert strings to numbers with &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="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;42&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="nf"&gt;int&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="c1"&gt;# prints: 42
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The string is turned into a number you can use in math.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you get the largest value in a list in Python?
&lt;/h3&gt;

&lt;p&gt;You are comparing numbers from a set of results. You want to know the highest score. You want a simple command to get it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You need to find the largest number without writing a loop.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you use the &lt;code&gt;max()&lt;/code&gt; function to return the biggest value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you find the biggest value with &lt;code&gt;max()&lt;/code&gt;.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;88&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;95&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&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;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# prints: 100
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;max()&lt;/code&gt; function returns the largest number in the list.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem: How do you run code when you don't know the function name yet in Python?
&lt;/h3&gt;

&lt;p&gt;You are writing a program that picks a function at runtime. The function name is stored in a variable. You want to call it without writing a long if-statement.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; You want to call a function that is stored in a variable.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Python lets you call any callable object with &lt;code&gt;()&lt;/code&gt;, including built-in functions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python lets you store and call functions like 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;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;len&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Alan&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: 4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can pass &lt;code&gt;len&lt;/code&gt; around and use it like any other value.&lt;/p&gt;




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

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




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

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
