timmy / Python Mixin 設計模式
0 gustos
0 bifurcaciones
1 archivos
Última actividad 10 months ago
Mixin 讓類別能以組合方式擴展功能,如日誌記錄、時間標記、權限管理、事件驅動等,提高程式的模組化與可維護性。適合用於 Python 物件導向開發。
| 1 | import datetime |
| 2 | import json |
| 3 | import traceback |
| 4 | import threading |
| 5 | |
| 6 | class LoggingMixin: |
| 7 | """ 提供日誌功能的 Mixin 類別,可選擇寫入控制台或檔案 """ |
| 8 | |
| 9 | def __init__(self, log_to_file=False, log_file="app.log", *args, **kwargs): |
| 10 | self.log_to_file = log_to_file |
Siguiente
Anterior