Content · Programming · Python

Python Arithmetic Special Methods: __add__, __mul__, and __truediv__

__add__, __mul__ and __truediv__ define the +, * and / behavior for custom classes. Validate operand types.

2 views Published Oct 18, 2026 Updated Oct 18, 2026 Admin

__add__, __mul__ and __truediv__ define the +, * and / behavior for custom classes. Validate operand types.

Why This Matters


Python has a documented data model, but CPython also exposes implementation-level objects. Understand the distinction before using introspection or altering object behavior.

In this lesson, the focus is Arithmetic Special Methods: __add__, __mul__, and __truediv__. The aim is to understand its inputs, observable behavior, and a realistic situation where it is useful, rather than memorize a command name.

How the Example Works


The example exposes one observable part of arithmetic special methods: __add__, __mul__, and __truediv__. Identify which names reference objects, when Python calls a special method, and which result is returned or printed. For introspection and interpreter internals, treat displayed representations as diagnostic information rather than stable application data.

Step-by-Step Example


class Quantity:
    def __init__(self, value): self.value = value
    def __add__(self, other): return Quantity(self.value + other.value)
    def __mul__(self, number): return Quantity(self.value * number)
    def __truediv__(self, number): return Quantity(self.value / number)
q = Quantity(12)
print((q * 2).value, (q / 3).value)

Understanding the Result


  1. Watch these operations in order: Quantity. The arguments and return values are the central parts of the demonstration.
  2. Decide which part is a guaranteed language behavior and which part is diagnostic, interpreter-dependent detail.

Observed Output


24 4.0

This output was produced by the example in the available local Python test environment. Output that depends on the operating system, time, installed packages, or object representations may differ on your computer.

Important Details and Limitations


Arithmetic Special Methods: __add__, __mul__, and __truediv__ describes internal behavior or an advanced API. Code that relies on private CPython structures, undocumented object layouts or a specific disassembly may break between versions. Avoid evaluating or executing untrusted source, even when globals appear restricted.

Prefer stable language protocols and standard public APIs. Be skeptical of code that depends on exact refcounts, opcode sequences or object-memory layout.

Try It Yourself


Change one input in the arithmetic special methods: __add__, __mul__, and __truediv__ demonstration and predict which values, attributes or operations will be different. Add a second test case that highlights its limitation.

Before running the modified example, write down what you expect to happen. Test one normal case and one boundary, missing-input, or invalid case when the operation accepts them.

Common Beginner Mistakes


  • Assuming Arithmetic Special Methods: __add__, __mul__, and __truediv__ behaves the same way with missing, malformed, or unsupported inputs as it does in the worked example.
  • Ignoring this requirement: Arithmetic Special Methods: __add__, __mul__, and __truediv__ describes internal behavior or an advanced API.
  • Treating a small illustrative snippet as a production-ready program without testing failures, cleanup, and the target environment.

Key Takeaways


  • __add__, __mul__ and __truediv__ define the +, * and / behavior for custom classes. Validate operand types.
  • The example exposes one observable part of arithmetic special methods: __add__, __mul__, and __truediv__.
  • To confirm understanding: Change one input in the arithmetic special methods: __add__, __mul__, and __truediv__ demonstration and predict which values, attributes or operations will be different. Add a second test case that highlights its limitation.
Share this resourceSend this page to someone who may find it useful.