Software Development

What is the correct way to define and use constants in a Python project?

SU Asked by Susan Fletcher · 15-05-2025
0 upvotes 12,890 views 0 comments
The question

Coming from a Java background, I am used to using the final keyword to ensure a variable cannot be reassigned. How do I achieve this same behavior in Python to define global constants like an API_URL or a DATABASE_PORT? Is there a built-in keyword for this in the latest Python 3 versions, or is it strictly a naming convention that developers must follow to prevent accidental data modification?

3 answers

0
DE
Answered on 17-05-2025

In Python, there is no strict "constant" keyword that prevents reassignment at runtime. Instead, Python relies on the PEP 8 naming convention: you should name your variable in all capital letters with underscores, like MAX_CONNECTIONS = 100. This signals to other developers that the value should not be changed. For better enforcement during development, you can use the Final type hint from the typing module, such as TOTAL: Final = 50. While this doesn't stop the code from running if the value changes, static type checkers like Mypy will flag it as an error, providing a much cleaner development workflow.

0
MA
Answered on 19-05-2025

Are you looking for a way to enforce these constants at the class level, or are you just trying to keep your global configuration file organized for a large-scale application?

TH 21-05-2025

Mark, if it's for a large-scale application, I’d suggest putting all constants into a separate config.py file. Then, you can import them across the whole project. This keeps the logic and the data separate. Even though Python won't physically block a change to config.API_KEY, having a dedicated file makes it much less likely that a teammate will accidentally overwrite it in the middle of a functional loop.

0
MI
Answered on 23-05-2025

The standard is simply using UPPER_CASE names. Python is built on a philosophy of "we are all consenting adults," meaning we trust developers to respect the naming convention.

SU 24-05-2025

Michelle is right. Python prioritizes flexibility. If you really need to lock a value down, you'd have to use a custom @property decorator without a setter in a class, but that’s usually overkill for simple constants.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session