โมดูล

Generated Thu 18 Jun 2026 14:23:40 UTC

พารามิเตอร์เฉพาะตำแหน่ง (Positional-only Parameters)

เพื่อประหยัดพื้นที่โค้ด ฟังก์ชันหลายอย่างที่รับ keyword arguments ใน CPython รับเฉพาะ positional arguments ใน MicroPython เท่านั้น

MicroPython ทำเครื่องหมายพารามิเตอร์เฉพาะตำแหน่งในลักษณะเดียวกับ CPython โดยการแทรก / เพื่อทำเครื่องหมายสิ้นสุดของพารามิเตอร์ตำแหน่ง ฟังก์ชันใดที่มี signature ลงท้ายด้วย / รับ เฉพาะ positional arguments เท่านั้น สำหรับรายละเอียดเพิ่มเติม ดู PEP 570

ตัวอย่าง

ตัวอย่างเช่น ใน CPython 3.4 นี่คือ signature ของ constructor socket.socket

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

อย่างไรก็ตาม signature ที่บันทึกไว้ใน MicroPython คือ:

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

/ ที่ท้ายของพารามิเตอร์บ่งชี้ว่าทั้งหมดเป็นแบบเฉพาะตำแหน่งใน MicroPython โค้ดต่อไปนี้ทำงานได้ใน CPython แต่ไม่ได้ใน MicroPython ports ส่วนใหญ่:

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

MicroPython จะ raise ข้อยกเว้น:

TypeError: function doesn't take keyword arguments

โค้ดต่อไปนี้จะทำงานได้ทั้งใน CPython และ MicroPython:

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

array

ไม่รองรับการเปรียบเทียบระหว่าง typecodes ที่แตกต่างกัน

สาเหตุ: ขนาดโค้ด

วิธีแก้ไข: เปรียบเทียบองค์ประกอบแต่ละตัว

โค้ดตัวอย่าง:

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:

ยังไม่รองรับการตรวจสอบ Overflow

สาเหตุ: MicroPython รองรับการตัดค่าโดยนัยเพื่อลดขนาดโค้ดและเวลาทำงาน

วิธีแก้ไข: หากต้องการความเข้ากันได้กับ CPython ให้ mask ค่าอย่างชัดเจน

โค้ดตัวอย่าง:

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:

ยังไม่รองรับการลบ Array

โค้ดตัวอย่าง:

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 ที่มี step != 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 ไม่ throw exception เมื่อออบเจ็กต์ไม่สามารถ serialise ได้

โค้ดตัวอย่าง:

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 คืนค่าจริงแทนที่จะเป็นค่าที่ cache ไว้

สาเหตุ: ยังไม่รองรับแอตทริบิวต์ 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 บิตต่อครั้งเท่านั้น

สาเหตุ: state ภายในของ PRNG มีเพียง 32 บิต จึงสามารถคืนค่าข้อมูลได้สูงสุด 32 บิตต่อครั้ง

วิธีแก้ไข: หากต้องการตัวเลขที่มีมากกว่า 32 บิต ให้ใช้โมดูล random จาก micropython-lib

โค้ดตัวอย่าง:

import random


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

CPython output:

MicroPython output:

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

เมธอด randint สามารถคืนค่าจำนวนเต็มที่มีขนาดสูงสุดเท่ากับ native word size เท่านั้น

สาเหตุ: PRNG สามารถสร้าง state ได้เพียง 32 บิตต่อครั้ง จากนั้นผลลัพธ์จะถูก cast เป็น int ขนาด native แทนที่จะเป็นออบเจ็กต์ int เต็มรูปแบบ

วิธีแก้ไข: หากต้องการจำนวนเต็มที่ใหญ่กว่า native wordsize ให้ใช้โมดูล random จาก micropython-lib

โค้ดตัวอย่าง:

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

โค้ดตัวอย่าง:

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

ไม่สามารถ override 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'