Most Micropython ports contain a 'micropython
' module, which has a specific function called 'const()
'. I am led to believe it behaves a lot like '#define
' in C, however, it only accepts integers. Does anyone know why this is? You can declare float constants in C, so why is this unavailable in micropython?
I want to efficiently store values which are used to convert between units, but many of them are float values... (I thought about multiplying them by 10^x and then just dividing by that at runtime, but this will likely take just as long as storing the float in a variable and letting the code use the lookup table).
Any ideas why we can only declare integers with micropython.const()
?
Cheers :)
#define
is a pre-processor command and therefore works completely different. – Discommendconst
is recognized by the MicroPython compiler, though, so it behaves a lot more like#define
than anything you could do in CPython. You can't do any complex macro stuff, butconst
values will be directly substituted into their usage sites (within a module, at least), and non-exportedconst
s don't require a storage location. – Zouaveconst
; 'large' integers (i.e. integers which don't fit in a machine word minus some bits) and floating point values need to be allocated on the heap.const
replacement etc is done when parsing I think, before compiling. As to your actual question, i.e. why that wouldn't work for heap-allocated objects: I guess technically it could be possible but it might be too hard, not sure.. – Andraandrade