◐ Shell
clean mode source ↗

Python Pipeline Operator - Flexiple

In functional programming, a “pipe” operator lets you thread a value through a series of functions in a clear, linear style. In Python we don’t have a built-in |> operator—if you see |> in examples, that’s borrowing R’s syntax. In Python you can emulate a pipe by overloading the bitwise OR operator |. Below, we’ll explore how to implement and use a custom “pipe” in pure Python.

Why Emulate a Pipe?

Without piping, nested calls can get hard to read:

result = summarize(filter_data(load_csv("data.csv")))

A pipe style with | lets you write:

result = (
  Pipe("data.csv")
  | load_csv
  | filter_data
  | summarize
).result()

Advantages:

  • Linear, top-to-bottom flow
  • Easy to inspect or break at any step
  • Encourages small, reusable functions

Basic Pipe Implementation

Here’s how to overload | for piping:

class Pipe:
    def __init__(self, value):
        self.value = value

    def __or__(self, func):
        # Apply the function to the current value
        self.value = func(self.value)
        return self

    def result(self):
        return self.value

# Sample functions
def load_csv(path):
    return [line.strip().split(',') for line in open(path)]

def filter_data(rows):
    return [r for r in rows if int(r[2]) > 50]

def summarize(rows):
    return {"count": len(rows), "first_row": rows[0]}

# Using the pipe
report = (
    Pipe("data.csv")
    | load_csv
    | filter_data
    | summarize
).result()

print(report)

Using Third-Party Helpers

If you prefer a utility rather than rolling your own, toolz offers a pipe() function:

from toolz import pipe

report = pipe(
    "data.csv",
    load_csv,
    filter_data,
    summarize
)

print(report)

Supporting Multiple Arguments

To pipe functions with multiple parameters, adapt Pipe to track args and kwargs:

class Pipe2:
    def __init__(self, *args, **kwargs):
        self.args = args
        self.kwargs = kwargs

    def __or__(self, func):
        result = func(*self.args, **self.kwargs)
        if isinstance(result, tuple):
            self.args = result
        else:
            self.args = (result,)
        self.kwargs = {}
        return self

    def result(self):
        return self.args[0]

# Example
def multiply(x, y):
    return x * y

def add(total, x):
    return total + x

res = (
    Pipe2(5, 3)
    | multiply     # returns 15
    | (lambda t: add(t, 2))
).result()

print(res)  # 17

Conclusion

While Python doesn’t support |> natively, you can achieve a clean, readable pipeline with the | operator. Whether you write a small helper class or use toolz.pipe, piping helps keep your transformations straightforward and your code easy to follow.