Documenting builtin methods

Chris Angelico rosuav at gmail.com
Thu Jul 11 03:23:06 EDT 2013


On Thu, Jul 11, 2013 at 5:15 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Thu, 11 Jul 2013 17:06:39 +1000, Chris Angelico wrote:
>
>> On Thu, Jul 11, 2013 at 4:06 PM, Steven D'Aprano <steve at pearwood.info>
>> wrote:
>>> I think the right solution here is the trivial:
>>>
>>> def exhaust(it):
>>>     """Doc string here."""
>>>     deque(maxlen=0).extend(it)
>>>
>>>
>>> which will be fast enough for all but the tightest inner loops. But if
>>> you really care about optimizing this:
>>>
>>>
>>> def factory():
>>>     eatit = deque(maxlen=0).extend
>>>     def exhaust_iter(it):
>>>         """Doc string goes here"""
>>>         eatit(it)
>>>     return exhaust_iter
>>>
>>> exhaust_it = factory()
>>> del factory
>>>
>>>
>>> which will be about as efficient as you can get while still having a
>>> custom docstring.
>>
>> Surely no reason to go for the factory function:
>>
>> def exhaust(it,eatit=deque(maxlen=0).extend):
>>       eatit(it)
>
> Now you have the function accept a second argument, which is public, just
> to hold a purely internal reference to something that you don't want the
> caller to replace.

True, but doesn't that happen fairly often with default args? Usually
it's in the "int=int" notation to snapshot for performance.

ChrisA



More information about the Python-list mailing list