Python常量工具类,,1.定义常量类con
Python常量工具类,,1.定义常量类con
1.定义常量类constant.py
# -*- coding: utf-8 -*"""常量工具类author: Jillusage: from constant import _Const const = _Const() const.PI = 3.14"""class _Const: class ConstError(TypeError): pass class ConstCaseError(ConstError): pass def __setattr__(self, name, value): if name in self.__dict__: raise self.ConstError("Can‘t change const(%s) value" % name) if not name.isupper(): raise self.ConstCaseError("Const name %s is not all uppercase" % name) self.__dict__[name] = value def __getitem__(self, key): if key in self.__dict__: return self.__dict__[key] else: raise self.ConstError("Can‘t return const %s, No Existing Key!" % key) def __delattr__(self, name): if name in self.__dict__: raise self.ConstError("Can‘t unbind const(%s)" % name) raise NameError(name)
2.将常量放在一个模块中common_constant.py
# -*- coding: utf-8 -*"""常用常量定义author: Jillusage: from common_constant import const print(const.ROOT_PATH)"""from common.constant import _Constconst = _Const()const.PI = 3.14if __name__ == "__main__": print(const.PI) print(const[‘PI‘])
3.在其他模块里使用test.py
from common_constant import const print(const.ROOT_PATH)
Python常量工具类
相关内容
- "#!/usr/bin/python: No such file or director"引发的
- Python中的异常处理的习题,,1 定义一个函数fu
- 一个简单的监控redis性能的python脚本,,一个简单的监控
- python爬取小说,,运行结果:代码: 1
- Python mysql-表的创建,删除和更新,,2017-09-06
- Python进阶:如何实现用 Python代码生成视频的缩略图,
- Python3基础 多分支结构 if-elif-else,,镇场诗:———大梦
- python 杂七杂八 :,,subprocess
- python 基础2,,三元运算result
- python_原始_web框架,,创:10_4_201
评论关闭