このドキュメンテーションは、MicroPython の最新開発ブランチのためのものです。 リリースバージョンでは利用できない機能に言及することがあります。

特定のリリースのドキュメントをお探しの場合は、左側のドロップダウンメニューを使って、 望みのバージョンを選択します。

構文

Generated Wed 07 Feb 2024 12:13:19 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
Traceback (most recent call last):
  File "<stdin>", line 21, in <module>
SyntaxError: too many args

演算子

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'
Traceback (most recent call last):
  File "<stdin>", line 7, in <listcomp>
SyntaxError: identifier redefined as global

スペース

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
Should have worked
Should have worked
Should have worked

Unicode

Unicode 名エスケープは未実装

サンプルコード:

print("\N{LATIN SMALL LETTER A}")

CPy 出力:

uPy 出力:

a
NotImplementedError: unicode name escapes