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…
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 (|):
- The
__ror__method gets called first when usinga | our_infix - It returns an
Infixinstance with a lambda that captures the first value - Then
__or__gets called when doing(a | our_infix) | b - 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:
- Be sure to clap and follow the writer ️👏️️
- Follow us: **X | [LinkedIn](https://www.linkedin.com/company/inplainenglish/) | [YouTube](https://www.youtube.com/@InPlainEnglish) | [Newsletter](https://newsletter.plainenglish.io/) | [Podcast](https://open.spotify.com/show/7qxylRWKhvZwMz2WuEoua0) | [Differ](https://differ.blog/inplainenglish) | [Twitch](https://twitch.tv/inplainenglish)**
- **Check out CoFeed, the smart way to stay up-to-date with the latest in tech 🧪**
- **Start your own free AI-powered blog on Differ** 🚀
- **Join our content creators community on Discord** 🧑🏻💻
- For more content, visit **plainenglish.io + [stackademic.com](https://stackademic.com/)**
메타데이터
- 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