Page 1 of 1

comparing class methods

Posted: Thu May 28, 2020 3:07 pm
by seandepagnier
I noticed something about micropython that breaks my code:

Code: Select all

>>> class a():
...     def x(self):
...         pass
... 
>>> x=a()
>>> x.x
<bound_method>
>>> x.x==x.x
False
On micropython on x86 I get slightly different output for x.x:

Code: Select all

>>> x.x
<bound_method 7f2fd0491e20 <a object at 7f2fd0491c80>.<function x at 0x7f2fd0491a80>>
but the resulting comparison of x.x==x.x is still False on both micropythons.

In normal python the output is:

Code: Select all

>>> class a():
...  def x(self):
...   pass
... 
>>> x=a()
>>> x.x
<bound method a.x of <__main__.a instance at 0x7f7374d37550>>
>>> x.x == x.x
True

Re: comparing class methods

Posted: Fri May 29, 2020 10:28 am
by pythoncoder
This is surely a bug. There were recent changes to equality testing which may have caused a regression. I suggest you raise an issue on GitHub - if you're unclear how to do this I could do it.

Re: comparing class methods

Posted: Fri May 29, 2020 10:36 am
by kevinkk525
If I remember correctly, it is not a bug but a difference to CPython.
Each time a method is accessed, it is created on the heap and therefore does not show equality. I had the same problem with asynchronous methods.
Cpython handles it somehow differently.

I can't find a link right now but it was discussed with Damien somewhere..

Re: comparing class methods

Posted: Fri May 29, 2020 1:41 pm
by seandepagnier
Could the methods still be created on the stack but somehow tagged with the object and method to allow comparisons? It seems they are already tagged in this way on micropython on x86 because of what is printed.

If this is somehow expensive, would it be possible to delay this logic and perform it only during comparisons?

Otherwise, is this considered "not a bug" but a "feature" of micropython, or should I raise an issue on github?

Re: comparing class methods

Posted: Fri May 29, 2020 1:55 pm
by kevinkk525
I found the issue on github. It's not really a feature, more like saving code and RAM I think: https://github.com/micropython/micropython/issues/5233