Python Singleton
2016-01-18 20:23
771 查看
Python Singleton
首先声明一个singleton装饰器
[code]#!/usr/bin/env python # -*- coding: utf-8 -*- def singleton(cls, *args, **kw): instances = {} def _singleton(): if cls not in instances: instances[cls] = cls(*args, **kw) return instances[cls] return _singleton
使用@singleton注解在需要单例的类上标注
[code]#!/usr/bin/env python # -*- coding: utf-8 -*- @singleton class Config: def __init__(self): pass def test(self): pass
测试
[code]#!/usr/bin/env python # -*- coding: utf-8 -*- config = Config() config.test() ...
相关文章推荐
- 【python】一维二维插值
- Python中optionParser模块的使用方法[转]
- 使用Python找出历史上5星好评数最多的50部电影,基于MovieLens20M数据集
- python学习笔记(一)
- python
- Python进程和线程
- 协同过滤算法python实现简单入门详细注释
- 使用python处理表格:python-texttable官方使用说明
- python (1)一个简单的爬虫: python 在windows下 创建文件夹并写入文件
- 找出假币
- python学习笔记-Day1
- Python学习之--文件操作
- 在Window平台下安装xgboost的Python版本
- Python求索之路2——深浅拷贝&函数
- python:编写登陆接口(day 1)
- zabbix使用微信报警python脚本
- Python 5.1 使用 __slots__
- Python脚本运行出现语法错误:IndentationError: unindent does not match any outer indentation level
- python 写excal
- python 字符编码