Pythonic way? In module, conditionally import for other similar modules or import unique module with multiple options?

Viewed 43

I would like to know your opinion about which of these two scenarios is more pythonic.

Imagine that we have two modules of constants with same names but different values and depending on a value polite, we should use one or another. Which one is more pythonic? This is a very basic and reduced example of a real situation that I am considering.

Scenario 1

cts_not_polite.py

CT_HI = 'Hi'
CT_BYE = 'Bye'

cts_polite.py

CT_HI = 'Hello'
CT_BYE = 'Goodbye'

other_module.py

def function():
    if polite:
        import cts_polite as cts
    else:
        import cts_not_polite as cts
    
    print(cts.CT_HI)
    print(cts.CT_BYE)

Scenario 2

cts.py

CT_HI = 'Hi'
CT_HI_POLITE = 'Hello'
CT_BYE = 'Bye'    
CT_BYE_POLITE = 'Goodbye'

other_module.py

import cts

....

def function()

    if polite:
        a = cts.CT_HI_POLITE
        b = cts.CT_BYE_POLITE
    else:
        a = cts.CT_HI
        b = cts.CT_BYE
    
    print(a)
    print(b)

Thank you very much in advance!

0 Answers
Related