python没有命名常量_python中定义常量

实际开发中我们经常会碰到需要定义常量的情形,c语言提供了const关键字来实现,但是python本身却没有提供const,要想在python代码中定义常量,需要我们自己来实现。

下面定义一个const类。要求符合“命名全部为大写”和“值一旦绑定便不可再修改”这两个条件。代码

class const(object):

class ConstError(TypeError):

pass

class ConstCaseError(ConstError):

pass

def __setattr__(self, name, value):

if self.__dict__.has_key(name):

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

import sys

sys.modules[__name__] = const()

这里通过对常量对应的值进行修改时或者命名不符合规范时抛出异常来满足以上常量的两个条件。

###NOTE

使用sys.modules[name]可以获取一个模块对象,并可以通过该对象获取模块的属性,这儿使用了sys.modules向系统字典注入了一个const对象从而实现了在执行import const时实际获取了一个const实例的功能,sys.module在文档中的描述如下:

sys.modules

This is a dictionary that maps module names to modules which have already been loaded. This can be manipulated to force reloading of modules and other tricks. Note that removing a module from this dictionary is not the same as calling reload() on the corresponding module object.

sys.modules[name] = const()这行代码将系统已经加载的模块列表中的const替换为了const(),即一个const实例

##使用

整个工程需要使用的常量都应该定义在一个文件中。例如你将const.py放在了project.utils目录下,常量被定义在project.apps目录下的project_consts.py文件中,那么应该这么写:

#project_consts.py

from project.utils import const

const.NAME = 'IBM'

const.EMAIL = 'ibm.com'

在其他文件中需要使用这些常量时,用如下方式调用:

from project.apps.project_consts import const

print const.NAME

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值