Comparing class methods using `is` is wrong? - Python Help - Discussions on Python.org
Comparing class methods using `is` is wrong? Python Help komoto48g (Kazuya O'moto) July 6, 2023, 4:25pm 1 Hi there, I ran into the problem when comparing class methods using is . The following example seems very strange to me. Why can’t I use is ? >>> class A: ... def call(self, a): ... pass ... >>> a = A() >>> a.call is a.call False NeilGirdhar (Neil Girdhar) July 6, 2023, 4:26pm 2 Print it and you’ll see? jamestwebber (James Webber) July 6, 2023, 4:35pm 3 What is the motivation for doing this?
What are you trying to check? is is typically used to check if two variables point to the same object–they aren’t just equal, they point to the same address in memory. Using it on a method is a bit odd, and I wonder what you are hoping to check here. Nah, I think this is weirder. a.call points to a specific memory bytecode address, and one might except is to work here. The tricky bit is that is uses the id() function to decide if two things are the same, and id(a.call) doesn’t give the same answer every time.
Honestly I don’t know why that is? Perhaps because it’s storing traceback info? bryevdv (Bryan Van de Ven) July 6, 2023, 4:36pm 4 I’m not sure how illuminating it is to print In [4]: id(a.call) Out[4]: 140684486544512 In [5]: id(a.call) Out[5]: 140684489158400 That really just restates what the OP sees, but does not explain it. To answer the actual question: when you define a method on a class, what Python actually does it to create a descriptor for that attribute name, and that descriptor returns a “bound” version of original plain function (i.e. that has self ) with the self parameter bound to the instance (think of what functools.partial does).
Offhand, I’m not entirely sure why these bound method objects are not cached, but certainly that would add non-trivial complexity to do so. NeilGirdhar (Neil Girdhar) July 6, 2023, 4:38pm 5 You don’t need to look at the “memory addresses”. It’s a bound method object returned by the descriptor protocol method __get__ of functions. That method is being called twice, and returning two different bound methods. I didn’t say to print the id .
I meant just print the a.call , which will show that they’re both bound methods. You can learn how bound methods are produced from the documentation. bryevdv (Bryan Van de Ven) July 6, 2023, 4:40pm 6 But that’s not really instructive either: In [6]: print(a.call) <bound method A.call of <__main__.A object at 0x7ff3d865af70>> In [7]: print(a.call) <bound method A.call of <__main__.A object at 0x7ff3d865af70>> The only id present is for the instance, which is the same in both cases, so the output looks identical in both cases.
I’m not sure a reasonable person would conclude that these are different objects based solely on this output. But regardless, it does not explain “why”, which was the actual question. jamestwebber (James Webber) July 6, 2023, 4:42pm 7 So your advice “print it and you’ll see” was actually “print it, see that this doesn’t answer anything, and then read the documentation to figure out the real answer” NeilGirdhar (Neil Girdhar) July 6, 2023, 4:43pm 8 But that’s not really instructive either: In my opinion, good pedagogy is teaching people to answer their own questions.
When you print it, you see that you’re not getting functions (what you would get if you printed A.call ), but something else. So that should be enough to give you search terms (“bound method”) to do your own investigation using the documentation. I guess it’s a philosophical question about how teaching ought to be done, but this is what helped me grow, so this is how I teach others. NeilGirdhar (Neil Girdhar) July 6, 2023, 4:47pm 9 It would be significantly slower for no benefit.
The bound method is just an aggregate of its parameters, so there can’t be any computational efficiency to caching it. Rosuav (Chris Angelico) July 6, 2023, 7:51pm 10 Notably, though, these bound method objects don’t actually exist when you simply call a method directly. So there isn’t a massive spam of these objects for every method call that ever happens, only the ones where you refer to the method without calling it (eg stashing it in a collection or something).
For a class of your own, you could actually cache these, having it so that whenever a lookup is done, the resulting bound method gets stashed into the object’s __dict__ ; but this would come at the price of defeating the aforementioned optimization, for little real benefit. Stefan2 (Stefan Pochmann) July 6, 2023, 8:50pm 11 I thought CPython does cache methods now. Does it not? Or does it only for method calls or so?
Rosuav (Chris Angelico) July 6, 2023, 9:14pm 12 It elides the method object in a call. The exact details are subject to change, but broadly speaking, x.y() doesn’t fully evaluate x.y before calling. Which covers the vast majority of situations. If you’re curious, try this on your version of Python: def f(obj): meth = obj.method meth() obj.method() import dis dis.dis(f) This will tell you how the interpeter handles method calls.
Older versions of Python would do this as “get obj, get attrinbute method, call function”, the same whether it’s being stored in the name meth or not (the only difference literally being “store into meth, load from meth”). Newer Pythons will do something a bit different, but the details do vary. Stefan2 (Stefan Pochmann) July 6, 2023, 9:40pm 13 With dis.dis(f, show_caches=True) it also shows some CACHE entries.
Do you know what those do? my dis output 1 0 RESUME 0 2 2 LOAD_FAST 0 (obj) 4 LOAD_ATTR 0 (method) 6 CACHE 0 8 CACHE 0 10 CACHE 0 12 CACHE 0 14 STORE_FAST 1 (meth) 3 16 PUSH_NULL 18 LOAD_FAST 1 (meth) 20 PRECALL 0 22 CACHE 0 24 CALL 0 26 CACHE 0 28 CACHE 0 30 CACHE 0 32 CACHE 0 34 POP_TOP 4 36 LOAD_FAST 0 (obj) 38 LOAD_METHOD 0 (method) 40 CACHE 0 42 CACHE 0 44 CACHE 0 46 CACHE 0 48 CACHE 0 50 CACHE 0 52 CACHE 0 54 CACHE 0 56 CACHE 0 58 CACHE 0 60 PRECALL 0 62 CACHE 0 64 CALL 0 66 CACHE 0 68 CACHE 0 70 CACHE 0 72 CACHE 0 74 POP_TOP 76 LOAD_CONST 0 (None) 78 RETURN_VALUE Rosuav (Chris Angelico) July 6, 2023, 9:43pm 14 Not sure, but they’re largely NOPs that can be used for other information, I think.
You might be able to dig up a thread or tracker issue with details. da-dada (Georg Klingenberg) July 6, 2023, 11:13pm 15 it’s tricky kknechtel (Karl Knechtel) July 7, 2023, 12:44am 16 In broad strokes I agree, but… Speaking from practical experience, there are very few students who can make this kind of logical inference and take this kind of initiative to translate “huh what is this result” into search terms and then understand the discussion that would be turned up that way .
And those who can do this… wouldn’t have asked the question in the first place , because simply coming up with the idea to print the a.call result is a lot easier than all the rest that you’re expecting here. ptmcg (Paul McGuire) July 12, 2023, 4:18pm 17 If you reference call via the class, you can use is : >>> class A: ... def call(self, a): ... pass ... >>> A.call is A.call True This is important when monkeypatching an existing class with custom behavior. da-dada (Georg Klingenberg) July 13, 2023, 10:01pm 18 which is the function of the binding: C blasts off into OO >>> class A: ... def call(self, a): ... pass ... >>> a = A() >>> a.call.__func__ is a.call.__func__ True jeff5 (Jeff Allen) July 21, 2023, 6:31am 19 (Off-topic) Thanks, I had not noticed that option to dis .
They provide a space in which the interpreter does bookkeeping in support of “quickening” in PEP-659 . You can regard them as NOPs or part of the space occupied by an opcode. komoto48g (Kazuya O'moto) July 28, 2023, 5:14pm 20 Thank you very much everyone for the hints and instructions especially, @Rosuav and @Stefan2 . I wanted to make an adapter function that takes a method and returns a function that can accept at least one GUI event argument; Otherwise, it returns the meth
The rest of this page is on the live site.