← Back to list

Creating Custom Infix Operators in Python

Python offers many powerful features, but the ability to create custom infix operators isn’t widely known. Let me share this interesting…

Naveen Pandey in Python in Plain English · 2025-04-21 09:34 · 42 claps · 2.4 min read paywalled
#infix-operators #python-programming #codi̇ng #programming #infix
Open on Medium ↗
Wiki topics: 💻 · Programming

Creating Custom Infix Operators in Python

Python offers many powerful features, but the ability to create custom infix operators isn’t widely known. Let me share this interesting Python trick that allows you to define your own operators between values — something that feels almost like extending the language itself.

What are Infix Operators?

Before diving into the code, let’s quickly understand what infix operators are:

  • Infix operators are placed between two operands (like a + b)
  • Prefix operators come before their operands (like -a)
  • Postfix operators come after their operands (like a++ in other languages)

Python primarily uses infix notation for most of its operators (like +, -, *, etc.), but doesn't have a straightforward way to create custom ones.

Creating Custom Infix Operators

Here’s how we can implement a custom infix operator system in Python:

from typing import Callable, Self, Any

class Infix:
    def __init__(self, function: Callable) -> None:
        self.function = function

    def __ror__(self, other: Any) -> Self:
        return Infix(lambda x: self.function(other, x))

    def __or__(self, other: Any) -> Any:
        return self.function(other)

# Example 1: Custom multiplication operator
multiply = Infix(lambda a, b: a * b)
result = 10 | multiply | 5
print(f"10 | multiply | 5 = {result}")

# Example 2: Custom contains operator
contains = Infix(lambda a, b: b in a)
result = "Python is amazing" | contains | "i"
print(f"'Python is amazing' | contains | 'i' = {result}")

result = "Python is amazing" | contains | "X"
print(f"'Python is amazing' | contains | 'X' = {result}")

# Example 3: Custom print operator
p = Infix(lambda a, b: print(a, b))
"Hello" | p | "World"

# Example 4: More practical - a custom power operator
power = Infix(lambda a, b: a ** b)
result = 2 | power | 8
print(f"2 | power | 8 = {result}")

How It Works

The implementation relies on Python’s dunder methods (__ror__ and __or__) and pipeline operators (|):

  1. The __ror__ method gets called first when using a | our_infix
  2. It returns an Infix instance with a lambda that captures the first value
  3. Then __or__ gets called when doing (a | our_infix) | b
  4. This executes the original function with both values

Practical Uses

While this might seem like just a cool trick, it can make code more readable in certain scenarios:

  • DSLs (Domain-Specific Languages): Creating expressive mini-languages for specific tasks
  • Math expressions: Making mathematical formulas more readable
  • Data processing pipelines: Creating custom operations between processing steps
  • Testing frameworks: Building expressive assertion syntax

Limitations

This approach has some drawbacks:

  • It’s not immediately obvious to other Python developers
  • It’s more verbose than built-in operators
  • It requires the | (pipe) symbol which might conflict with its use as the bitwise OR operator
  • Performance impact from the additional function calls

Conclusion

Custom infix operators in Python represent an interesting programming technique that showcases the flexibility of the language. While it’s probably not something you’ll use in everyday code, it’s a fascinating example of Python’s extensibility.

Thank you for being a part of the community

Before you go:


메타데이터
post_id
a1c5a5cc2c71
slug
creating-custom-infix-operators-in-python-a1c5a5cc2c71
url
https://python.plainenglish.io/creating-custom-infix-operators-in-python-a1c5a5cc2c71
canonical_url
https://python.plainenglish.io/creating-custom-infix-operators-in-python-a1c5a5cc2c71
author_url
https://medium.com/@naveenpandey2706
status
ok
fetched_at
2026-07-20 06:08:36