Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add instancemethod to types.py #80118

Open
mr-nfamous mannequin opened this issue Feb 8, 2019 · 5 comments
Open

Add instancemethod to types.py #80118

mr-nfamous mannequin opened this issue Feb 8, 2019 · 5 comments
Labels
3.8 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@mr-nfamous
Copy link
Mannequin

mr-nfamous mannequin commented Feb 8, 2019

BPO 35937
Nosy @ronaldoussoren, @stevendaprano, @mr-nfamous, @csabella

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2019-02-08.05:36:55.534>
labels = ['3.8', 'type-feature', 'library']
title = 'Add instancemethod to types.py'
updated_at = <Date 2019-02-08.13:06:52.129>
user = 'https://github.com/mr-nfamous'

bugs.python.org fields:

activity = <Date 2019-02-08.13:06:52.129>
actor = 'cheryl.sabella'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2019-02-08.05:36:55.534>
creator = 'bup'
dependencies = []
files = []
hgrepos = []
issue_num = 35937
keywords = []
message_count = 4.0
messages = ['335058', '335072', '335074', '335077']
nosy_count = 4.0
nosy_names = ['ronaldoussoren', 'steven.daprano', 'bup', 'cheryl.sabella']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue35937'
versions = ['Python 3.8']

@mr-nfamous
Copy link
Mannequin Author

mr-nfamous mannequin commented Feb 8, 2019

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 instancemethods 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)

@mr-nfamous mr-nfamous mannequin added 3.8 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Feb 8, 2019
@ronaldoussoren
Copy link
Contributor

Could you expand on why instancemethod is amazing, other than that's it is apparently fast at something.

@stevendaprano
Copy link
Member

stevendaprano commented Feb 8, 2019

I presume you aren't referring to this:

from types import MethodType

There really isn't anything else to say about it

How about starting with why you want this and what you will do with it?

According to this post on StackOverflow:

https://stackoverflow.com/questions/40876368/where-can-i-find-an-instancemethod-in-the-python-3-standard-library

The name instancemethod looks a lot like a bound method 
object, but it turns out it's something else entirely. 
It's a weird internal thing that, according to its 
documentation, is supposed to be the new way for C types
to represent their methods, except that the standard 
C-level API for creating a type doesn't actually use it.

According to conversations on the Python issue tracker,
this feature was requested by the developers of Cython
and Pyrex.

Given that this is not used anywhere in CPython, the documentation for the C API specifically says it is not exposed to Python code, and the name is painfully misleading, we should assume that it is undocumented for a reason. It might help to persuade the core devs to expose it as part of the public Python API if you have a use-case for this.

(And if so, I think it desperately needs to change the displayed name away from "instancemethod", since that's not what it returns.)

See also # #73028.

@csabella
Copy link
Contributor

csabella commented Feb 8, 2019

Also see #55985 about documenting the signatures.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@terryjreedy
Copy link
Member

There are 3 issues here.

  1. Change the name 'instancemethod' (Steven, do you want to open an issue for this?).
  2. Uncomment it hash method (PyInstanceMethod_Type isn't hashable #73028).
  3. Expose it in types (this issue).

I am inclined to close this as insufficiently justified. Steven? @Irit?

(#55985 would only be relevant here is 3 were done.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.8 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants