const() function
const() function
Hi!
Reading code from the mpr121 module, I discovered that the author used a const() function to define some constants.
What exactly does this function()? Is it to put the value in the flash, rather than in memory?
Reading code from the mpr121 module, I discovered that the author used a const() function to define some constants.
What exactly does this function()? Is it to put the value in the flash, rather than in memory?
Frédéric
Re: const() function
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/
Re: const() function
Still not sure to clearly understand what it does... I made a quick test, and it seems that it consumes memory
Frédéric
Re: const() function
Code: Select all
FOO = const(1)
Note that it's extension to CPython, and won't work with it out of the box. (But making it work is trivial:
Code: Select all
const = lambda x: x
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/
Re: const() function
Sorry, but I don't really understand what is said in the post; I'm not a C-coder
It's a constant from a C-dev point of view, am I right?
But from a python point of view, how and when should I use it? What are the pros and cons?
It's a constant from a C-dev point of view, am I right?
But from a python point of view, how and when should I use it? What are the pros and cons?
Frédéric
Re: const() function
Using const allows optimisation, but retains compatibility with Python semantics (as much as possible).
The idea is that you want to substitute the constant value directly in your code. If you write some Python code that uses a (global) variable, then looking up that global variable is relatively expensive (in time). If it's a constant value then you could just write that constant value in the code directly (eg i2c.send(0x42, 0x54)), but that would make it difficult to read (don't want magic numbers!).
Micro Python recognises the "x = const(123)" pattern in your script and during the compile stage will replace "x" in your code with "123", in that module/file. This makes it a lot faster when running.
The reason it also uses RAM is because it still puts "x" in memory as a global variable, in case someone external to the module also wants to use this constant; eg mpr121.DEBOUNCE will still work.
The recommended use for const is: use it when you have a global variable which is a constant integer that is set once and never changed.
The idea is that you want to substitute the constant value directly in your code. If you write some Python code that uses a (global) variable, then looking up that global variable is relatively expensive (in time). If it's a constant value then you could just write that constant value in the code directly (eg i2c.send(0x42, 0x54)), but that would make it difficult to read (don't want magic numbers!).
Micro Python recognises the "x = const(123)" pattern in your script and during the compile stage will replace "x" in your code with "123", in that module/file. This makes it a lot faster when running.
The reason it also uses RAM is because it still puts "x" in memory as a global variable, in case someone external to the module also wants to use this constant; eg mpr121.DEBOUNCE will still work.
The recommended use for const is: use it when you have a global variable which is a constant integer that is set once and never changed.
Re: const() function
Ok, I understand ; it is a speed-vs-size tool...
Thanks for the explanation.
Thanks for the explanation.
Frédéric
Re: const() function
Thanks for this feature and the graspable explanation. The example from Damien did give a good use-case for this. Especially with I2C addresses and bitmasks this is a nice feature. Reasonable close to a 'define'ment in C.
Re: const() function
Actually, the const optimisation can in some cases use slightly less RAM than the non-optimised case: in the non-optimised case the lookup of the const global requires some byte code to do the lookup. If it's replaced with a constant, then the load of the constant can take less RAM (in the byte code function) if the constant is a small integer.fma wrote:Ok, I understand ; it is a speed-vs-size tool...
Also, with constants the compiler can optimise things like REG | FLAG into a single constant, thus eliminating the byte code to load the 2 values and compute the or. This therefore uses less RAM.
All in all, using constants is best all round.
Re: const() function
I'll give a try with the app I'm currently developing, which uses a lot of constants (this is a port from a C app, which uses a lot of #define) :
https://github.com/fma38/micropython/tree/master/baos
https://github.com/fma38/micropython/tree/master/baos
Frédéric