I have written this code in Python 3;
dotenv_path = r"./../.env"
load_dotenv(dotenv_path=dotenv_path)
Notice how I have named the variable containing the path to the .env
file the same as the name of the argument load_dotenv()
takes. This results in ...(dotenv_path=dotenv_path)
Is this considered bad practice? If so, what can be done to more clearly differentiate between variables (apart from simply choosing a different name), not just in this specific context but in a broader sense, without compromising for reduced code readability and/or self-documentation?
It's unlikely to be considered bad practice, given that it is used more often than not in the major python packages; see e.g. a random pandas example here (takes a while to jump to the right line).
As for readability I would instead consider it beneficial to have identical names in both scopes whenever you are referring to the same thing. If appropriate, you can still use more specific names to refer to more specific concepts outside the function, such as the production environment file (e.g. dotenv_prod_path = r"./../.env.prod"; load_dotenv(dotenv_path=dotenv_prod_path)
).
If you are concerned about reduced readability as it might be unclear which scope the name belongs to, I would suggest to keep methods/functions short and not to use global and nonlocal variables.