Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #50499

Re: Documenting builtin methods

References <mailman.4558.1373512579.3114.python-list@python.org> <51de4b6c$0$11094$c3e8da3@news.astraweb.com> <mailman.4603.1373584321.3114.python-list@python.org> <krntld$h9o$1@dont-email.me>
From Joshua Landau <joshua@landau.ws>
Date 2013-07-12 08:09 +0100
Subject Re: Documenting builtin methods
Newsgroups comp.lang.python
Message-ID <mailman.4615.1373613045.3114.python-list@python.org> (permalink)

Show all headers | View raw


On 12 July 2013 04:43, alex23 <wuwei23@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.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Documenting builtin methods Joshua Landau <joshua@landau.ws> - 2013-07-11 04:15 +0100
  Re: Documenting builtin methods alex23 <wuwei23@gmail.com> - 2013-07-11 15:35 +1000
  Re: Documenting builtin methods Steven D'Aprano <steve@pearwood.info> - 2013-07-11 06:06 +0000
    Re: Documenting builtin methods Chris Angelico <rosuav@gmail.com> - 2013-07-11 17:06 +1000
      Re: Documenting builtin methods Steven D'Aprano <steve@pearwood.info> - 2013-07-11 07:15 +0000
        Re: Documenting builtin methods Chris Angelico <rosuav@gmail.com> - 2013-07-11 17:23 +1000
    Re: Documenting builtin methods Joshua Landau <joshua@landau.ws> - 2013-07-12 00:11 +0100
      Re: Documenting builtin methods alex23 <wuwei23@gmail.com> - 2013-07-12 13:43 +1000
        Re: Documenting builtin methods Joshua Landau <joshua@landau.ws> - 2013-07-12 08:09 +0100

csiph-web