简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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