python-fundamentals-313 について
現代的な構文やランタイム機能を学ぶ開発者向けのPython 3.13以降の基礎スキルです。例を通じて例外、メモリ、命名規則、基本構文を解説します。
主な機能
- 01Python 3.13のランタイム機能
- 02パターンマッチングと代入式
- 03ジェネリック型パラメーター構文
- 04例外グループと例外処理
- 05データクラス、slots、列挙型
活用シーン
- 01Python 3.13のフリースレッドとJITを学ぶ
- 02現代的なパターンマッチングコードを書く
- 03ジェネリック関数やクラスを定義する
- 04複数の例外をまとめて処理する
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ josiahsiegel/ claude-plugin-marketplace --skill python-fundamentals-313インストール後、Claude Code で/python-fundamentals-313を入力して読み込まれたか確認します。
詳細
クイックリファレンス
| 機能 | バージョン | 構文 |
|---|---|---|
| フリースレッド | 3.13t | python3.13t script.py |
| JIT コンパイラー | 3.13 | PYTHON_JIT=1 python3.13 script.py |
| パターンマッチング | 3.10+ | match x: case {...}: |
| セイウチ演算子 | 3.8+ | if (n := len(x)) > 10: |
| 型パラメーター | 3.12+ | def first[T](items: list[T]) -> T: |
| 型エイリアス | 3.12+ | type Point = tuple[float, float] |
| 構文 | コード | 用途 |
|---|---|---|
| 例外グループ | ExceptionGroup("msg", [e1, e2]) | 複数のエラー |
| except* | except* ValueError as eg: | グループの処理 |
| @dataclass(slots=True) | メモリー効率が高い | 大量のオブジェクト |
| StrEnum | class Status(StrEnum): | 文字列列挙型(3.11+) |
このスキルを使う場面
Python 3.13+ の基礎に使います:
- Python 3.13 の新機能(フリースレッド、JIT)を学ぶ
- 最新のパターンマッチング構文を使う
- 型注釈付きのジェネリック関数を書く
- データクラスと列挙型を作成する
- Python の命名規則を理解する
関連スキル:
- 型ヒントについては、
python-type-hintsを参照してください - 非同期処理については、
python-asyncioを参照してください - よくある間違いについては、
python-gotchasを参照してください
Python 3.13+ の基礎
概要
2024 年 10 月にリリースされた Python 3.13 では、実験的なフリースレッド(no-GIL)モード、JIT コンパイラー、強化されたエラーメッセージ、改善された REPL など、パフォーマンスが大幅に向上しています。
Python 3.13 の新機能
フリースレッドモード(実験的)
# Python 3.13t - Free-threaded build (GIL disabled)
# Use python3.13t or python3.13t.exe
import threading
import time
def cpu_bound_task(n):
"""CPU-intensive calculation that benefits from true parallelism"""
total = 0
for i in range(n):
total += i * i
return total
# With free-threading, these actually run in parallel
threads = []
for _ in range(4):
t = threading.Thread(target=cpu_bound_task, args=(10_000_000,))
threads.append(t)
t.start()
for t in threads:
t.join()
主なポイント:
- 専用の実行可能ファイル
python3.13tが必要です --disable-gilオプションを指定してビルドします- CPU 負荷の高いマルチスレッドワークロードに最適です
- 科学技術計算とデータ分析で最も効果を発揮します
JIT コンパイラー(実験的)
# Enable JIT with environment variable or flag
# PYTHON_JIT=1 python3.13 script.py
# JIT provides 5-15% speedups, up to 30% for computation-heavy tasks
def fibonacci(n: int) -> int:
"""JIT-optimized computation"""
if n <= 1:
return n
a, b = 0, 1
for _ in range(n - 1):
a, b = b, a + b
return b
# Hot loops benefit most from JIT compilation
result = fibonacci(10000)
改善された対話型 REPL
# New REPL features (automatic in Python 3.13):
# - Multiline editing with history preservation
# - Colored prompts and tracebacks (default)
# - F1: Interactive help browsing
# - F2: History browsing (skips output)
# - F3: Paste mode for larger code blocks
# - Direct commands: help, exit, quit (no parentheses needed)
より分かりやすいエラーメッセージ
# Python 3.13 provides:
# - Colored tracebacks by default
# - Suggests correct keyword if incorrect one passed
# - Warns when script name shadows stdlib module
# Example: If you name a file "random.py"
# Python now shows: "Note: File '/path/random.py' shadows the 'random' module"
最新の Python 構文
パターンマッチング(3.10+)
def process_command(command: dict) -> str:
match command:
case {"action": "create", "name": str(name)}:
return f"Creating {name}"
case {"action": "delete", "id": int(id_)}:
return f"Deleting item {id_}"
case {"action": "update", "id": int(id_), "data": dict(data)}:
return f"Updating {id_} with {data}"
case {"action": action}:
return f"Unknown action: {action}"
case _:
return "Invalid command format"
# With guards
def categorize_value(value):
match value:
case int(n) if n < 0:
return "negative"
case int(n) if n == 0:
return "zero"
case int(n) if n > 0:
return "positive"
case float(f) if f.is_integer():
return "float-integer"
case str(s) if len(s) > 10:
return "long-string"
case _:
return "other"
セイウチ演算子(:=)
# Assignment expressions
if (n := len(data)) > 10:
print(f"Processing {n} items")
# In comprehensions
filtered = [y for x in data if (y := transform(x)) is not None]
# In while loops
while (line := file.readline()):
process(line)
# Avoid in simple cases - prefer explicit assignment
# Bad: if (x := get_value()) > 0: use(x)
# Good: x = get_value(); if x > 0: use(x)
F 文字列(3.12+ で強化)
# Basic f-strings
name = "World"
print(f"Hello, {name}!")
# Expressions
items = [1, 2, 3]
print(f"Sum: {sum(items)}")
# Format specifiers
value = 123.456789
print(f"{value:.2f}") # 123.46
print(f"{value:>10.2f}") # ' 123.46'
print(f"{value:_}") # 123.456789 (underscore separator)
# Debug format (3.8+)
x = 42
print(f"{x=}") # x=42
print(f"{x=:>5}") # x= 42
# Nested quotes (3.12+)
data = {"key": "value"}
print(f"Value: {data["key"]}") # Now allowed!
# Multiline f-strings (3.12+)
result = f"""
Name: {user.name}
Email: {user.email}
Status: {"Active" if user.active else "Inactive"}
"""
型パラメーター構文(3.12+)
# New generic syntax - no need for TypeVar
def first[T](items: list[T]) -> T:
return items[0]
# Generic classes
class Stack[T]:
def __init__(self) -> None:
self._items: list[T] = []
def push(self, item: T) -> None:
self._items.append(item)
def pop(self) -> T:
return self._items.pop()
# Type aliases (3.12+)
type Point = tuple[float, float]
type Vector[T] = list[T]
# Multiple type parameters
def merge[K, V](d1: dict[K, V], d2: dict[K, V]) -> dict[K, V]:
return {**d1, **d2}
# Bounded type parameters
from collections.abc import Comparable
def max_value[T: Comparable](items: list[T]) -> T:
return max(items)
メモリーとパフォーマンス
メモリーの最適化
# __slots__ for memory efficiency
class Point:
__slots__ = ('x', 'y')
def __init__(self, x: float, y: float):
self.x = x
self.y = y
# Generator expressions (lazy evaluation)
# Bad: sum([x * x for x in range(1000000)]) # Creates full list
# Good: sum(x * x for x in range(1000000)) # Generator
# Use itertools for memory efficiency
from itertools import islice, chain, filterfalse
def chunked(iterable, n):
"""Yield successive n-sized chunks."""
it = iter(iterable)
while chunk := list(islice(it, n)):
yield chunk
Python 3.13 のメモリー改善
# 7% smaller memory footprint vs 3.12
# Docstrings have leading indentation stripped
# Modified mimalloc allocator (default if platform supports)
# Check memory usage
import sys
obj = {"key": "value"}
print(sys.getsizeof(obj))
# For deep size calculation
def get_size(obj, seen=None):
"""Recursively calculate object size."""
size = sys.getsizeof(obj)
if seen is None:
seen = set()
obj_id = id(obj)
if obj_id in seen:
return 0
seen.add(obj_id)
if isinstance(obj, dict):
size += sum(get_size(k, seen) + get_size(v, seen) for k, v in obj.items())
elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes)):
size += sum(get_size(i, seen) for i in obj)
return size
例外処理
例外グループ(3.11+)
# Raise multiple exceptions
def validate_data(data: dict):
errors = []
if not data.get("name"):
errors.append(ValueError("name is required"))
if not data.get("email"):
errors.append(ValueError("email is required"))
if not data.get("age") or data["age"] < 0:
errors.append(ValueError("age must be positive"))
if errors:
raise ExceptionGroup("Validation failed", errors)
# Handle with except*
try:
validate_data({})
except* ValueError as eg:
for error in eg.exceptions:
print(f"Validation error: {error}")
# Catch specific exception types
try:
risky_operation()
except* TypeError as eg:
handle_type_errors(eg.exceptions)
except* ValueError as eg:
handle_value_errors(eg.exceptions)
例外に注記を追加する(3.11+)
try:
process_data(data)
except ValueError as e:
e.add_note(f"Processing data: {data[:100]}...")
e.add_note(f"Timestamp: {datetime.now()}")
raise
# Notes appear in traceback after exception message
ベストプラクティス
命名規則
# Variables and functions: snake_case
user_name = "john"
def calculate_total(items):
pass
# Classes: PascalCase
class UserAccount:
pass
# Constants: SCREAMING_SNAKE_CASE
MAX_CONNECTIONS = 100
DEFAULT_TIMEOUT = 30
# Private: single underscore prefix
class MyClass:
def __init__(self):
self._internal_value = 42 # Convention: "private"
def __private_method(self): # Name mangling
pass
# Dunder methods: double underscore
def __init__(self):
pass
コンテキストマネージャー
from contextlib import contextmanager, asynccontextmanager
from typing import Iterator, AsyncIterator
# Class-based context manager
class DatabaseConnection:
def __init__(self, url: str):
self.url = url
self.connection = None
def __enter__(self):
self.connection = connect(self.url)
return self.connection
def __exit__(self, exc_type, exc_val, exc_tb):
if self.connection:
self.connection.close()
return False # Don't suppress exceptions
# Generator-based context manager
@contextmanager
def temporary_directory() -> Iterator[str]:
import tempfile
import shutil
path = tempfile.mkdtemp()
try:
yield path
finally:
shutil.rmtree(path)
# Async context manager
@asynccontextmanager
async def async_session() -> AsyncIterator[Session]:
session = await create_session()
try:
yield session
finally:
await session.close()
データクラス
from dataclasses import dataclass, field
from typing import Optional
@dataclass
class User:
name: str
email: str
age: int = 0
tags: list[str] = field(default_factory=list)
id: Optional[int] = field(default=None, repr=False)
# Frozen (immutable)
@dataclass(frozen=True)
class Point:
x: float
y: float
# With slots (memory efficient, 3.10+)
@dataclass(slots=True)
class OptimizedData:
value: int
label: str
# Post-init processing
@dataclass
class Rectangle:
width: float
height: float
area: float = field(init=False)
def __post_init__(self):
self.area = self.width * self.height
# Comparison and ordering
@dataclass(order=True)
class Priority:
priority: int
name: str = field(compare=False)
列挙型
from enum import Enum, auto, StrEnum, IntEnum
class Status(Enum):
PENDING = "pending"
ACTIVE = "active"
COMPLETED = "completed"
CANCELLED = "cancelled"
# With auto values
class Color(Enum):
RED = auto()
GREEN = auto()
BLUE = auto()
# String enum (3.11+)
class HttpMethod(StrEnum):
GET = auto()
POST = auto()
PUT = auto()
DELETE = auto()
# Integer enum
class Priority(IntEnum):
LOW = 1
MEDIUM = 2
HIGH = 3
CRITICAL = 4
# Usage
status = Status.ACTIVE
print(status.value) # "active"
print(status.name) # "ACTIVE"
# Pattern matching with enums
match status:
case Status.PENDING:
print("Waiting...")
case Status.ACTIVE:
print("Processing...")
case Status.COMPLETED | Status.CANCELLED:
print("Done")
コメント
Markdown 対応