Home > other >  Overriding "constants" from parent class python
Overriding "constants" from parent class python

Time:05-24

I have a simple inheritance setup as follows:

class A:
    CONST_VAR = "aaa"
    # more code where CONST_VAR does not change
    # ...

class B(A):
    CONST_VAR = "bbb"
    # more code where CONST_VAR does not change
    # ...

So within each class CONST_VAR is indeed constant, however it clearly changes between classes. Is CAPITAL_CASE the correct convention for CONST_VAR? Can anyone point to the appropriate PEP or any style guide?

CodePudding user response:

I believe PEP8 recommends using capital case for constants, but I usually interpret this as "module-level constants".

On the other hand, this is a class variable and PEP8 doesn't specify the naming for class variables.

But if we look at Google Style Guide for Python, here it clearly states that class constants should also be capitalized:

Global/Class Constants CAPS_WITH_UNDER

CodePudding user response:

Yes, all uppercase is the convention for constants. See PEP 8.

CodePudding user response:

These are two different constants, A.CONST_VAR and B.CONST_VAR. Clearly all capitals is correct.

  • Related