const no longer a keyword

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

const no longer a keyword

Post by pythoncoder » Sun Nov 06, 2016 5:08 pm

This will break code which may be run against different firmware versions. The const declaration was a MicroPython keyword; it is now a member of the micropython library. For code compatible with current and earlier firmware versions issue:

Code: Select all

try:
	from micropython import const
except ImportError:
	pass

_ANSWER = const(42)
Peter Hinch
Index to my micropython libraries.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: const no longer a keyword

Post by Roberthh » Sun Nov 06, 2016 8:18 pm

It does not look like that. In today's build of EPS8266 I can still enter/use something like

Code: Select all

a = const(3)
without having imported const before. And I definitely have code on the esp8266, that uses const and does not import it.

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: const no longer a keyword

Post by Damien » Mon Nov 07, 2016 2:03 am

"const" was never a keyword in the strict sense of the lexer (eg "for" is a keyword). The recent changes to "const" usage are the following:
  1. micropython.const(x) function was added, which just returns its argument.
  2. all Python scripts in the micropython repository that use the const feature had "from micropython import const" added to them.
The reason is to make this const feature compatible with CPython, if you have a dummy micropython module with a dummy const function.

Old scripts will continue to work just fine, the "from micropython import const" is not strictly necessary, but highly recommended for new scripts.

Old firmware won't run the new scripts because the new scripts need the micropython.const() function. So please update to the new firmware :)

Post Reply