Use const() for booleans?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Pete (NZ)
Posts: 1
Joined: Thu Sep 16, 2021 7:43 am

Use const() for booleans?

Post by Pete (NZ) » Thu Sep 16, 2021 7:52 am

A newbie Q here (first post!)... :) ...

The docs say...
The argument to const() may be anything which, at compile time, evaluates to an integer e.g. 0x100 or 1 << 8. It can even include other const symbols that have already been defined, e.g. 1 << BIT.
Sure, a boolean can be represented by an integer (-1 | 0), but does the optimization potential of using const() still apply if the value is set to TRUE | FALSE...?

Thanks! :)

samneggs
Posts: 20
Joined: Tue Jun 15, 2021 12:57 am

Re: Use const() for booleans?

Post by samneggs » Sat Sep 25, 2021 1:13 am

I don't know about Boolean optimization but I use them to selectively turn on/off sections of code. So near the top of the code after importing I have the constants DEMO_MODE = const(False) # set True for demo mode or THREADED = const(False) # set True to crash.
Sam

Post Reply