Modules#
Generated Mon 03 Aug 2026 05:40:41 UTC
Uitsluitend positionele parameters#
Om coderuimte te besparen accepteren veel functies die in CPython sleutelwoordargumenten accepteren, in MicroPython alleen positionele argumenten.
MicroPython markeert uitsluitend positionele parameters op dezelfde manier als CPython, door een / in te voegen om het einde van de positionele parameters aan te geven. Elke functie waarvan de handtekening eindigt op / accepteert uitsluitend positionele argumenten. Zie voor meer informatie PEP 570.
Voorbeeld#
In CPython 3.4 is dit bijvoorbeeld de handtekening van de constructor socket.socket:
socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None)
De handtekening die is gedocumenteerd in MicroPython is echter:
socket(af=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP, /)
De / aan het einde van de parameters geeft aan dat ze in MicroPython allemaal uitsluitend positioneel zijn. De volgende code werkt in CPython maar niet in de meeste MicroPython-ports:
import socket
s = socket.socket(type=socket.SOCK_DGRAM)
MicroPython zal een uitzondering genereren:
TypeError: function doesn't take keyword arguments
De volgende code werkt zowel in CPython als in MicroPython:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
array#
Vergelijking tussen verschillende typecodes wordt niet ondersteund#
Oorzaak: Codeomvang
Oplossing: Vergelijk afzonderlijke elementen
Voorbeeldcode:
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:
|
Overloopcontrole is niet geïmplementeerd#
Oorzaak: MicroPython implementeert impliciete afkapping om de codeomvang en uitvoeringstijd te verkleinen
Oplossing: Als CPython-compatibiliteit vereist is, maskeer de waarde dan expliciet
Voorbeeldcode:
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])
|
Zoeken naar gehele getallen is niet geïmplementeerd#
Voorbeeldcode:
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:
|
Array-verwijdering is niet geïmplementeerd#
Voorbeeldcode:
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 met stap != 1 is nog niet geïmplementeerd#
Voorbeeldcode:
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.
Voorbeeldcode:
import errno
print(f"{errno.errorcode[errno.EOPNOTSUPP]=!s}")
CPython output: | MicroPython output: |
errno.errorcode[errno.EOPNOTSUPP]=ENOTSUP
| errno.errorcode[errno.EOPNOTSUPP]=EOPNOTSUPP
|
json#
De JSON-module gooit geen uitzondering wanneer een object niet serialiseerbaar is#
Voorbeeldcode:
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#
Het attribuut environ is niet geïmplementeerd#
Oplossing: Gebruik getenv, putenv en unsetenv
Voorbeeldcode:
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 retourneert de werkelijke waarde in plaats van de gecachte waarde#
Oorzaak: Het attribuut environ is niet geïmplementeerd
Voorbeeldcode:
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#
De methode getrandbits kan maximaal 32 bits tegelijk teruggeven.#
Oorzaak: De interne toestand van de PRNG is slechts 32 bits, waardoor maximaal 32 bits aan gegevens tegelijk kunnen worden teruggegeven.
Oplossing: Als u een getal nodig heeft met meer dan 32 bits, gebruik dan de random-module uit micropython-lib.
Voorbeeldcode:
import random
x = random.getrandbits(64)
print("{}".format(x))
CPython output: | MicroPython output: |
11947790255791459587
| Traceback (most recent call last):
File "<stdin>", line 11, in <module>
ValueError: bits must be 32 or less
|
De methode randint kan alleen een geheel getal teruggeven van maximaal de native woordgrootte.#
Oorzaak: De PRNG kan slechts 32 bits toestand tegelijk genereren. Het resultaat wordt vervolgens omgezet naar een native int in plaats van een volledig int-object.
Oplossing: Als u gehele getallen groter dan de native woordgrootte nodig heeft, gebruik dan de random-module uit micropython-lib.
Voorbeeldcode:
import random
x = random.randint(2**128 - 1, 2**128)
print("x={}".format(x))
CPython output: | MicroPython output: |
x=340282366920938463463374607431768211455
| 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#
Voorbeeldcode:
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#
Voorbeeldcode:
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#
Oorzaak: MicroPython is geoptimaliseerd voor codeomvang.
Oplossing: Gebruik geen spaties in formaatreeksen.
Voorbeeldcode:
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#
Het overschrijven van sys.stdin, sys.stdout en sys.stderr is niet mogelijk#
Oorzaak: Ze zijn opgeslagen in alleen-lezengeheugen.
Voorbeeldcode:
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'
|