Possible enhancement to const()

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Possible enhancement to const()

Post by pythoncoder » Sat May 21, 2016 4:58 am

In device drivers it's common practice to have a substantial number of global constants specifying things like device commands and registers e.g.:

Code: Select all

DATA_REG = const(0x23)
COMMAND_REG = const(0X24)
Each line causes four bytes of RAM to be used, even if the module is implemented as frozen bytecode. This is because the language requires the variables to be available to other modules even if this is not the designer's requirement. Given the use of MicroPython on resource constrained platforms this seems wasteful.

Would it be feasible to enforce module locality on globals having names starting with an underscore and declared as constant? This would turn a Python naming convention into physical reality, but only when using the const() keyword.

Code: Select all

_DATA_REG = const(0x23)
_COMMAND_REG = const(0X24)
This arose from considering a discussion here http://forum.micropython.org/viewtopic. ... nst#p10773
Peter Hinch
Index to my micropython libraries.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Possible enhancement to const()

Post by deshipu » Sat May 21, 2016 6:55 am

Another alternative would be to not make available names that are not listed in "__all__" for the module -- if it's defined, that is.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Possible enhancement to const()

Post by pythoncoder » Sat May 21, 2016 9:59 am

Indeed. I have no knowledge of compiler design so I've no idea which would be easier to implement. One arguable advantage to my approach is that, with the change, MicroPython code remains compatible with standard Python by adding

Code: Select all

const = lambda x : x
Peter Hinch
Index to my micropython libraries.

Post Reply