Python实现单例模式常量类
生活随笔
收集整理的這篇文章主要介紹了
Python实现单例模式常量类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import osclass Singleton:"""單例模式"""def __new__(cls, *args, **kwargs):if not hasattr(cls, "_instance"):cls._instance = super(Singleton, cls).__new__(cls)return cls._instanceclass Const(Singleton):"""單例常量類"""class ConstError(TypeError):passclass ConstCaseError(ConstError):passdef __setattr__(self, name, value):if name in self.__dict__:raise self.ConstError(f"can't change const {name}")self.__dict__[name] = valueconst = Const() # 常量實例
總結
以上是生活随笔為你收集整理的Python实现单例模式常量类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode Algorithm 2
- 下一篇: websocket python爬虫_p