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

Yanghao Hua yanghao.py at gmail.com
Wed May 29 09:06:45 EDT 2019


On Wed, May 29, 2019 at 2:57 PM Ricky Teachey <ricky at teachey.org> wrote:
>
> For the understanding of all, it would help tremendously for you to implement a WORKING TOY example showing exactly the behavior you want in real (not theoretical) python, maybe something like this as a starting point:
>
> class SignalBehavior:
>     """"A descriptor that behaves like a HDL signal"""
>     def __init__(self):
>         # actual values for each inst stored here
>         inst_dict = dict()
>     def __get__(self,inst,owner):
>         if inst is None:
>             return self
>         # getting of a signal behavior here
>         # most basic version:
>         return self.inst_dict[inst]
>     def __set__(self,inst,value):
>         # assignment of a signal behavior here
>         # most basic version:
>         self.inst_dict[inst] = value
>
> class HDL:
>     """Every new member of this namespace has signal behavior"""
>     def __setattr__(self, attr, value):
>         # note: if attr existed already,
>         # SignalBehavior.__set__ was called
>
>         # add attr as signal behavior
>         setattr(type(self), attr, SignalBehavior())
>         # SignalBehavior.__set__ will now be called
>         setattr(self, attr, value)
>
> And then demo some simple operations like this:
>
> hdlns = HDL()
> hdlns.x = 1. # .x is a new descriptor
> hdlns.y = 2. # .y is a new descriptor
> # .z will also be a new descriptor
> hdlns.z = hdlns.x + hdlns.y
>
> ~~~ all hdlns members above behave like signals ~~~

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.

And yes, I realized a comprehensive working example will help, give me
some days ... to prepare it all.


More information about the Python-ideas mailing list