Re: Peak rules example's simplification
Alexander Artemenko <[email protected]> Fri, 27 Feb 2009 09:06:40 +0300
| Newsgroups | gmane.comp.python.peak |
|---|---|
| Message-ID | <[email protected]> |
Good day, >> 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". You right, but how about DRY priciple? My variant allows to not repeat one predicate - 'isinstance(obj, (list,))'. Wat if later I'll decide to change it, to accept tuples or even iterators? Is there any way to not repeat predicates from previous methods? Something like decorators inheritance? Thanks, -- Alexander Artemenko (a.k.a. Svetlyak 40wt) Blog: http://aartemenko.com Photos: http://svetlyak.ru Jabber: [email protected]