Moduli¶
Generated Fri 19 Jun 2026 22:08:45 UTC
Parametri solo posizionali¶
Per ridurre la dimensione del codice, molte funzioni che accettano argomenti keyword in CPython accettano solo argomenti posizionali in MicroPython.
MicroPython contrassegna i parametri solo posizionali nello stesso modo di CPython, inserendo / per indicare la fine dei parametri posizionali. Qualsiasi funzione la cui firma termina con / accetta solo argomenti posizionali. Per ulteriori dettagli, vedere PEP 570.
Esempio¶
Ad esempio, in CPython 3.4 questa è la firma del costruttore socket.socket:
socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None)
Tuttavia, la firma documentata in MicroPython è:
socket(af=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP, /)
Il / alla fine dei parametri indica che sono tutti solo posizionali in MicroPython. Il seguente codice funziona in CPython ma non nella maggior parte dei port MicroPython:
import socket
s = socket.socket(type=socket.SOCK_DGRAM)
MicroPython genererà un’eccezione:
TypeError: function doesn't take keyword arguments
Il seguente codice funzionerà sia in CPython che in MicroPython:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
array¶
Il confronto tra typecode diversi non è supportato¶
Causa: Dimensione del codice
Soluzione alternativa: Confrontare i singoli elementi
Codice di esempio:
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:
|
Il controllo dell’overflow non è implementato¶
Causa: MicroPython implementa il troncamento implicito per ridurre la dimensione del codice e il tempo di esecuzione
Soluzione alternativa: Se è necessaria la compatibilità con CPython, mascherare esplicitamente il valore
Codice di esempio:
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])
|
La ricerca di interi non è implementata¶
Codice di esempio:
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:
|
La cancellazione di array non è implementata¶
Codice di esempio:
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
|
L’indicizzazione con passo != 1 non è ancora implementata¶
Codice di esempio:
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.
Codice di esempio:
import errno
print(f"{errno.errorcode[errno.EOPNOTSUPP]=!s}")
CPython output: | MicroPython output: |
errno.errorcode[errno.EOPNOTSUPP]=ENOTSUP
| errno.errorcode[errno.EOPNOTSUPP]=EOPNOTSUPP
|
json¶
Il modulo JSON non genera un’eccezione quando l’oggetto non è serializzabile¶
Codice di esempio:
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¶
L’attributo environ non è implementato¶
Soluzione alternativa: Usare getenv, putenv e unsetenv
Codice di esempio:
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 restituisce il valore effettivo invece del valore memorizzato nella cache¶
Causa: L’attributo environ non è implementato
Codice di esempio:
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¶
Il metodo getrandbits può restituire al massimo 32 bit alla volta.¶
Causa: Lo stato interno del PRNG è di soli 32 bit, quindi può restituire al massimo 32 bit di dati alla volta.
Soluzione alternativa: Se si ha bisogno di un numero con più di 32 bit, utilizzare il modulo random di micropython-lib.
Codice di esempio:
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
|
Il metodo randint può restituire solo un intero al massimo delle dimensioni della parola nativa.¶
Causa: Il PRNG è in grado di generare solo 32 bit di stato alla volta. Il risultato viene quindi convertito in un int di dimensione nativa invece di un oggetto int completo.
Soluzione alternativa: Se si hanno bisogno di interi più grandi della dimensione della parola nativa, usare il modulo random di micropython-lib.
Codice di esempio:
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¶
Codice di esempio:
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¶
Codice di esempio:
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¶
Causa: MicroPython è ottimizzato per la dimensione del codice.
Soluzione alternativa: Non usare spazi nelle stringhe di formato.
Codice di esempio:
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¶
Non è possibile sovrascrivere sys.stdin, sys.stdout e sys.stderr¶
Causa: Sono memorizzati in memoria di sola lettura.
Codice di esempio:
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'
|