[Python-3000] PEP 3124 - Overloading, Generic Functions, Interfaces, etc.
Phillip J. Eby
pje at telecommunity.com
Wed May 2 18:00:03 CEST 2007
At 07:48 PM 5/2/2007 +1200, Greg Ewing wrote:
>Is there something about generic functions that makes
>them different from methods in this regard?
Yes.
1. When you're dispatching on more than one argument type, you're likely to
have more methods involved.
2. If you are using generic functions to implement "events", or using them
AOP-style to "hook" other actions (e.g. to implement logging, persistence,
transactions, undo, etc.), then you will be *mostly* doing "before" and
"after" actions, with the occasional "around". (See also Jason's comment
quoted below.)
>>1) a lot more pleasant not to write the extra boilerplate all the time,
>
>I'd work on that by finding ways to reduce the boilerplate.
Um... I did. They're called @before and @after. :)
>There are examples, yes, but they don't come across as
>very compelling as to why there should be so many variations
>of the overloading decorator rather than a single general
>one.
I notice that you didn't respond to my point that these also make it easier
for the reader to tell what the method is doing, without needing to
carefully inspect the body.
Meanwhile, it takes less than 40 lines of code to implement both @before
and @after; if nothing else they would serve as excellent examples of how
to implement other method combinations (besides the @discount example in
the PEP). However, as it happens they are quite useful in and of
themselves. As Jason Orendorff put it:
"""In short, you have to ask yourself: am I hooking something
(before/after), implementing it (when), or just generally looking for
trouble (around)?"""
>CLOS strikes me as being the union of all Lisp dialects that
>anyone has ever used,
You seem to be confusing Common Lisp with CLOS. They are not the same thing.
Meanwhile, AspectJ and Inform 7 also include before/after/around advice for
their generic functions, so it's hardly only CLOS as an example of their
usefulness. In Inform 7, the manual notes that "after" and "instead"
(around) are the most commonly used; however, this is probably because
every action (generic function) in the language already has three method
combination phases called "check", "carry out", and "report"!
So, some of the uses of "before" that would happen in other languages get
handled as "check"-phase rules in Inform. (Also, they are called "instead"
rules because in Inform you are usually *not* invoking the overridden
action, but providing a substitute behavior.)
More information about the Python-3000
mailing list