pythonfunctionglobal

Set company standards that are accessed by everyone's code in phython


So there are corporate identity things, like colors, fonts, etc. that seldom change but when they do everyone should implement them in the same way. For example, I want to write a separate subroutine that everyone calls to import the colors so everyone's reports have the same appearance. The question is what is the best way to pass those variables?

Option 1: normal passing of variables

def main():
company_BLUE          = "#015b95"
company_GREEN         = "#3D8B37"
company_GREY          = "#656565"
company_RED           = "#8F2D22"
return company_BLUE, company_GREEN, company_GREY, company_RED

Import and then call it using:

import getColors
[Blue, Green, Grey, Red] = getColors.main()

This one gets tedious if there are many more variables that not everyone needs.

Option 2: using globals

def main():
    global company_BLUE
    global company_GREEN
    global company_GREY
    global company_RED
    company_BLUE = "#015b95"
    company_GREEN = "#3D8B37"
    company_GREY = "#656565"
    company_RED = "#8F2D22"

This one is annoying to use having to always put global. Or you write a new variable like,

Blue = global company_BLUE

but that just adds unnecessary variables.

Option 3: somehow using classes, but I'm not sure how to adapt that here.

What are your opinions or is there an even better solution?


Solution

  • You could use a simple dataclass:

    from dataclasses import dataclass
    @dataclass
    class Color:
        BLUE = "#015b95"
        GREEN = "#3D8B37"
        GREY = "#656565"
        RED = "#8F2D22"
    company_color = Color()    
    print(company_color.RED)
    

    gives:

    #8F2D22