comparing class methods

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
seandepagnier
Posts: 15
Joined: Tue Feb 25, 2020 5:10 pm

comparing class methods

Post by seandepagnier » Thu May 28, 2020 3:07 pm

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

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: comparing class methods

Post by pythoncoder » Fri May 29, 2020 10:28 am

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.
Peter Hinch
Index to my micropython libraries.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: comparing class methods

Post by kevinkk525 » Fri May 29, 2020 10:36 am

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..
Last edited by kevinkk525 on Fri May 29, 2020 1:54 pm, edited 1 time in total.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

seandepagnier
Posts: 15
Joined: Tue Feb 25, 2020 5:10 pm

Re: comparing class methods

Post by seandepagnier » Fri May 29, 2020 1:41 pm

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?

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: comparing class methods

Post by kevinkk525 » Fri May 29, 2020 1:55 pm

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
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Post Reply