[PEP draft 2] Adding new math operators

Konrad Hinsen hinsen at cnrs-orleans.fr
Wed Aug 9 03:43:39 EDT 2000


hzhu at localhost.localdomain (Huaiyu Zhu) writes:

> 3. Implement a shadowing "elementwise class" and use casting to indicate the
>    operators.  For example a*b for matrix multiply, and a.E*b for
>    elementwise multiply.
> 
>    Advantage:
>    - No need for new operators.
>    - Benefits of infix operators with correct precedence rules.
>    - Clean formulas in applications.
>    
>    Disadvantage:
>    - Hard to maintain in current Python because ordinary numbers cannot have
>      class methods.  (a.E*b will fail if a is a pure number.)

That's trivial to change, numbers can in principle have attributes
(and thus methods) just like any other type. For syntactical reasons
one would have to write parentheses around number constants, e.g.
(2).E, but that should be hardly needed in practice.

>    - Difficult to implement, as this will interfere with existing method
>      calls, like .T for transpose, etc.

What kind of interference? There is certainly no shortage of attribute names!

>    - Runtime overhead of method lookup.

Negligible for types implemented in C, but significant for Python classes.
Question: how often would this lookup happen in practice?

>    - The shadowing class cannot replace a true class, because it does not
>      return its own type.  So there need to be a M class with shadow E class,
>      and an E class with shadow M class.

I don't quite understand this. In practice, the two classes would be
an "array" and a "matrix" class. Both would be standard classes with
constructors etc., there is no need for a special "shadow" class with
different behaviour. Array objects would have an attribute called
"matrix" whose value is the equivalent matrix object, and vice versa.

>    - Unnatural to mathematicians.

But very natural to object-oriented programming. How disturbing it
would be for mathematicians depends a lot on the frequency of
conversion constructs; in my experience they would be rare.

I think this approach deserves a much more serious study, because it
is much more "Python-like", and requires no syntactical changes.
Lobbying for attributes on number types will be a lot easier than
lobbying for new operators.
-- 
-------------------------------------------------------------------------------
Konrad Hinsen                            | E-Mail: hinsen at cnrs-orleans.fr
Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-2.38.25.55.69
Rue Charles Sadron                       | Fax:  +33-2.38.63.15.17
45071 Orleans Cedex 2                    | Deutsch/Esperanto/English/
France                                   | Nederlands/Francais
-------------------------------------------------------------------------------



More information about the Python-list mailing list