الوحدات

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 تُعيد القيمة الفعلية بدلاً من القيمة المخزَّنة مؤقتاً

السبب: السمة 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'