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

Ricky Teachey ricky at teachey.org
Wed May 29 09:47:16 EDT 2019


I look forward to seeing the working example. Hopefully it's clear already,
but: I don't think anybody is yet claiming the descriptor approach is the
"correct" or "best" answer for you. But it will help a lot to demo what you
want, and it also allows you to use your first choice operator, which is =.


> Problem remains that you cannot pass hdlns.x/y around, x and y are
> really the things you want to pass around. You can pass hdlns in this
> case, but the receiving object has to figure out which signal (x or y)
> to use.
>

If you can pass x around, you can certainly pass hdlns.x around, or
something shorter if you prefer-- ns.x, perhaps.

The problem of: "the receiving object has to figure out which signal (x or
y)
to use" seems easily addressed by creating a Signal class that knows how to
return the Right Thing™ when doing math:

from numbers import Real  # or whatever number abc is appropriate
class Signal(Real):
    # override all mathematical operations

And modify the __set__ method of the SignalBehavior descriptor so it stores
a Signal:

class SignalBehavior:
    ...
    def __set__(self,inst,value):
        self.inst_dict[inst] = value if isinstance(value,Signal) else
Signal(value)

Now any math operation you care to do can result in whatever you wish it to
be:

>>> ns = HDL()
>>> ns.x = 1
>>> ns.y = ns.x / 2
>>> ns.z = 8 / ns.y
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190529/ba46b108/attachment.html>


More information about the Python-ideas mailing list