简体   繁体   English

python中类常量的定义

[英]Definition of class constants in python

I'm newbie in python and I wonder if I can do something like this for definition of class constant: Tks我是 python 的新手,我想知道我是否可以为类常量的定义做这样的事情:Tks

class MyClass:

    @classmethod
    @property
    def MY_CONSTANT(cls):
        return 0

No, that won't work.不,那行不通。

This is the typical pattern for class constants:这是类常量的典型模式:

class MyClass:
    MY_CONSTANT = 0

A lot of things in Python (like read-only attributes) are done by convention in Python. Python 中的很多事情(如只读属性)在 Python 中都是按照约定完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM