Issue 36175: Identity of bound methods (original) (raw)

Created on 2019-03-04 02:42 by Abe Leite, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg337063 - (view) Author: Abe Leite (Abe Leite) Date: 2019-03-04 02:42
The following code produces unexpected behavior in all versions of Python I have tested. >>> class a: ... def method(self): pass >>> inst = a() >>> inst.method is inst.method False It appears that id(inst.method) changes each time inst.method is accessed by normal means. So the tuple (id(inst.method), id(inst.method)) will have the same item repeated, but the tuple (id(inst.method), inst.method, id(inst.method)) will not. Note that for unbound methods and other functions, this issue does not occur. This creates a transparency issue for bound instance methods taking the place of functions. My apologies if this is a design decision that has already been resolved! It just seemed like a strange behavior to me. --Abe
msg337066 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2019-03-04 03:22
It is a designed behavior. I agree that it looks strange to some users who don't know implementation. And that's why people should not use `is` normally, except some use cases (e.g. `is None`.)
msg337067 - (view) Author: Abe Leite (Abe Leite) Date: 2019-03-04 03:54
Hi Inada-san, Could you explain (or send me a link to) what happens when an instance method is accessed and why this should be different from what happens when an unbound method is accessed? The `is` keyword has been very useful for me in introspection cases and that is why I'm confused that it doesn't behave as expected here. Thank you, Abe
msg337070 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2019-03-04 05:36
> > Could you explain (or send me a link to) what happens when an instance method is accessed descriptor of function object creates bound method. You can google "descriptor python". > and why this should be different from what happens when an unbound method is accessed? No "should", just "can". `3 + 5 is 8` can be True of False by language definition, while `3 + 5 == 8` is always True. Like it, descriptor may or may not same instance by language definition. > > The `is` keyword has been very useful for me in introspection cases and that is why I'm confused that it doesn't behave as expected here. > You're confused because you're using wrong guide; `is`. There are many cases that language spec doesn't define same instance is returned or not.
msg337071 - (view) Author: Abe Leite (Abe Leite) Date: 2019-03-04 06:01
Thank you for the explanation. I looked up the documentation for descriptors and I understand better now. I appreciate it! -Abe
History
Date User Action Args
2022-04-11 14:59:11 admin set github: 80356
2019-03-04 06:01:56 Abe Leite set messages: +
2019-03-04 05:36:07 methane set messages: +
2019-03-04 03:54:21 Abe Leite set messages: +
2019-03-04 03:22:43 methane set status: open -> closednosy: + methanemessages: + resolution: not a bugstage: resolved
2019-03-04 02:42:19 Abe Leite create