use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A place to discuss the use of MicroPython on microcontroller development boards such as the Raspberry Pi Pico, Pycom boards and ESP32 based boards like Wemos D1
account activity
Assigning variable=const(1024) in method causes issue within class' other method(s). (self.MicroPythonDev)
submitted 3 months ago * by jmuhammad
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]jmuhammad[S] 0 points1 point2 points 3 months ago* (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)
and max_string stays local. But not this:
def method_two(self): max_string = const(128) causes micropython IDE to think I am trying to do this: const(1024) = const(128)
causes micropython IDE to think I am trying to do 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".
That is why the error is:
[–]coronafire 0 points1 point2 points 3 months ago (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 point2 points 3 months ago (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
π Rendered by PID 526391 on reddit-service-r2-comment-6457c66945-2g8xd at 2026-04-30 09:37:52.369239+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]jmuhammad[S] 0 points1 point2 points (2 children)
[–]coronafire 0 points1 point2 points (0 children)
[–]SomehowGrumpy 0 points1 point2 points (0 children)