python自定义常量类 版本:python3。8 目的:将配置信息读取并设置到常量类中 目录结构如下: 这里给出常量类和测试类代码(我对python的代码规范不是很了解,所以就按照个人习惯写的,如有问题欢迎指正) const。pyimportsysfromutilimportreadYamlclassconst:classConstError(TypeError):passclassConstCaseError(ConstError):passdefsetattr(self,name,value):ifnameinself。dict:raiseself。ConstError(cantchangeconstsname)ifnotname。isupper():raiseself。ConstCaseError(constnamesisnotalluppercasename)self。dict〔name〕value读取配置文件readutilreadYaml。readConf()DBDEVBASEreadutil。readConfDBFile(dbdev。yaml)自定义常量属性PI3。14下面的这种写法在测试类中会提示AttributeError:constobjecthasnoattributeDBDEVBASEreadutilreadYaml。readConf()constconst()const。PI3。14const。DBDEVBASEreadutil。readConfDBFile(dbdev。yaml)将系统加载的模块列表中的constant替换为const()实例sys。modules〔name〕const()print(const。DBDEVBASE)print()print(const。PI) Test。pyfromconstantimportconstprint(const。DBDEVBASE) 运行Test。py{host:127。0。0。1,port:3066,user:jimmy,passwd:handsomeGuy,db:pyDb}