Operators for everything (was Re: Operators for matrix)

Paul Prescod paul at prescod.net
Mon Jul 24 22:38:36 EDT 2000


Huaiyu Zhu wrote:
> 
> ...
>
> Is this meta operator allowed to be combined with existing operators, or is
> it for identifiers only?  

Various people have proposed various different versions but my
preference would be that it is for identifiers only. I think we should
use alphanumerics for new operators for the same reason we use
alphanumerics for variable and function names. So that when you come
into a piece of code that you are not familiar with, you have a chance
of understanding it. For instance, you could write this:


from matpy import elementwiseadd

ea=elementwiseadd

j = a @ea b

Now you can figure out what ea means merely by walking backwards through
the code. Even if all you got from matpy was "ea" you might have a half
a chance of guessing. OTOH, there is no way to guess whether @+ is
elementwise or matrixwise in a particular piece of code.

> If the former, then @+ @- @* @/ etc are also available.

I don't think so.

> A related question:
> 
> If @add does not need to be hard coded in C, why @+ need so?  

Neither needs to be hard-coded into the grammar. The parser could always
recognize them from the leading @-symbol. This is different from your
earlier proposals where *any sequence of symbols* could be an operator.
That's quite different.

Nevertheless, I would be extremely reluctant to add a feature that would
encourage people to do @++, @-- @+* and so forth. @increment is not so
bad. @decrement is not so bad. @positive_mul (whatever that might mean
in some particular domain) is not so bad.

I think name-based binary operators are actually pretty neat and can be
used very Pythonically. IMO, they are the right compromise between
terseness and legibility.

-- 
 Paul Prescod - Not encumbered by corporate consensus
New from Computer Associates: "Software that can 'think', sold by 
marketers who choose not to."




More information about the Python-list mailing list