python常量管理
来源:互联网 发布:网络远程教育有学位吗 编辑:程序博客网 时间:2024/05/16 08:57
今天看书《编写高质量代码:改善python程序的91个建议》时阅读到关于常量的管理。觉得很有启发。遂记录在此。
如果有争议和版权问题,请联系我,删除相关内容。
python内置的常量只有None, True, False。没有提供直接定义常量的方法。因此,在python代码中定义常量好的方式就是编写一个类模块,专门存储常量。常量的命名方式应该是大写字母加下划线。具体如下:
class _const:class ConstError(TypeError): passclass ConstCaseError(ConstError): passdef __setattr__(self, name, value):if self.__dict__.has_key(name):raise self.ConstError, "Can not change const %s" % nameif not name.isupper():raise self.ConstCaseError, "const name %s is not all upper" % nameself.__dict__[name] = valueimport syssys.modules[__name__] = _const()
将上述代码保存为const.py
使用时只需要
import constconst.IP = '127.0.0.1'
保存为constant.py
其他模块使用常量是只需要:
from constant import constprint const.IP
0 0
- python常量管理
- python 常量实现
- python定义常量
- python设置常量参数
- python定义常量
- Python 常量与变量
- python学习之常量
- Python 字符串常量
- Python的常量
- python常量与变量
- Yii 常量的轻松管理
- Python-变量和常量
- python 中的特殊常量 None
- Python的内建常量
- 构造Python中的常量类
- Python的常量与变量
- 巧用Delphi 2010 RTTI 管理常量(const)!
- 统一的常量的存储管理
- 02-1
- [leetcode 22] Generate Parentheses
- 安卓自定义标题时候you cannot combine custom titles with other
- Linux CentOS 6.5 x64 SVN服务器搭建
- Qt之打包发布(NSIS详解)
- python常量管理
- Oracle 常用的一些参数存放位置及常用技巧
- Plus One
- 尽人事,听天命
- vim如何编辑二进制文件
- 一般java环境使用ormlite驱动sqlite
- php 重组数组
- 第三章 DispatcherServlet详解 ——跟开涛学SpringMVC
- 代码简洁之道:C++ 11 之auto+ for_each + lamda表达式