Skip to content

Latest commit

 

History

History
84 lines (62 loc) · 2.97 KB

syntax.rst

File metadata and controls

84 lines (62 loc) · 2.97 KB

Syntax

Generated Sun 31 Jan 2021 14:55:11 UTC

Operators

MicroPython allows using := to assign to the variable of a comprehension, CPython raises a SyntaxError.

Cause: MicroPython is optimised for code size and doesn't check this case.

Workaround: Do not rely on this behaviour if writing CPython compatible code.

Sample code:

print([i := -1 for i in range(4)])
CPy output: uPy output:
File "<stdin>", line 7

SyntaxError: assignment expression cannot rebind comprehension iteration variable 'i'

[-1, -1, -1, -1]

Spaces

uPy requires spaces between literal numbers and keywords, CPy doesn't

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")
CPy output: uPy output:
0
1
1
Should have worked
Should have worked
Should have worked

Unicode

Unicode name escapes are not implemented

Sample code:

print("\N{LATIN SMALL LETTER A}")
CPy output: uPy output:
a
NotImplementedError: unicode name escapes