UUIDs are 128-bit identifiers suited to systems where separate processes need keys without a central counter. uuid4() uses randomness, while the UUID constructor validates and normalizes received representations.
from uuid import UUID, uuid4
identifier = uuid4()
stored = str(identifier)
parsed = UUID(stored)
assert parsed == identifier
print(identifier.version)
How to use it safely
Choose a version according to privacy, ordering, and interoperability requirements. A UUID does not provide authorization or prove that a resource exists. Prefer a native UUID database type when available and validate text at the application boundary.
To strengthen the foundation, read Python collections guide and type hints guide.
The official Python documentation, accessed July 22, 2026, describes the API, edge cases, and version compatibility.