False / True / O / 1 not equals !!!

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
JacquesC
Posts: 16
Joined: Sun Feb 17, 2019 5:04 am

False / True / O / 1 not equals !!!

Post by JacquesC » Mon Jul 20, 2020 4:30 am

Hello,
Does somebody have an idea to explain that :

There is an embarrassing difference beetween Python and microPython :

Code: Select all

MicroPython v1.12-120-g4ab8bee82 on 2020-02-02; ESP32 module with ESP32
>>> False == 0
False
>>> True == 1
False
========= and ==========

Code: Select all

Python 3.7.2 (/usr/local/bin/python3.7)
>>> False == 0
True
>>> True == 1
True
===================
In the same way :? :

Code: Select all

MicroPython v1.12-120-g4ab8bee82 on 2020-02-02; ESP32 module with ESP32
>>> a = False
>>> if a :
    print ("Not nul or True")
else :
    print("Nul or False")
    
Nul or False
>>> a = 0
>>> if a :
    print ("Not nul or True")
else :
    print("Nul or False")
    
Nul or False
>>> a == False
False
I think it is because the conditionnal expression if ... use int or boolean types to test if the expression is True/False or Null/not Null
but an int is not a boolean therefore 0 is not equal at False ...

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

Re: False / True / O / 1 not equals !!!

Post by kevinkk525 » Mon Jul 20, 2020 7:00 am

I'd say this is a bug, not a difference. Probably an oversight during some changes last year to the comparator.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: False / True / O / 1 not equals !!!

Post by jimmo » Mon Jul 20, 2020 7:13 am

JacquesC wrote:
Mon Jul 20, 2020 4:30 am
Does somebody have an idea to explain that :
I think this was briefly an issue, introduced sometime after the v1.12 release and subsequently fixed. As Kevin mentioned, there were some changes to the way comparisons worked early this year that briefly caused some issues.

In v1.12 (built at the v1.12 tag)

Code: Select all

MicroPython v1.12 on 2020-07-20; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> False == 0
True
In the current latest version (current master branch)

Code: Select all

MicroPython v1.12-636-gdc73f9547-dirty on 2020-07-20; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> False == 0
True
I see you have "MicroPython v1.12-120-g4ab8bee82 on 2020-02-02;" which would be right about at the time that this was being worked on.


User avatar
JacquesC
Posts: 16
Joined: Sun Feb 17, 2019 5:04 am

Re: False / True / O / 1 not equals !!!

Post by JacquesC » Mon Jul 20, 2020 8:28 am

Thank you Jimmo,
I am going to change my microPython version.

Post Reply