Page 1 of 1

const no longer a keyword

Posted: Sun Nov 06, 2016 5:08 pm
by pythoncoder
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)

Re: const no longer a keyword

Posted: Sun Nov 06, 2016 8:18 pm
by Roberthh
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.

Re: const no longer a keyword

Posted: Mon Nov 07, 2016 2:03 am
by Damien
"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 :)