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

Yanghao Hua yanghao.py at gmail.com
Wed May 22 15:31:39 EDT 2019

On Wed, May 22, 2019 at 2:54 PM Rhodri James <rhodri at kynesim.co.uk> wrote:
> On 22/05/2019 13:29, Yanghao Hua wrote:
> > Problem is python do not allow you to define new operators in the
> > language itself, except those pre-defined you can modify their
> > behavior. Even in case, if python would have been able to allow me to
> > redefine the behavior of "=", e.g. by checking if the left hand side
> > has a method (e.g. __assign__) to override the default behavior of the
> > equal sign, I would be 100% happy already :) I have looked into all
> > the existing operators that python supports which looks like an
> > "assignment", none has been fallen in love with me. The example in
> > Chisel w/ Scala is: they defined quite a few operators: ":=" for
> > signal assignment, "<>" for bulk signal connections etc. And you can
> > keep inventing new operators with which your design can look really
> > elegant.
> A note of caution: your design can look really elegant *as long as you
> know what your new operators mean*.  If you don't, it's looks like
> incomprehensible noise.  I have had far too many bad experiences with
> "clever" C++ with redefined operators to assume that "elegant" and
> "obvious" go hand in hand.
> If you really must prat around with redefining assignment, make the
> relevant attribute a property.

Fully acknowledge it. I am from hardware design background so I do
assume I might know what it means by elegant in hardware design. I do
agree too that overloading the equal sign is probably asking for more
problem than the problem it solves. That is the reason why I would
like to see if this arrow method I proposed makes sense, or actually
Python developers would love to make it possible for users to define
their own operators (consider in math, how many operators would
mathematicians love to see, and make it possible to write beautiful
math formula directly in Python, like the matrix operations in PEP465.

Just another example from the TLA+ formal language, if Python would
allow defining new operators, you don't even need to write a parser
for TLA+, you can write TLA+ using the mathematical operators you like
directly in Python instead.


IsSafe(S) == \/ S \subseteq Cannibals
\/ Cardinality(S \cap Cannibals) =< Cardinality(S \cap Missionaries)

More information about the Python-ideas mailing list