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

Ricky Teachey ricky at teachey.org
Wed May 29 08:56:58 EDT 2019


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:

<http://python.org/psf/codeofconduct/>

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 ~~~

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190529/43b9b1d0/attachment.html>


More information about the Python-ideas mailing list