Modulok¶
Generated Fri 19 Jun 2026 22:08:45 UTC
Csak pozicionális paraméterek¶
A kódméret csökkentése érdekében sok olyan függvény, amely kulcsszavas argumentumokat fogad el CPythonban, MicroPythonban csak pozicionális argumentumokat fogad el.
A MicroPython ugyanúgy jelöli a csak pozicionális paramétereket, mint a CPython: /-t szúr be a pozicionális paraméterek végének jelölésére. Minden olyan függvény, amelynek aláírása /-re végződik, kizárólag pozicionális argumentumokat fogad el. További részletekért lásd: PEP 570.
Példa¶
Például CPython 3.4-ben a socket.socket konstruktor aláírása:
socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None)
Azonban a MicroPython-ban dokumentált aláírás:
socket(af=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP, /)
A paraméterek végén lévő / azt jelzi, hogy MicroPythonban mindegyik csak pozicionális. A következő kód működik CPythonban, de a legtöbb MicroPython porton nem:
import socket
s = socket.socket(type=socket.SOCK_DGRAM)
A MicroPython kivételt vált ki:
TypeError: function doesn't take keyword arguments
A következő kód mind CPythonban, mind MicroPythonban működik:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
array¶
Különböző típuskódok összehasonlítása nem támogatott¶
Ok: Kódméret
Megkerülés: Hasonlítsa össze az egyes elemeket egyenként
Mintakód:
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:
|
A túlcsordulás-ellenőrzés nincs megvalósítva¶
Ok: A MicroPython implicit csonkítást alkalmaz a kódméret és a végrehajtási idő csökkentése érdekében
Megkerülés: Ha CPython-kompatibilitás szükséges, maszkolja az értéket explicit módon
Mintakód:
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])
|
Egész szám keresése nincs megvalósítva¶
Mintakód:
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:
|
Tömbelem törlése nincs megvalósítva¶
Mintakód:
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
|
Az indexelés 1-től eltérő lépéssel még nincs megvalósítva¶
Mintakód:
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.
Mintakód:
import errno
print(f"{errno.errorcode[errno.EOPNOTSUPP]=!s}")
CPython output: | MicroPython output: |
errno.errorcode[errno.EOPNOTSUPP]=ENOTSUP
| errno.errorcode[errno.EOPNOTSUPP]=EOPNOTSUPP
|
json¶
A JSON modul nem dob kivételt, ha az objektum nem szerializálható¶
Mintakód:
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¶
Az environ attribútum nincs megvalósítva¶
Megkerülés: Használja a getenv, putenv és unsetenv függvényeket
Mintakód:
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
|
A getenv tényleges értéket ad vissza gyorsítótárazott érték helyett¶
Ok: Az environ attribútum nincs megvalósítva
Mintakód:
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¶
A getrandbits metódus egyszerre legfeljebb 32 bitet tud visszaadni.¶
Ok: A PRNG belső állapota csak 32 bites, ezért egyszerre legfeljebb 32 bit adatot tud visszaadni.
Megkerülés: Ha 32 bitnél több bites számra van szüksége, használja a micropython-lib random modulját.
Mintakód:
import random
x = random.getrandbits(64)
print("{}".format(x))
CPython output: | MicroPython output: |
10880755926996596610
| Traceback (most recent call last):
File "<stdin>", line 11, in <module>
ValueError: bits must be 32 or less
|
A randint metódus legfeljebb a natív szóméretű egész számot tud visszaadni.¶
Ok: A PRNG egyszerre csak 32 bit állapotot tud generálni. Az eredmény ezután natív méretű int-té alakul, nem teljes int objektummá.
Megkerülés: Ha natív szóméret feletti egész számokra van szüksége, használja a micropython-lib random modulját.
Mintakód:
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¶
Mintakód:
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¶
Mintakód:
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¶
Ok: A MicroPython a kódméret optimalizálására van tervezve.
Megkerülés: Ne használjon szóközöket a formátumstringekben.
Mintakód:
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¶
A sys.stdin, sys.stdout és sys.stderr felülírása nem lehetséges¶
Ok: Csak olvasható memóriában vannak tárolva.
Mintakód:
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'
|