組込み型¶
Generated Sat 22 Dec 2018 08:00:49 UTC
例外¶
例外チェーンは実装されていない¶
サンプルコード:
try:
raise TypeError
except TypeError:
raise ValueError
CPy 出力: | uPy 出力: |
Traceback (most recent call last):
File "<stdin>", line 8, in <module>
TypeError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 10, in <module>
ValueError
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
組込み例外のユーザ定義属性はサポートされない¶
原因: MicroPython はメモリ使用量について高度に最適化しています。
回避策: ユーザ定義の例外サブクラスを使ってください。
サンプルコード:
e = Exception()
e.x = 0
print(e.x)
CPy 出力: | uPy 出力: |
0
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
while ループ条件での例外は予期しない行番号になる¶
原因: 条件チェックはループ本体の最後で実行されるように最適化されているので、その行番号が報告されます。
サンプルコード:
l = ["-foo", "-bar"]
i = 0
while l[i][0] == "-":
print("iter")
i += 1
CPy 出力: | uPy 出力: |
iter
iter
Traceback (most recent call last):
File "<stdin>", line 10, in <module>
IndexError: list index out of range
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
Exception.__init__ メソッドは存在しない¶
原因: ネイティブクラスのサブクラス化は、Micropython で完全にはサポートされていません。
回避策: 代わりに、次のように super()
を使って呼出してください。
class A(Exception):
def __init__(self):
super().__init__()
サンプルコード:
class A(Exception):
def __init__(self):
Exception.__init__(self)
a = A()
CPy 出力: | uPy 出力: |
/bin/sh: ../ports/unix/micropython: No such file or directory
|
bytearray¶
配列スライスへの代入はサポートされない¶
サンプルコード:
b = bytearray(4)
b[0:1] = [1, 2]
print(b)
CPy 出力: | uPy 出力: |
bytearray(b'\x01\x02\x00\x00\x00')
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
bytes¶
bytes オブジェクトが .format() メソッドをサポート¶
原因: MicroPython はより一般的な実装であるため、 str と bytes の両方が __mod__()
(% 演算子) をサポートすると、 format()
も両方でサポートすることになります。 __mod__()
のサポートが続く限り、bytes のフォーマットを行う format()
も残ります。
回避策: CPython との互換性にこだわるなら bytes オブジェクトに対して .format()
を使わないでください。
サンプルコード:
print(b'{}'.format(1))
CPy 出力: | uPy 出力: |
Traceback (most recent call last):
File "<stdin>", line 7, in <module>
AttributeError: 'bytes' object has no attribute 'format'
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
bytes() のキーワード引数は未実装¶
回避策: エンコーディングは位置パラメータで渡してください。たとえば print(bytes('abc', 'utf-8'))
のようにします。
サンプルコード:
print(bytes('abc', encoding='utf8'))
CPy 出力: | uPy 出力: |
b'abc'
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
スライスのステップが1以外は未実装¶
原因: MicroPython はメモリ使用量について高度に最適化しています。
回避策: このまれな操作には明示的なループを使ってください。
サンプルコード:
print(b'123'[0:3:2])
CPy 出力: | uPy 出力: |
b'13'
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
float¶
uPy と CPython の出力フォーマットが異なることがある¶
サンプルコード:
print('%.1g' % -9.9)
CPy 出力: | uPy 出力: |
-1e+01
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
int¶
int の派生型に対する int 変換は使えない¶
回避策: 本当に必要でない限り、組み込み型のサブクラス化は避けてください。 https://en.wikipedia.org/wiki/Composition_over_inheritance を推奨します。
サンプルコード:
class A(int):
__add__ = lambda self, other: A(int(self) + other)
a = A(42)
print(a+a)
CPy 出力: | uPy 出力: |
84
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
リスト¶
ステップが 1 以外のリスト要素の削除は未実装¶
回避策: このまれな操作には明示的なループを使ってください。
サンプルコード:
l = [1, 2, 3, 4]
del l[0:4:2]
print(l)
CPy 出力: | uPy 出力: |
[2, 4]
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
リストのスライスへのイテレーティブの代入は未実装¶
原因: 右辺はタプルかリストに制限されています。
回避策: イテレータブルをリストに変換するよう、右辺で list(<iter>)
を使ってください。
サンプルコード:
l = [10, 20]
l[0:1] = range(4)
print(l)
CPy 出力: | uPy 出力: |
[0, 1, 2, 3, 20]
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
ステップ 1 以外のリストスライスへの代入は未実装¶
回避策: このまれな操作には明示的なループを使ってください。
サンプルコード:
l = [1, 2, 3, 4]
l[0:4:2] = [5, 6]
print(l)
CPy 出力: | uPy 出力: |
[5, 2, 6, 4]
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
str¶
str.endswith(s, start) のような開始/終了インデックスは未実装¶
サンプルコード:
print('abc'.endswith('c', 1))
CPy 出力: | uPy 出力: |
True
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
属性置換は未実装¶
サンプルコード:
print('{a[0]}'.format(a=[1, 2]))
CPy 出力: | uPy 出力: |
1
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
str(...) のキーワード引数は未実装¶
回避策: エンコーディングは位置パラメータで指定してください。たとえば print(str(b'abc', 'utf-8'))
のようにします。
サンプルコード:
print(str(b'abc', encoding='utf8'))
CPy 出力: | uPy 出力: |
abc
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
str.ljust() and str.rjust() は未実装¶
原因: MicroPython はメモリ使用量について高度に最適化しています。これは簡単に回避できます。
回避策: s.ljust(10)
の代わりに "%-10s" % s
、s.rjust(10)
の代わりに "% 10s" % s
を使ってください。あるいは "{:<10}".format(s)
や "{:>10}".format(s)
を使ってください。
サンプルコード:
print('abc'.ljust(10))
CPy 出力: | uPy 出力: |
abc
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
str.rsplit(None, n) のように rsplit の第1引数を None にするのは未実装¶
サンプルコード:
print('a a a'.rsplit(None, 1))
CPy 出力: | uPy 出力: |
['a a', 'a']
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
str のサブクラスのインスタンスと str のインスタンスとの等価性は比較できない¶
サンプルコード:
class S(str):
pass
s = S('hello')
print(s == 'hello')
CPy 出力: | uPy 出力: |
True
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
ステップが 1 でないスライスは未実装¶
サンプルコード:
print('abcdefghi'[0:9:2])
CPy 出力: | uPy 出力: |
acegi
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|