How to document a module constant in Python?

后端 未结 8 1172
忘掉有多难
忘掉有多难 2020-12-13 11:57

I have a module, errors.py in which several global constants are defined (note: I understand that Python doesn\'t have constants, but I\'ve defined them by conv

8条回答
  •  情话喂你
    2020-12-13 12:38

    Writing only because I haven't seen this option in the answers so far:

    You can also define your constants as functions that simply return the desired constant value when called, so for example:

    def get_const_my_const() -> str:
        """Returns 'my_const'."""
        return "my_const"
    

    This way they'll be a bit "more constant" on one hand (less worrying about reassignment) and they'll also provide the opportunity for regular documentation, as with any other function.

提交回复
热议问题