Question Details

No question body available.

Tags

python configuration

Answers (1)

July 23, 2025 Score: 1 Rep: 78,053 Quality: Low Completeness: 50%

For the "canonical" solution, turn Config into a function that creates the config object in a global variable.

import threading

config = None congiglock = threading.Lock()

def Config(...): global config with configlock.lock(): if config: raise Error() config = _Config(...)

Programmers no longer instantiate the configuration object, so it can be renamed.

Consumers would use the config variable via its module

import pkg.config

def action(): if pkg.config.config.foo: print("bar")

You could move the function to the package init for less typing. But you shouldn't from pkg.config import config because you might get the None before config really happened.