语法¶
Generated Sat 23 Nov 2024 08:40:09 UTC
参数解包在参数被解包的位置是第n个或更大的参数时(其中n是MP_SMALL_INT中的位数),将不起作用。¶
原因: 实现中使用了 MP_SMALL_INT 来标记需要解包的参数。
解决方法: 使用更少的参数。
示例代码:
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输出: |
uPy输出: |
67
|
/bin/sh: 1: ../ports/unix/build-standard/micropython: not found
|
运算符¶
MicroPython 允许使用 := 来对推导式中的变量进行赋值,CPython 会引发 SyntaxError。¶
原因: MicroPython 针对代码大小进行了优化,不检查此情况。
解决方法: 如果编写与 CPython 兼容的代码,请不要依赖此行为。
示例代码:
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: 1: ../ports/unix/build-standard/micropython: not found
|
空格¶
uPy 要求在文字数字和关键字之间加入空格,CPy 不需要¶
示例代码:
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
<string>:1: SyntaxWarning: invalid decimal literal
<string>:1: SyntaxWarning: invalid decimal literal
<string>:1: SyntaxWarning: invalid decimal literal
<string>:1: SyntaxWarning: invalid decimal literal
|
/bin/sh: 1: ../ports/unix/build-standard/micropython: not found
|
Unicode¶
未实现Unicode名称转义¶
示例代码:
print("\N{LATIN SMALL LETTER A}")
CPy输出: |
uPy输出: |
a
|
/bin/sh: 1: ../ports/unix/build-standard/micropython: not found
|