מודולים

Generated Thu 18 Jun 2026 05:00:56 UTC

פרמטרים מיקומיים בלבד

לחסכון בגודל הקוד, פונקציות רבות המקבלות ארגומנטים לפי שם ב-CPython מקבלות רק ארגומנטים מיקומיים ב-MicroPython.

MicroPython מסמן פרמטרים מיקומיים-בלבד באותה שיטה כמו CPython, על ידי הוספת / לסמן את סוף הפרמטרים המיקומיים. כל פונקציה שחתימתה מסתיימת ב-/ מקבלת רק ארגומנטים מיקומיים. לפרטים נוספים, ראה PEP 570.

דוגמה

לדוגמה, ב-CPython 3.4 זו חתימת הבנאי socket.socket:

socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None)

אולם, החתימה המתועדת ב-MicroPython היא:

socket(af=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP, /)

ה-/ בסוף הפרמטרים מציין שכולם מיקומיים-בלבד ב-MicroPython. הקוד הבא עובד ב-CPython אך לא ברוב הפורטים של MicroPython:

import socket
s = socket.socket(type=socket.SOCK_DGRAM)

MicroPython יעלה חריגה:

TypeError: function doesn't take keyword arguments

הקוד הבא יעבוד גם ב-CPython וגם ב-MicroPython:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

array

השוואה בין קודי סוג שונים אינה נתמכת

סיבה: גודל קוד

עקיפה: השווה אלמנטים בודדים

קוד לדוגמה:

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:

בדיקת גלישה אינה מיושמת

סיבה: MicroPython מיישם קיצוץ משתמע כדי לצמצם את גודל הקוד ואת זמן הריצה

עקיפה: אם נדרשת תאימות ל-CPython, הסתר את הערך במפורש

קוד לדוגמה:

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])

חיפוש מספר שלם אינו מיושם

קוד לדוגמה:

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:

מחיקת מערך אינה מיושמת

קוד לדוגמה:

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

אינדוקס עם צעד שונה מ-1 טרם מיושם

קוד לדוגמה:

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.

קוד לדוגמה:

import errno

print(f"{errno.errorcode[errno.EOPNOTSUPP]=!s}")

CPython output:

MicroPython output:

errno.errorcode[errno.EOPNOTSUPP]=ENOTSUP
errno.errorcode[errno.EOPNOTSUPP]=EOPNOTSUPP

json

מודול JSON אינו זורק חריגה כאשר אובייקט אינו ניתן לסריאליזציה

קוד לדוגמה:

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

המאפיין environ אינו מיושם

עקיפה: השתמש ב-getenv, putenv ו-unsetenv

קוד לדוגמה:

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 מחזיר את הערך בפועל במקום הערך ה-cached

סיבה: המאפיין environ אינו מיושם

קוד לדוגמה:

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

השיטה getrandbits יכולה להחזיר לכל היותר 32 ביט בכל פעם.

סיבה: המצב הפנימי של ה-PRNG הוא 32 ביט בלבד, ולכן הוא יכול להחזיר לכל היותר 32 ביט של נתונים בכל פעם.

עקיפה: אם אתה זקוק למספר בעל יותר מ-32 ביט, השתמש במודול random מ-micropython-lib.

קוד לדוגמה:

import random


x = random.getrandbits(64)
print("{}".format(x))

CPython output:

MicroPython output:

3426663832757540188
Traceback (most recent call last):
  File "<stdin>", line 11, in <module>
ValueError: bits must be 32 or less

השיטה randint יכולה להחזיר רק מספר שלם שגודלו לכל היותר גודל המילה המקורי.

סיבה: ה-PRNG מסוגל לייצר 32 ביט של מצב בלבד בכל פעם. התוצאה מומרת לאחר מכן ל-int בגודל מקורי במקום לאובייקט int מלא.

עקיפה: אם אתה זקוק למספרים שלמים גדולים מגודל המילה המקורי, השתמש במודול random מ-micropython-lib.

קוד לדוגמה:

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

קוד לדוגמה:

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

קוד לדוגמה:

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

סיבה: MicroPython מותאם לגודל הקוד.

עקיפה: אל תשתמש ברווחים במחרוזות פורמט.

קוד לדוגמה:

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

לא ניתן לעקוף את sys.stdin, sys.stdout ו-sys.stderr

סיבה: הם מאוחסנים בזיכרון לקריאה בלבד.

קוד לדוגמה:

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'