美文网首页
python使用到的代码块(持续更新中...)

python使用到的代码块(持续更新中...)

作者: longsan0918 | 来源:发表于2018-02-25 13:24 被阅读8次

1 Python构建外部常量类 供全局使用
新建 const.py文件

# -*- coding: utf-8 -*-
# @Time    : 2018/2/25 下午1:11
# @Author  : scl
# @Email   : 1163820757@qq.com
# @File    : const.py
# @Software: PyCharm

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" % name)
      if not name.isupper():
          raise self.ConstCaseError('const name "%s" is not all uppercase' % name)
      self.__dict__[name] = value

const = _const()
const.PI = 3.14

在使用的文件中 导入该模块

from const import const
print(const.PI)

相关文章

网友评论

      本文标题:python使用到的代码块(持续更新中...)

      本文链接:https://www.haomeiwen.com/subject/cclrxftx.html