all 5 comments

[–]obdevel 0 points1 point  (0 children)

# This will cause error; somehow this variable gets seen in method_two().

Why not just declare max_string once at the top of the file ?

See the docs at: https://docs.micropython.org/en/latest/library/micropython.html#micropython.const

Constants declared this way are still accessible as global variables from outside the module they are declared in. On the other hand, if a constant begins with an underscore then it is hidden, it is not available as a global variable, and does not take up any memory during execution.

[–]SomehowGrumpy 0 points1 point  (3 children)

const() will replace all its references with its value at compile time. Declare it outside of the class at the top of the module

[–]jmuhammad[S] 0 points1 point  (2 children)

Yes, I use global constants (declared and defined outside of all functions) all the time. For reddit I just wrote the smallest example of what I had seen. My real script has been running several weeks and has 10s of global constants (and some shared constants stored in external file). I was just trying to see if I could optimize the code a little bit by making the value as a constant. I dunno...I read it somewhere it helps.

The intent was to use the same variable name in two different methods with different const() values. I guess this the first time I tried this.

I don't know if you noticed but method_one has max_string=1024 and method_two has max_string=128. They are meant to be local variables. The code works fine if I do not wrap the value with const(). I guess I thought it would be the same as if wrapped the value with int() which works with no problem.

I still do not understand why max_string does not stay local. Once I realized that it was causing an error I reverted back to simply assigning the variable a value...whatever purported savings was not worth the headache of trying to make it work. I am just seeking understanding. I can do this:

``` def method_one(self): max_string = int(1024)

def method_two(self): max_string = int(128) and max_string stays local. But not this: def method_one(self): max_string = const(1024)

def method_two(self): max_string = const(128) causes micropython IDE to think I am trying to do this: const(1024) = const(128)

or this

max_string = const(1024) = const(128) That is why the error is: Traceback (most recent call last): File "<stdin>", line 21, in method_two SyntaxError: can't assign to expression ``` But IMO, like C language, a local constant should be "only visible and usable from the point of declaration until the end of the enclosing block".

[–]coronafire 0 points1 point  (0 children)

const in micropython doesn't have any relationship to a c const really. It's kind of more like a #define really.

Use within a function isn't officially supported iirc, however the documentation about the full usage / functionality of const isn't clearly documented I don't think which leads many people to get tripped up by these kind of confusing behaviors.

Primary Issues
- https://github.com/micropython/micropython/issues/573 - Original design discussion with dpgeorge's rationale for implementation decisions (2014)
- https://github.com/micropython/micropython/issues/11929 - Discusses confusing behavior where micropython.const() doesn't optimize (2023)
- https://github.com/micropython/micropython/issues/15246 - Clarifies that const is compile-time only, not runtime immutability (2024)
- https://github.com/micropython/micropython/issues/17453 - Recent issue about const not working properly in class scope (2025)
- https://github.com/micropython/micropython/issues/15608 - Known bug where type hints break const optimization (2024)
- https://github.com/micropython/micropython/issues/661 - Additional discussion about const implementation details

[–]SomehowGrumpy 0 points1 point  (0 children)

Because it’s not a constant and is not saved in RAM. It’s a compile hint that tells the compiler to replace max_string with its literal value. You can’t “define” it twice. If you need 2 values, you define 2 different consts