[PEAK] FYI: Generic function API change
Phillip J. Eby
pje at telecommunity.com
Mon Jan 10 02:14:22 EST 2005
Just a heads-up; I checked in a change to the generic function API that
allows you to use chained methods (i.e., call the next-most-specific
method) without doing anything special to the generic function definition.
But, you do have to change the signature of the individual method if you
want to be able to call the next method, e.g.:
[dispatch.generic()]
def foo(bar,baz):
"""Foo bar and baz"""
[foo.when("bar>1 and baz=='spam'")]
def foo_one_spam(next_method, bar, baz):
return bar + next_method(bar,baz)
[foo.when("baz=='spam'")]
def foo_spam(bar, baz):
return 42
In this example, calling 'foo(2,"spam")' would return '44'. Notice that
only methods that need to call the next less-specific method should add the
'next_method' parameter, as this allows the method combination machinery to
stop chaining methods at that point.
This new mechanism replaces the need for using
'dispatch.generic(strategy.chained_methods)' and 'strategy.next_method' to
call the next method. The only reason that there were two different
strategies for "single best" and "chained methods" was because the latter
was more resource intensive. The new implementation incurs no additional
resource overhead when you don't have a 'next_method' parameter, so you no
longer have to decide in advance if you need it. And, it should be faster
than the old mechanism even when you do use it.
More information about the PEAK
mailing list