- 重要な情報
- はじめに
- 用語集
- ガイド
- エージェント
- インテグレーション
- OpenTelemetry
- 開発者
- API
- CoScreen
- アプリ内
- Service Management
- インフラストラクチャー
- アプリケーションパフォーマンス
- 継続的インテグレーション
- ログ管理
- セキュリティ
- UX モニタリング
- 管理
ID: python-best-practices/dataclass-special-methods
言語: Python
重大度: Notice
カテゴリー: ベストプラクティス
データクラス (@dataclass でアノテーションされたクラス) は __init__
や __gt__
などの特別なメソッドを使用しません。
@dataclass
class _Leaf(Generic[T]):
parent: _Leaf
value: T
def __init__(self, value: Optional[T] = None):
self.value = value
self.parent = self
def update(self, value: T):
self.value = value
return self
def __lt__(self, other: _Leaf):
return repr(self) < repr(other)
def __gt__(self, other: _Leaf):
return repr(self) > repr(other)
# __eq__ は使用しないでください
def __eq__(self, other: _Leaf):
return repr(self) == repr(other)
def __repr__(self):
return self.value
@dataclass
class _Leaf(Generic[T]):
parent: _Leaf
value: T
def update(self, value: T):
self.value = value
return self
def __repr__(self):
return self.value