1

On the code I'm viewing, I saw a class like this:

class User(ModelBase):
    """# User: docstring"""

    COMBAT_RANK_KEY_PREFIX = 'combat_rank'
    LEVEL_RANK_KEY_PREFIX = 'level_rank'
    EQUIPMENT_RANK_KEY_PREFIX = 'equipment_rank'
    WORLD_REGAIN_RANK_KEY_PREFIX = 'world_regain_rank'
    COMMANDER_RANK_KEY_PREFIX = 'commander_rank'
    LIKE_RANK_KEY_PREFIX = 'like_rank'

    # Rest part of the class is omitted here

There are other many class constant name like this in the project.
I consult PEP8 and saw no regulations for class constant.
Which only mentions golabl constant can use name like GLOBAL_CONSTANT.

But I still don't feel right to write a "class constant" like this,
but as they were wrote by my senior colleagues, I'm not sure if it's right or not.

So, is this kind of naming OK according to PEPs and Common rules?

3

PEP 8 Allows you a bit of flexibility¸but requires consistency. I would use underscored caps, as per your initial suggestion for all constants, since what will make them instantly recognizable and distinct from Class names and other variables.

So in short it is right. But when working on a project where a different patterns are follow the pattern used in the project you are working on, unless you are willing to go through the entire project and update all the naming.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.