Python装饰器单例
来源:互联网 发布:犀牛软件调整模型尺寸 编辑:程序博客网 时间:2024/06/05 19:11
#!/usr/bin/python#coding: utf-8"""an elegant way to spawn singleton"""def singleton(cls, *args, **kw): """ singleton decorator """ instances = {} def _singleton(): if cls not in instances: instances[cls] = cls(*args, **kw) return instances[cls] return _singleton@singletonclass TestClass(object): a = 1 def __init__(self, x=0): self.x = xif __name__ == '__main__': one = TestClass() two = TestClass() print one.a print id(one) print id(two) print one is two one.x = 1 print one.x print two.x
0 0
- Python装饰器单例
- python装饰
- python装饰器装饰类
- [python]python装饰器
- Python装饰器学习
- Python装饰器
- python 装饰器
- Python装饰器
- python 装饰器 应用
- python 装饰器2
- Python装饰器学习
- python装饰器
- 装饰模式(python)
- python装饰器
- python 装饰器入门
- python装饰器
- python装饰器
- python 装饰器'@'
- 打印堆栈
- 从0开始学习 GitHub 系列之「初识 GitHub」
- IronWasp Web应用程序漏洞扫描
- HDU1029
- 不用加减乘除做加法47
- Python装饰器单例
- Spark源码解读(6)——Shuffle过程
- Android 可拖拽的GridView效果实现, 长按可拖拽和item实时交换
- Android内容提供者Provider
- 证明增广路算法的正确性及dinic算法的使用
- jsf2入门demo
- noip2011 day2-2 聪明的质监员
- jQuery获取Select选择的Text和 Value
- 只能游客登录以及ubuntu输入密码登录后继续回到登录界面