On 12 July 2013 04:43, alex23 <wuwe...@gmail.com> wrote: > > My last post seems to have been eaten by either Thunderbird or the > EternalSeptember servers, but it contained an erroneous claim that the > straight function version performed as well as the factory one. However, in > the interim a co-worker has come up with a slightly faster variant: > > from functools import partial > from collections import deque > > class exhaust_it(partial): > """custom doc string""" > > exhaust_it = exhaust_it(deque(maxlen=0).extend) > > Shadowing the class name with the partial instance will ensure it has the > same name when accessed via help(), and it's a simple way to avoid needing > to clean up the namespace, as well.
That's beautiful. You could even trivially make a wrapper function: def wrap_docstring(function, docstring, *, name=None): class Wrapper(partial): pass Wrapper.__name__ = function.__name__ if name is None else name Wrapper.__doc__ = docstring return Wrapper(function) which is no slower. You get great introspection through the "func" attribute, too :). Also: >>> times = time_raw(), time_function(), time_factory(), time_argument_hack(), >>> time_partial() >>> [round(time/times[0], 1) for time in times] [1.0, 16.8, 3.1, 3.0, 1.8] This times almost purely the constant overhead by calling exhaust_iterabe on an empty iterable. So your friend wins the premature optimisation test, too. -- http://mail.python.org/mailman/listinfo/python-list