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

Yanghao Hua yanghao.py at gmail.com
Thu May 30 04:21:15 EDT 2019


On Thu, May 30, 2019 at 7:07 AM Stephen J. Turnbull
<turnbull.stephen.fw at u.tsukuba.ac.jp> wrote:
>
> Steven D'Aprano writes:
>
>  > You might be right, but then the first post in this thread talked
>  > about it:
>  >
>  >     I realize there is no way to overload the behavior of the
>  >     assignment operator in python
>  >
>  >     ... where signal = 5 will always make signal to be 5, instead of
>  >     feeding 5 into this signal.
>
> I've seen no hint that "signal arithmetic" is a thing, so as far as I
> can see Chris Angelico's "+=" (analogous to "feeding characters into
> the end of a string") wins.  (The OP simply ignored the argument, and
> replied "but I want '<==='".)  Or you can make the algebraists happy
> with "*=".  If you need "+=" for signal addition in an arithmetical
> sense, maybe "*=" is still available at the sacrifice of the
> possibility of saying "signal *= 5" to mean "repeat last signal 5
> times", and some degree of "WTF multiplication? oh yeah ...".
>
>     signal = Signal()
>     signal = Signal(args)     # args might preload stuff into a buffer
>                               # or configure the Signal in some way
>     signal += value           # signal changes state to value, read
>                               # "signal gets next <value>".
>
> Unless Yanghao can come up with a reason not to use this such as
> (a) he needs that operator for a more natural meaning, or (b) that it
> confuses other software (eg, linters, as Chris admits "<=" would do),
> or (c) that he needs polymorphic variables such that having the
> semantics of the operator differ across classes in the union would
> confuse humans, I think this moots the whole "new operator" proposal,
> and we can move on to bikeshedding the choice of operator symbol.

Digital signals are really just integers, you can have arbitrary
length of bits, e.g. 1 bit as bool, 8 bit as byte, 32 bit as word and
hundreds of bits (we have bus width that is 1024 bits in some system).
So yes all the arithmetic operations, almost all of them can be used
for simulation, and majority of them is even directly synthesizable
into actual hardware implementation, that's the reason why I would
even like to keep the matrix operator as is rather than using it to
mean signal assignments, because I might very well want to do matrix
operations on signals as they are really just a special type of
integers.

(a) Yes I really needs all the arithmetic operators for their natural meaning

(b) Yes it confuses almost all existing python libraries that related
to arithmetic (hopefully <== does not confuse too much for linters)

(c) Yes, signals should be a different kind of integer, but only on
assignment, so introduce a new one really makes sense for me.


More information about the Python-ideas mailing list