Modul¶
Generated Thu 18 Jun 2026 14:23:40 UTC
Parameter Hanya-Posisional¶
Untuk menghemat ukuran kode, banyak fungsi yang menerima argumen kata kunci di CPython hanya menerima argumen posisional di MicroPython.
MicroPython menandai parameter hanya-posisional dengan cara yang sama seperti CPython, yaitu dengan menyisipkan / untuk menandai akhir parameter posisional. Fungsi apa pun yang tanda tangannya diakhiri dengan / hanya mengambil argumen posisional. Untuk detail lebih lanjut, lihat PEP 570.
Contoh¶
Misalnya, di CPython 3.4 ini adalah tanda tangan konstruktor socket.socket
socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None)
Namun, tanda tangan yang didokumentasikan dalam MicroPython adalah:
socket(af=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP, /)
/ di akhir parameter menunjukkan bahwa semuanya hanya-posisional di MicroPython. Kode berikut berfungsi di CPython tetapi tidak di sebagian besar port MicroPython:
import socket
s = socket.socket(type=socket.SOCK_DGRAM)
MicroPython akan menimbulkan exception:
TypeError: function doesn't take keyword arguments
Kode berikut akan berfungsi di CPython maupun MicroPython:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
array¶
Perbandingan antara typecode yang berbeda tidak didukung¶
Penyebab: Ukuran kode
Solusi: Bandingkan elemen individual
Contoh kode:
import array
array.array("b", [1, 2]) == array.array("i", [1, 2])
CPython output: |
MicroPython output: |
Traceback (most recent call last):
File "<stdin>", line 10, in <module>
NotImplementedError:
|
Pemeriksaan overflow tidak diimplementasikan¶
Penyebab: MicroPython mengimplementasikan pemotongan implisit untuk mengurangi ukuran kode dan waktu eksekusi
Solusi: Jika diperlukan kompatibilitas CPython maka masking nilai secara eksplisit
Contoh kode:
import array
a = array.array("b", [257])
print(a)
CPython output: |
MicroPython output: |
Traceback (most recent call last):
File "<stdin>", line 10, in <module>
OverflowError: signed char is greater than maximum
|
array('b', [1])
|
Pencarian bilangan bulat tidak diimplementasikan¶
Contoh kode:
import array
print(1 in array.array("B", b"12"))
CPython output: |
MicroPython output: |
False
|
Traceback (most recent call last):
File "<stdin>", line 10, in <module>
NotImplementedError:
|
Penghapusan array tidak diimplementasikan¶
Contoh kode:
import array
a = array.array("b", (1, 2, 3))
del a[1]
print(a)
CPython output: |
MicroPython output: |
array('b', [1, 3])
|
Traceback (most recent call last):
File "<stdin>", line 11, in <module>
TypeError: 'array' object doesn't support item deletion
|
Subscript dengan step != 1 belum diimplementasikan¶
Contoh kode:
import array
a = array.array("b", (1, 2, 3))
print(a[3:2:2])
CPython output: |
MicroPython output: |
array('b')
|
Traceback (most recent call last):
File "<stdin>", line 11, in <module>
NotImplementedError: only slices with step=1 (aka None) are supported
|
errno¶
MicroPython does not include ENOTSUP as a name for errno 95.¶
Cause: MicroPython does not implement the ENOTSUP canonical alias for EOPNOTSUPP added in CPython 3.2.
Workaround: Use errno.EOPNOTSUPP in place of errno.ENOTSUP.
Contoh kode:
import errno
print(f"{errno.errorcode[errno.EOPNOTSUPP]=!s}")
CPython output: |
MicroPython output: |
errno.errorcode[errno.EOPNOTSUPP]=ENOTSUP
|
errno.errorcode[errno.EOPNOTSUPP]=EOPNOTSUPP
|
json¶
Modul JSON tidak melempar exception ketika objek tidak dapat diserialisasi¶
Contoh kode:
import json
try:
print(json.dumps(b"shouldn't be able to serialise bytes"))
except TypeError:
print("TypeError")
CPython output: |
MicroPython output: |
TypeError
|
"shouldn't be able to serialise bytes"
|
os¶
Atribut environ tidak diimplementasikan¶
Solusi: Gunakan getenv, putenv dan unsetenv
Contoh kode:
import os
try:
print(os.environ.get("NEW_VARIABLE"))
os.environ["NEW_VARIABLE"] = "VALUE"
print(os.environ["NEW_VARIABLE"])
except AttributeError:
print("should not get here")
print(os.getenv("NEW_VARIABLE"))
os.putenv("NEW_VARIABLE", "VALUE")
print(os.getenv("NEW_VARIABLE"))
CPython output: |
MicroPython output: |
None
VALUE
|
should not get here
None
VALUE
|
getenv mengembalikan nilai aktual alih-alih nilai yang di-cache¶
Penyebab: Atribut environ tidak diimplementasikan
Contoh kode:
import os
print(os.getenv("NEW_VARIABLE"))
os.putenv("NEW_VARIABLE", "VALUE")
print(os.getenv("NEW_VARIABLE"))
CPython output: |
MicroPython output: |
None
None
|
None
VALUE
|
random¶
Metode getrandbits hanya dapat mengembalikan maksimum 32 bit sekaligus.¶
Penyebab: Status internal PRNG hanya 32 bit sehingga hanya dapat mengembalikan maksimum 32 bit data sekaligus.
Solusi: Jika Anda membutuhkan angka yang lebih dari 32 bit maka gunakan modul random dari micropython-lib.
Contoh kode:
import random
x = random.getrandbits(64)
print("{}".format(x))
CPython output: |
MicroPython output: |
12327776399964966313
|
Traceback (most recent call last):
File "<stdin>", line 11, in <module>
ValueError: bits must be 32 or less
|
Metode randint hanya dapat mengembalikan bilangan bulat yang paling besar sebesar ukuran kata asli.¶
Penyebab: PRNG hanya mampu menghasilkan 32 bit status sekaligus. Hasilnya kemudian dicast ke int berukuran asli alih-alih objek int penuh.
Solusi: Jika Anda membutuhkan bilangan bulat yang lebih besar dari ukuran kata asli, gunakan modul random dari micropython-lib.
Contoh kode:
import random
x = random.randint(2**128 - 1, 2**128)
print("x={}".format(x))
CPython output: |
MicroPython output: |
x=340282366920938463463374607431768211456
|
Traceback (most recent call last):
File "<stdin>", line 11, in <module>
OverflowError: overflow converting long int to machine word
|
struct¶
Struct pack with too few args, not checked by MicroPython¶
Contoh kode:
import struct
try:
print(struct.pack("bb", 1))
print("Should not get here")
except:
print("struct.error")
CPython output: |
MicroPython output: |
struct.error
|
b'\x01\x00'
Should not get here
|
Struct pack with too many args, not checked by MicroPython¶
Contoh kode:
import struct
try:
print(struct.pack("bb", 1, 2, 3))
print("Should not get here")
except:
print("struct.error")
CPython output: |
MicroPython output: |
struct.error
|
b'\x01\x02'
Should not get here
|
Struct pack with whitespace in format, whitespace ignored by CPython, error on MicroPython¶
Penyebab: MicroPython dioptimalkan untuk ukuran kode.
Solusi: Jangan gunakan spasi dalam string format.
Contoh kode:
import struct
try:
print(struct.pack("b b", 1, 2))
print("Should have worked")
except:
print("struct.error")
CPython output: |
MicroPython output: |
b'\x01\x02'
Should have worked
|
struct.error
|
sys¶
Mengganti sys.stdin, sys.stdout dan sys.stderr tidak memungkinkan¶
Penyebab: Keduanya disimpan di memori hanya-baca.
Contoh kode:
import sys
sys.stdin = None
print(sys.stdin)
CPython output: |
MicroPython output: |
None
|
Traceback (most recent call last):
File "<stdin>", line 10, in <module>
AttributeError: 'module' object has no attribute 'stdin'
|