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

Steven D'Aprano steve at pearwood.info
Fri May 24 05:42:39 EDT 2019


On Fri, May 24, 2019 at 10:05:17AM +0200, Yanghao Hua wrote:

> Well, if python is not envisioned to be able to represent almost
> everything elegantly maybe I should indeed walk away from this idea.

Python code is supposed to look like Python, not arbitrary languages. If 
you want a language where you can invent and use arbitrary syntax, 
Python is not the language for you.

Of course we could add a new dunder method __arrow__ and a new 
operator <== but *why*? What would it do? What types would support it?

PEP 465 had a concrete purpose for its new operator, and although no 
built-in object supported the @ operator, we understood why Numpy wanted 
it. But I do not understand why you want an arrow operator or what you 
will do with it.

What should built-in types do with this new dunder?

    "Hello world!" <== "aardvark"

    23.5 <== 1.25

    [1, 2, 3] <== 4


Have you considered the __ilshift__ method?

    signal <<= 5


Or perhaps a preprocessor, such as "Like Python" (a joke) or Coconut 
(serious)?

https://jon.how/likepython/

http://coconut-lang.org/

Perhaps something like that will solve your problem.


> (This is sad for me ... I have been saying and believing python is
> envisioned that way for more than 20 years now). Your argument could
> be applied on PEP465 as well, where the only justification is to make
> matrix operation prettier.

Yes. You should study PEP 465. It is one of the best PEPs in Python's 
history. If you want your proposal to succeed, you should think "How can 
I write this to be more like PEP 465?"



> I have explained the problem of use
> descriptors in previous replies, where you cannot have a local signal,
> e.g. obj.signal = thing # works, but local_signal = thing # doesn't
> work.

I am completely aware that we can't hook into assignment except with 
attribute or item assignment. But I still do not understand why you want 
to hook into assignment.

Perhaps I have missed something when reading your earlier posts, but I 
don't understand what you will do with this new operator. Perhaps you 
can show up your intended method?


x <== y

def __arrow__(self, other):
   # what goes here?



In your first post, you said:

    "it [item assignment using descriptors] does not work if you 
    want a local signal, where signal = 5 will always make signal 
    to be 5, instead of feeding 5 into this signal."

I am not sure if I understand what you mean by "feeding 5 into this 
signal". But if it means what I think it means, then you can give signal 
a method:

    signal.feed(5)

which is explicit and readable. Or you could use <<= as I mentioned 
above.

You also said:

    "[arrow operator] looks like kind of the non-blocking 
    assignment operators in hardware description languages"

What is "non-blocking assignment"?

Thank you,


-- 
Steven


More information about the Python-ideas mailing list