如何在Python中創(chuàng)建常量?有辦法在Python中聲明常量嗎?在Java中,我們可以這種方式創(chuàng)建常量值:public static final String CONST_NAME = "Name";Python中上述Java常量聲明的等效性是什么?
3 回答

慕勒3428872
TA貢獻1848條經(jīng)驗 獲得超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
提交
取消