[New-bugs-announce] [issue35937] Add instancemethod to types.py
Dan Snider
report at bugs.python.org
Fri Feb 8 00:36:55 EST 2019
New submission from Dan Snider <mr.assume.away at gmail.com>:
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)
----------
components: Library (Lib)
messages: 335058
nosy: bup
priority: normal
severity: normal
status: open
title: Add instancemethod to types.py
type: enhancement
versions: Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35937>
_______________________________________
More information about the New-bugs-announce
mailing list