[Python-ideas] Operator as first class citizens -- like in scala -- or yet another new operator?

Chris Angelico rosuav at gmail.com
Wed May 22 05:48:30 EDT 2019

On Wed, May 22, 2019 at 7:32 PM Yanghao Hua <yanghao.py at gmail.com> wrote:
> The .next could have been saved by using python descriptors but now
> you have to type something like "obj.signal = 5" instead of "signal =
> 5", and it does not work if you want a local signal, where signal = 5
> will always make signal to be 5, instead of feeding 5 into this
> signal.

Redefining assignment based on the data type of the left hand side is
fraught with peril. Various languages have it, or something like it,
and it can VERY quickly become a source of nightmare bugs.

> I have experimented by adding two new python operators, left arrow: <-
> and right arrow ->, which users can define their behaviors. and it
> still looks like kind of the non-blocking assignment operators in
> hardware description languages (e.g. verilog <=). Also it could be
> used to build data flows like a -> b -> c -> d -> ...

Bear in mind that the <- operator can also be interpreted as a <
comparison and - unary negation.

>>> x = 0
>>> x <- 1
>>> x = -2
>>> x <- 1

> Another side effect is this "__arrow__" call can fully replace
> descriptors and allow you to generate descriptor-like behavior on
> object initialization time instead of class definition time, e.g. in
> __init__(self,...) to dynamically create self.abc like fields which
> can be then accessed like obj.abc <- 3 etc. This currently with
> descriptors can only be achieved elegantly by using meta classes.

If you want an operator to (ab)use for this kind of thing, I would
recommend << and >>, implemented with __lshift__ and __rshift__ (and
their corresponding reflected forms). After all, you'd be following in
the footsteps of C++, and as we all know, C++ is a bastion of elegant
language design with no strange edge cases at all! :)

> This is the first time I am writing to python-ideas list, just want to
> hear what you guys think, should python actually allow operators to be
> first class citizens? does the arrow operator makes sense at all? Any
> other better ways to redefine the "assignment" behavior by user?

Your post doesn't really say much about first-class operators. You
suggested two things (allowing assignment to be redefined, and the
arrow operators), but I'm not sure what you actually mean by operators
being first-class citizens.


To be first-class citizens, operators would have to be able to be
passed to functions - for instance:

def frob(x, y, oper):
    return x oper y
assert frob(10, 20, +) == 30
assert frob(10, 20, *) == 200

The nearest Python currently has to this is the "operator" module, in
which you'll find function versions of the operators:

def frob(x, y, oper):
    return oper(x, y)
assert frob(10, 20, operator.add) == 30
assert frob(10, 20, operator.mul) == 200

Is this what you're talking about?


More information about the Python-ideas mailing list