如何在Python中创建常量?有办法在Python中声明常量吗?在Java中,我们可以这种方式创建常量值:public static final String CONST_NAME = "Name";Python中上述Java常量声明的等效性是什么?
3 回答
慕勒3428872
TA贡献1848条经验 获得超6个赞
__method
_method
MY_CONSTANT = "one"
def MY_CONSTANT(): return "one"
MY_CONSTANT = "one"
>>> from collections import namedtuple>>> Constants = namedtuple('Constants', ['pi', 'e']) >>> constants = Constants(3.14, 2.718)>>> constants.pi3.14>>> constants.pi = 3Traceback (most recent call last): File "<stdin>", line 1, in <module>AttributeError: can't set attribute
添加回答
举报
0/150
提交
取消