Sintassi¶
Generated Fri 19 Jun 2026 22:08:45 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.
Codice di esempio:
try:
print(eval("1__1"))
except SyntaxError:
print("Should not work")
try:
print(eval("1_"))
except SyntaxError:
print("Should not work")
CPython output: | MicroPython 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.
Codice di esempio:
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")
CPython output: | MicroPython 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
|
Operatori¶
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.
Codice di esempio:
print([[(j := i) for i in range(2)] for j in range(2)])
CPython output: | MicroPython output: |
File "<stdin>", line 8
SyntaxError: assignment expression cannot rebind comprehension iteration variable 'j'
| [[0, 1], [0, 1]]
|
Unicode¶
Le sequenze di escape per nomi Unicode non sono implementate¶
Codice di esempio:
print("\N{LATIN SMALL LETTER A}")
CPython output: | MicroPython output: |
a
| NotImplementedError: unicode name escapes
|
Unpacking¶
L’unpacking degli argomenti non funziona se l’argomento da spacchettare è l’n-esimo o superiore, dove n è il numero di bit in un MP_SMALL_INT.¶
Causa: L’implementazione usa un MP_SMALL_INT per contrassegnare gli argomenti che devono essere spacchettati.
Soluzione alternativa: Usare meno argomenti.
Codice di esempio:
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,
)
CPython output: | MicroPython output: |
67
| Traceback (most recent call last):
File "<stdin>", line 21, in <module>
SyntaxError: too many args
|