Python Glossary Русский

Python 3.14+uuidfunction

uuid.uuid7()

generating UUIDs

A version 7 UUID: Unix-time milliseconds first and random bits after. It sorts by creation time — a good primary key. New in 3.14.

Syntax

uuid.uuid7()

The main difference from uuid4(): new values increase, so inserts land at the end of a database index instead of all over it. It holds no MAC, but the creation time can be read back (the time attribute is Unix milliseconds), so it does not suit public links where the creation moment is secret. Consecutive calls in one process increase strictly even within one millisecond: a counter keeps the order.

Examples

import uuid
ids = [uuid.uuid7() for _ in range(5)]
print(ids[0])  # → ? 0199b8e2-4a1b-7c3d-8e4f-5a6b7c8d9e0f — зависит от времени
print(ids == sorted(ids))  # → True
print(ids[0].version, len(str(ids[0])))  # → 7 36
import time, uuid
u = uuid.uuid7()
print(abs(u.time - time.time_ns() // 10**6) < 1000)  # → True — мс Unix

See also

Open in the full glossarydocs.python.orgReport an inaccuracy