Python and generic programming
someone at microsoft.com
Sun Nov 21 01:40:56 CET 2004
I hate to break it to you Terry, but Issac did understand the question. The
example he gave was perhaps a little fanciful, but it was on the money.
That is exactly the kind of thing I was asking about.
More typical examples of specialization may be found in the Standard
Template Library, and in the Boost Graph Library.
When you define a function in Python, you are (in effect) defining many
functions, just as Issac says. What the byte codes eventually do depends on
type information that is acted on at runtime. C++ has a mechanism
(templates) for acting on the type information similarly, but at compile
time. However, a modern C++ compiler can do something beyond that. It can
recognize special cases (which the programmer must code for) and use a
different functional form (template) for the special case than for the
generic case. Both STL and BGL use the mechanism extensively.
jdadson at yahoo dott com
"Terry Reedy" <tjreedy at udel.edu> wrote in message
news:mailman.6647.1100967514.5135.python-list at python.org...
> "Isaac To" <iketo2 at netscape.net> wrote in message
> news:87actcrhlo.fsf at sinken.local.csis.hku.hk...
> > "Terry" == Terry Reedy <tjreedy at udel.edu> writes:
> >> Terry> Specialization goes in special methods -- the ones named
> >> Terry> __xxx__ -- of which there are now 50-100.
> > No, you don't quite understand what the OP is asking for.
> No, YOU are the one who does not understand, because you apparently did
> do me the courtesy of reading my entire post, in which I quoted the
> question I answered. Here again is the OP's question about *type*
> specialization *in Python*:
> >>> 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?
> The has NOTHING to do with writing base cases in recursive C++ template
> metaprogramming (which, ironically, uses the compiler as an interpreter).
> Although there may be other answers now and in the future, the answer I
> gave is the straightforward standard answer for Python today.
> Terry J. Reedy
More information about the Python-list