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

Chris Angelico rosuav at gmail.com
Tue May 28 16:38:32 EDT 2019


On Wed, May 29, 2019 at 6:30 AM Yanghao Hua <yanghao.py at gmail.com> wrote:
>
> > You do realise that repeatedly avoiding the question "what problem do
> > you think you are solving?" does not convincingly make the case that
> > there is a problem to be solved, don't you?
>
> It might be that I am not good enough yet to present it in a way for
> some of you to better comprehend it. But I do see positive feedbacks
> too. I plan to stop this discussion here and make a toe-to-toe
> comparison how it looks like without assignment overloading, how it
> looks like with assignment overloading, and how it compares with other
> languages like verilog and Scala/Chisel, and in the meantime to look
> into some corner cases to make sure the proposed solution can cover
> all situations.
>
> To repeat what the problem do I think I am solving? A variable, that
> behaves like an integer (e.g. all normal integer ops should just
> work), but has a different assignment behavior, such that it can be
> used to develop equally good hardware descriptions.

So what you want is for this code to behave very very weirdly:

a = thing()
a = other_thing()
a = another_thing()

Because if one of the things redefines assignment, the subsequent ones
are going to do something different.

I recommend sticking with something that's deliberately and
consciously namespaced. Descriptors give you a convenient way to do
this with dot notation, and a custom object with a __setitem__ method
lets you use bracket notation.

stuff = thing_collection()
stuff.a = other_thing()
stuff.a = another_thing()

This is much clearer, because you can see a distinct difference
between initializing the thing and making the thing do stuff.
Alternatively, using a different operator works:

a = thing()
a += other_thing()
a += another_thing()

because augmented assignment IS handled by the target object. (I don't
recommend abusing <= for this, as people and linters will get very
surprised by that. But that would also work.)

Redefining assignment is a minefield in the languages that support it.
Please can you seek a better way to do this?

I'm done posting now. If you're not listening, I'm not going to keep talking.

ChrisA


More information about the Python-ideas mailing list