语法¶
Generated Wed 03 Jan 2024 12:07:50 UTC
操作符¶
MicroPython 允许使用 := 给变量赋值, CPython 会报语法错误.¶
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.
参考代码
print([i := -1 for i in range(4)])
CPy 输出  | 
uPy 输出  | 
  File "<stdin>", line 7
SyntaxError: assignment expression cannot rebind comprehension iteration variable 'i'
 | 
/bin/sh: ../ports/unix/micropython: No such file or directory
 | 
作用域¶
uPy requires spaces between literal numbers and keywords, CPy doesn’t¶
参考代码
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 输出  | 
uPy 输出  | 
0
1
1
 | 
/bin/sh: ../ports/unix/micropython: No such file or directory
 | 
Unicode¶
Unicode name escapes are not implemented¶
参考代码
print("\N{LATIN SMALL LETTER A}")
CPy 输出  | 
uPy 输出  | 
a
 | 
/bin/sh: ../ports/unix/micropython: No such file or directory
 |