This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author bup
Recipients bup
Date 2019-02-08.05:36:55
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1549604215.56.0.840022461634.issue35937@roundup.psfhosted.org>
In-reply-to
Content
Having the ability to add binding behavior to any callable is an incredibly useful feature if one knows how to take advantage of it, but unfortunately, nothing that currently (publicly) exists comes close to filling the gap `instancemethod`s secrecy leaves.

There really isn't anything else to say about it because it's simply amazing at what it does, and publicly exposing it has zero drawback that I could possibly imagine.
Here's a benchmark I just ran 3.6 (can't check 3.7 but shouldn't be significantly different):

if 1:
    from functools import _lru_cache_wrapper, partial
    method = type((lambda:0).__get__(0))
    for cls in object.__subclasses__():
        if cls.__name__ == 'instancemethod':
            instancemethod = cls
            break
    del cls

    class Object:
        lrx = _lru_cache_wrapper(id, 0, 0, tuple)
        imx = instancemethod(id)
        smx = property(partial(method, id))
        fnx = lambda self, f=id: f(self)

>>> ob = Object()
>>> stex.Compare.load_meth_call('ob', '', None, 'lrx', 'imx', 'smx', 'fnx')

    ------------------------------------------------------------------------
    [*] Trial duration: 0.5 seconds
    [*] Num trials: 20
    [*] Verbose: True
    [*] Imports: ('ob',)

    +-------+-----------------------+-------+-------+
    +-INDEX +-INSTRUCTION           + OPARG-+ STACK-+
    +-------------------------------+-------+-------+
    |[   22]  FOR_ITER ------------> (   12)       1|
    |[   24]  STORE_FAST ----------> (    2)       0|
    |[   26]  LOAD_FAST -----------> (    0)       1|
    |[   28]  LOAD_ATTR -----------> (    2)       1|
    |[   30]  CALL_FUNCTION -------> (    0)       1|
    |[   32]  POP_TOP                 -----        0|
    |[   34]  JUMP_ABSOLUTE -------> (   22)       0|
    +-------+-------+-----------+---+-------+-------+---+
    +-STATEMENT     +   NANOSEC-+ NUM LOOPS-+ EACH LOOP-+
    +---------------+-----------+-----------+-----------+
    | "ob.lrx()"  ->   10.000(b)   55_902(k)     179(ns)
    | "ob.imx()"  ->   10.000(b)   80_053(k)     125(ns)
    | "ob.smx()"  ->   10.000(b)   48_040(k)     208(ns)
    | "ob.fnx()"  ->   10.000(b)   42_759(k)     234(ns)
History
Date User Action Args
2019-02-08 05:36:59bupsetrecipients: + bup
2019-02-08 05:36:55bupsetmessageid: <1549604215.56.0.840022461634.issue35937@roundup.psfhosted.org>
2019-02-08 05:36:55buplinkissue35937 messages
2019-02-08 05:36:55bupcreate