Python and generic programming
Terry Reedy
tjreedy at udel.edu
Sat Nov 20 02:46:03 EST 2004
"Jive Dadson" <jdadson at yahoo.com> wrote in message
news:419EEEAA.D3C1046F at yahoo.com...
> How about specialization? I'm relatively new to Python. I ask for
> information, not to be argumentative.
>
> If you have, for example, several data-types for matrices, how do you
> write code that will automatically find the right routine for quickly
> multiplying a vector by a diagonal matrix represented as a vector, and
> automatically call the right code for multiplying by a sparse matrix
> represented by some sparse coding, etc?
Specialization goes in special methods -- the ones named __xxx__ -- of
which there are now 50-100. For each pair of classes, at least one of the
two must know how to do xxx on the other. There is no way to get around
the n square problem, but Python pushes it into individual operations so
you only need one function that uses the operation. So planning is
required for a system of user-defined classed such as multiple matrix
implementations.
For more, see the ref manual and the section on user classes. Also check
'special methods' in the index.
Terry J. Reedy
More information about the Python-list
mailing list