Modüller¶
Generated Fri 19 Jun 2026 22:08:45 UTC
Yalnızca konumsal parametreler¶
Kod boyutunu azaltmak amacıyla, CPython’da anahtar sözcük bağımsız değişkeni kabul eden pek çok fonksiyon MicroPython’da yalnızca konumsal bağımsız değişken kabul eder.
MicroPython, yalnızca konumsal parametreleri CPython ile aynı şekilde işaretler: konumsal parametrelerin sonunu belirtmek için / ekler. İmzası / ile biten herhangi bir fonksiyon yalnızca konumsal bağımsız değişken alır. Daha fazla ayrıntı için bkz. PEP 570.
Örnek¶
Örneğin, CPython 3.4’te socket.socket yapıcısının imzası şöyledir:
socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None)
Ancak MicroPython için belgelenen imza şöyledir:
socket(af=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP, /)
Parametrelerin sonundaki /, MicroPython’da hepsinin yalnızca konumsal olduğunu gösterir. Aşağıdaki kod CPython’da çalışır ancak çoğu MicroPython portunda çalışmaz:
import socket
s = socket.socket(type=socket.SOCK_DGRAM)
MicroPython bir istisna fırlatacaktır:
TypeError: function doesn't take keyword arguments
Aşağıdaki kod hem CPython hem de MicroPython’da çalışacaktır:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
array¶
Farklı tür kodları arasında karşılaştırma desteklenmemektedir¶
Neden: Kod boyutu
Geçici çözüm: Tek tek öğeleri karşılaştırın
Örnek kod:
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:
|
Taşma denetimi uygulanmamıştır¶
Neden: MicroPython, kod boyutunu ve yürütme süresini azaltmak için örtük kesme uygular
Geçici çözüm: CPython uyumluluğu gerekiyorsa değeri açıkça maskeleyin
Örnek kod:
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])
|
Tamsayı arama uygulanmamıştır¶
Örnek kod:
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:
|
Dizi silme uygulanmamıştır¶
Örnek kod:
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
|
Adım != 1 olan indisleme henüz uygulanmamıştır¶
Örnek kod:
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.
Örnek kod:
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 modülü, nesne serileştirilemediğinde istisna fırlatmaz¶
Örnek kod:
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 özelliği uygulanmamıştır¶
Geçici çözüm: getenv, putenv ve unsetenv kullanın
Örnek kod:
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 önbelleğe alınan değer yerine gerçek değeri döndürür¶
Neden: environ özelliği uygulanmamıştır
Örnek kod:
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 metodu bir seferde en fazla 32 bit döndürebilir.¶
Neden: PRNG’nin iç durumu yalnızca 32 bit olduğundan bir seferde en fazla 32 bit veri döndürebilir.
Geçici çözüm: 32 bitten fazla sayıya ihtiyacınız varsa micropython-lib’den random modülünü kullanın.
Örnek kod:
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
|
randint metodu yalnızca en fazla yerel sözcük boyutunda bir tamsayı döndürebilir.¶
Neden: PRNG bir seferde yalnızca 32 bitlik durum üretebilir. Sonuç daha sonra tam bir int nesnesi yerine yerel boyutlu bir int’e dönüştürülür.
Geçici çözüm: Yerel sözcük boyutundan büyük tamsayılara ihtiyacınız varsa micropython-lib’den random modülünü kullanın.
Örnek kod:
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¶
Örnek kod:
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¶
Örnek kod:
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¶
Neden: MicroPython, kod boyutu için optimize edilmiştir.
Geçici çözüm: Biçim dizelerinde boşluk kullanmayın.
Örnek kod:
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 ve sys.stderr’ın geçersiz kılınması mümkün değildir¶
Neden: Salt okunur bellekte saklanmaktadırlar.
Örnek kod:
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'
|