[PEAK] Peak rules example's simplification
P.J. Eby
pje at telecommunity.com
Thu Feb 26 22:54:19 EST 2009
At 06:13 AM 2/27/2009 +0300, Alexander Artemenko wrote:
>Hi All,
>
>I found, that example at http://pypi.python.org/pypi/PEAK-Rules/ page
>can be simplified by omiting one part of the predicate in
>pprint_long_list:
>
># Skip Skip
> >>> @when(pprint, (list,))
>... def pprint_list(lst):
>... print "pretty-printing a list"
>
> >>> @when(pprint_list, "len(lst)>50") # Change 'pprint' to
> 'pprint_list' here and remove type check
>... def pprint_long_list(lst):
>... print "pretty-printing a long list"
>
># Skip Skip
>
>Is it feature or bug?
>Which are the best practices in function extention, using Peak.Rules?
Your version is still correct, but potentially much slower, since it
does dispatching *twice*... once to invoke pprint_list, and then
once to select whether to run the original pprint_list or
pprint_long_list. This is slower because the dispatch algorithm runs
more than once, and because the dispatcher can't optimize complex
conditions that are split between the two functions.
The only benefit to the way you're doing it here is that if
pprint_list is ever used independently, it will still be modified by
pprint_long_list. But I'm not sure that even that is really a "benefit".
More information about the PEAK
mailing list