56gt 发表于 2015-12-2 14:33:44

Python定义常量

# coding=utf-8  # const.py
  class ConstAssignError(Exception): pass
  
  class _const(object):
  def __setattr__(self, k, v):
  if k in self.__dict__:
  raise ConstAssignError, "Can't rebind const (%s)" % k
  else:
  self.__dict__ = v
  
  
  def _test():
  const = _const()
  const.A = 1
  print const.A
  
  const.A = 1
  
  
  if __name__ == '__main__':
  _test()
页: [1]
查看完整版本: Python定义常量