Python:我应该使用常量还是属性?

Dec*_*n_K 4 python syntax constants

以下哪两种方法被认为是最佳实践?两者都达到相同的结果。

class Foo():
    LABELS = ('One','Two','Three')

class Bar():
    def __init__(self):
        self.__labels = ('One','Two','Three')

    @property
    def labels(self):
        return self.__labels
Run Code Online (Sandbox Code Playgroud)

Pet*_*per 5

如果您不需要自定义获取或设置行为,那么将某些内容设为属性就毫无意义。第一个版本更好。

此外,它们的行为也不完全相同。在 中Foo,标签有一个类级属性。在Bar,没有。引用Foo.LABELS将正常工作,引用Bar.labels将引发异常。