Syntax¶
Generated Thu 22 May 2025 13:57:03 UTC
Literals¶
MicroPython accepts underscores in numeric literals where CPython doesn’t¶
Cause: Different parser implementation
MicroPython’s tokenizer ignores underscores in numeric literals, while CPython rejects multiple consecutive underscores and underscores after the last digit.
Workaround: Remove the underscores not accepted by CPython.
Sample code:
try:
print(eval("1__1"))
except SyntaxError:
print("Should not work")
try:
print(eval("1_"))
except SyntaxError:
print("Should not work")
CPy output: |
uPy output: |
Should not work
Should not work
|
11
1
|
MicroPython requires spaces between literal numbers and keywords or “.”, CPython doesn’t¶
Cause: Different parser implementation
MicroPython’s tokenizer treats a sequence like 1and
as a single token, while CPython treats it as two tokens.
Since CPython 3.11, when the literal number is followed by a token, this syntax causes a SyntaxWarning
for an “invalid literal”. When a literal number is followed by a “.” denoting attribute access, CPython does not warn.
Workaround: Add a space between the integer literal and the intended next token.
This also fixes the SyntaxWarning
in CPython.
Sample code:
try:
print(eval("1and 0"))
except SyntaxError:
print("Should have worked")
try:
print(eval("1or 0"))
except SyntaxError:
print("Should have worked")
try:
print(eval("1if 1else 0"))
except SyntaxError:
print("Should have worked")
try:
print(eval("0x1.to_bytes(1)"))
except SyntaxError:
print("Should have worked")
CPy output: |
uPy output: |
<string>:1: SyntaxWarning: invalid decimal literal
0
<string>:1: SyntaxWarning: invalid decimal literal
1
<string>:1: SyntaxWarning: invalid decimal literal
<string>:1: SyntaxWarning: invalid decimal literal
1
b'\x01'
|
Should have worked
Should have worked
Should have worked
Should have worked
|
Operators¶
MicroPython allows := to assign to the iteration variable in nested comprehensions, CPython does not.¶
Cause: MicroPython is optimised for code size. Although it is a syntax error to assign to the iteration variable in a standard comprehension (same as CPython), it doesn’t check if an inner nested comprehension assigns to the iteration variable of the outer comprehension.
Workaround: Do not use := to assign to the iteration variable of a comprehension.
Sample code:
print([[(j := i) for i in range(2)] for j in range(2)])
CPy output: |
uPy output: |
File "<stdin>", line 8
SyntaxError: assignment expression cannot rebind comprehension iteration variable 'j'
|
[[0, 1], [0, 1]]
|
Unicode¶
Unicode name escapes are not implemented¶
Sample code:
print("\N{LATIN SMALL LETTER A}")
CPy output: |
uPy output: |
a
|
NotImplementedError: unicode name escapes
|
Unpacking¶
Argument unpacking does not work if the argument being unpacked is the nth or greater argument where n is the number of bits in an MP_SMALL_INT.¶
Cause: The implementation uses an MP_SMALL_INT to flag args that need to be unpacked.
Workaround: Use fewer arguments.
Sample code:
def example(*args):
print(len(args))
MORE = ["a", "b", "c"]
example(
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
*MORE,
)
CPy output: |
uPy output: |
67
|
Traceback (most recent call last):
File "<stdin>", line 21, in <module>
SyntaxError: too many args
|