🌐 English | 한국어 | 日本語 | हिन्दी | Deutsch
.py
| 要素 | Kind | 例 |
|---|---|---|
| 関数 | function |
def greet(): |
| 非同期関数 | function |
async def fetch(): |
| クラス | class |
class User: |
| メソッド | method |
def __init__(self): |
| クラスメソッド | method |
def method(cls): |
| モジュールレベル変数 | variable |
API_URL = "..." |
| コメント | doc |
# Comment |
# User model for the application.
class User:
"""Represents a user in the system."""
def __init__(self, name: str, email: str):
"""Initialize a new user."""
self.name = name
self.email = email
def get_display_name(self) -> str:
"""Return the display name."""
return f"{self.name} <{self.email}>"
# Create a new user instance.
def create_user(name: str, email: str) -> User:
"""Factory function to create a user."""
return User(name, email)
async def fetch_user(user_id: int) -> User:
"""Fetch a user from the database."""
pass<file path="user.py" language="python">
<signature kind="class" line="2">
<name>User</name>
<text>class User</text>
<doc>User model for the application.</doc>
</signature>
<signature kind="method" line="5">
<name>__init__</name>
<text>def __init__(self, name: str, email: str)</text>
</signature>
<signature kind="method" line="10">
<name>get_display_name</name>
<text>def get_display_name(self) -> str</text>
</signature>
<signature kind="function" line="16">
<name>create_user</name>
<text>def create_user(name: str, email: str) -> User</text>
<doc>Create a new user instance.</doc>
</signature>
<signature kind="function" line="21">
<name>fetch_user</name>
<text>async def fetch_user(user_id: int) -> User</text>
</signature>
</file>- Pythonはすべての要素をpublicとして扱う
_privateまたは__mangled命名規則も含まれる
- 最初のパラメータが
selfまたはclsの場合はmethodに分類 - それ以外は
functionに分類 @staticmethodデコレータが付いたメソッドはfunctionに分類(selfなし)
async defはfunctionkindに統一- シグネチャテキストに
asyncキーワードを含む
--include-bodyフラグ未使用時:
- 関数/メソッド:シグネチャ末尾のコロン(
:)以降の本体を削除 - クラス:クラス名と継承情報のみ保持
--include-privateを使用して非公開/unexportedシンボルを含める。
複雑な型ヒント(例:Dict[str, int])のコロンは関数終端のコロンと区別される:
def func(x: Dict[str, List[int]]) -> str: # 最後のコロンのみ関数終端- 現在のバージョン:関数/クラス上の
#コメントのみdocとしてキャプチャ - 将来のバージョン:トリプルクォートdocstring(
"""...""")サポート予定
デコレータはシグネチャに含まれない:
@decorator
def func(): # シグネチャ: "def func()"- Lambda式
- ネストされた関数(外側の関数のみキャプチャ)