python设置常量
2019-06-17 本文已影响0人
伟大的洪立
# -*- coding:utf-8 -*-
class Const(object):
class ConsError(TypeError):
pass
class ConstCaseError(ConsError):
pass
def __setattr__(self, name, value):
if name in self.__dict__:
raise (self.ConsError, "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.MY_CONSTANT = 1
const.MY_SECOND_CONSTANT = 2
const.MY_THIRD_CONSTANT = 'a'
const.MY_FORTH_CONSTANT = 'b'