How do I create a constant in Python?

后端 未结 30 3092
既然无缘
既然无缘 2020-11-22 09:07

Is there a way to declare a constant in Python? In Java we can create constant values in this manner:

public static          


        
30条回答
  •  庸人自扰
    2020-11-22 09:38

    A tuple technically qualifies as a constant, as a tuple will raise an error if you try to change one of its values. If you want to declare a tuple with one value, then place a comma after its only value, like this:

    my_tuple = (0 """Or any other value""",)
    

    To check this variable's value, use something similar to this:

    if my_tuple[0] == 0:
        #Code goes here
    

    If you attempt to change this value, an error will be raised.

提交回复
热议问题