https://github.com/shunsock/constant_with_python
Python Constants, but without mypy
https://github.com/shunsock/constant_with_python
python python3 techblogposts
Last synced: 10 months ago
JSON representation
Python Constants, but without mypy
- Host: GitHub
- URL: https://github.com/shunsock/constant_with_python
- Owner: shunsock
- License: mit
- Created: 2025-03-07T13:33:51.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-03-07T14:24:13.000Z (10 months ago)
- Last Synced: 2025-03-07T15:29:21.090Z (10 months ago)
- Topics: python, python3, techblogposts
- Language: Python
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Constant with Python
This repository contains examples of how to define constants in Python without mypy.
## Description
Python is a dynamically typed language, which means that the type of a variable is determined at runtime. This means that you can change the type of a variable at any time. However, there are some values that are constant which means they cannot be changed.
```python
PI = 3.14
```
Usually, constants are written in uppercase letters. But, in this way, you can still change the value of the constant.
```python
from typing import Final
PI: Final[int] = 3.14
PI = "OVERWRITTEN" # This will raise an error
```
However, sometimes we can not use mypy because of organizational reasons. We introduce another way to define constants in Python.
## Usage
You can run the following command to see the output:
```shell
python reassign_class.py
# uv run reassign_class.py, just class
python reassign_dataclass.py
# uv run reassign_dataclass.py, just dataclass
python reassign_enum.py
# uv run reassign_enum.py, just enum
python reassign_slots.py
# uv run reassign_slots.py, just slots
```
## License
This repository is under the [MIT License](./LICENSE).
## Contribution
Feel free to contribute to this repository. For example, you can add new examples or improve the existing ones. Just open a pull request and I will review it as soon as possible.
Check the code style with the following command:
```shell
uv run black .
```