[Python-ideas] Need feedback about exposing an operator overloading in a module

Julien Palard julien at palard.fr
Mon Oct 20 21:42:47 CEST 2014


Hi everybody,

Long long time ago, I wrote thehttps://pypi.python.org/pypi/pipe
Python module, TL;DR: a 6 lines of code decorator enabling infix
syntax.

Long time ago someone came to speak to me on IRC advising me to speak
about it here, but I was still blocked in my mind about the
exposed/leaked `|` operator overload. For me, a module should never
expose an operator overload, because it may lead to
unpredicted/unnatural/surprising behavior. But I may be wrong.

Recently I was thinking about my pipe module and got an idea: What
about exposing a single function instead of a whole bunch of
operator-overloaded functions ? Like Pipe (because I can't spot a real
name for the moment but need one for the example) and translate from:

fib() | pp.where(lambda x: x % 2 == 0)
       | pp.take_while(lambda x: x < 4000000)
       | pp.add

to

Pipe(fib()).where(lambda x: x % 2 == 0)
            .take_while(lambda x: x < 4000000)
            .add().data

It's a bit more dense but still far more readable than the
classic suffix notation:

pp.add(pp.take_while(lambdax: x < 4000000,
                      pp.where(lambda x: x % 2 == 0, fib())))

(I explicitly prefixed by `pp.` to explicityly show that using the `|`
operator we have to import either the module or every used functions,
but using Pipe we do not have to)

For clarity, where are the two implementations (Doctests permiting you to compare usability of both):
Actual Pipe module:

https://github.com/JulienPalard/Pipe/blob/master/pipe.py

Pipe module with a function instead of operator overloading:

https://github.com/JulienPalard/Pipe/blob/less_intrusive/pipe.py

So a few questions:

1) Do you think, like me, that exposing a `|` overload is bad ?

2) In the less intrusive version, do you think, like me, that .register, Pipe(...), and .data are too much ?

3) Do you want to see something permitting infix notation in Python
    standard library ?

Bests,

-- 
Julien Palard



More information about the Python-ideas mailing list