[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 10:24:46 EDT 2019
On Wed, May 29, 2019 at 3:47 PM Ricky Teachey <ricky at teachey.org> wrote:
>
> 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 =.
Will keep python-ideas and all of you posted.
>>
>> 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.
Take my previous example of class C which uses class A and B, let's
integrate this ns namespace:
class A:
def __init__(self, in, o1, o2):
self.in = in
self.o1 = o1 ; self.o2 = o2
def process(self):
# is self.in, self.o1 and self.o2 are still descriptor here to be used?
class C:
def __init__(self, in, out):
ns.in = 0
ns.o1 = 0
ns.o2 = 0
# how should it be passed to A()?
a = A(ns.in, ns.o1, ns.o2) # ?? doesn't work ...
a = A(ns) # ok this works, but how does class A's instance a
figures out is it ns.in/o1/o2 need to be used?
a = A(ns, ["in", "o1", "o2"]) # maybe this will do the trick?
you see, it starts to melt down ... of course it can work ... just
doesn't feel right does it?
> 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
I am not sure if I understood this part, the problem is how a
submodule uses this signals, it is pretty clear the top module can use
descriptors and hook up the assignment behavior.
More information about the Python-ideas
mailing list