모듈#
Generated Mon 03 Aug 2026 05:40:41 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은 코드 크기와 실행 시간을 줄이기 위해 암묵적 잘림(truncation)을 구현합니다.
해결 방법: 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
|
step != 1인 구독(subscript)이 아직 구현되지 않음#
예제 코드:
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비트를 초과하는 숫자가 필요한 경우 micropython-lib의 random 모듈을 활용하십시오.
예제 코드:
import random
x = random.getrandbits(64)
print("{}".format(x))
CPython output: | MicroPython output: |
11947790255791459587
| Traceback (most recent call last):
File "<stdin>", line 11, in <module>
ValueError: bits must be 32 or less
|
randint 메서드는 기본 워드 크기 이하의 정수만 반환할 수 있습니다.#
원인: PRNG는 한 번에 32비트의 상태만 생성할 수 있습니다. 결과는 전체 int 객체가 아닌 기본 크기의 int로 캐스팅됩니다.
해결 방법: 기본 워드 크기보다 큰 정수가 필요한 경우 micropython-lib의 random 모듈을 사용하십시오.
예제 코드:
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'
|