How do I create a constant in Python?

后端 未结 30 3172
既然无缘
既然无缘 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:20

    Properties are one way to create constants. You can do it by declaring a getter property, but ignoring the setter. For example:

    class MyFinalProperty(object):
    
        @property
        def name(self):
            return "John"
    

    You can have a look at an article I've written to find more ways to use Python properties.

提交回复
热议问题