uuid.NAMESPACE_DNS / URL / OID / X500
Fixed values published in RFC 4122, so every language computes the same uuid5 for the same name. Pick the one that matches what your name is; for your own kinds of names, make your own namespace.
Common call
uuid.uuid5(uuid.NAMESPACE_URL, 'https://example.com/')
Returns
UUID('dd2c1780-811a-5296-81c5-178a0ef488bc')
Replaces
Inventing a namespace UUID for URLs or domains
Watch out
They differ only in the last digit of the first group: 0, 1, 2 and 4
uuid.NAMESPACE_DNS · uuid.NAMESPACE_URL · uuid.NAMESPACE_OID · uuid.NAMESPACE_X500
→ uuid.UUID
Demo
Live evaluation
Look a namespace up by the short name python -m uuid uses for it.
Try:
Inputs
namestr@dns, @url, @oid, @x500
Code
import uuid namespaces = {'@dns': uuid.NAMESPACE_DNS, '@url': uuid.NAMESPACE_URL, '@oid': uuid.NAMESPACE_OID, '@x500': uuid.NAMESPACE_X500} ns = namespaces['@dns'] (str(ns), ns.version)
Result
('6ba7b810-9dad-11d1-80b4-00c04fd430c8', 1)
All four are version 1 UUIDs: time-based IDs minted when RFC 4122 was written, which is why they share every group except the first. There is no e-mail namespace; for other kinds of names, create your own namespace UUID once and keep it fixed.
Attributes
| Attribute | Type | Meaning |
|---|---|---|
| NAMESPACE_DNS | UUID | 6ba7b810-9dad-11d1-80b4-00c04fd430c8: names are fully qualified domain names ('python.org'). |
| NAMESPACE_URL | UUID | 6ba7b811-9dad-11d1-80b4-00c04fd430c8: names are URLs ('https://example.com/'). |
| NAMESPACE_OID | UUID | 6ba7b812-9dad-11d1-80b4-00c04fd430c8: names are ISO OIDs ('1.3.6.1'). |
| NAMESPACE_X500 | UUID | 6ba7b814-9dad-11d1-80b4-00c04fd430c8: names are X.500 distinguished names, in DER or a text output format. |
Common patterns
Your own namespace
Generate it once (python -c "import uuid; print(uuid.uuid4())") and paste the value into your code.
import uuid ORDERS_NS = uuid.UUID('1cc262f7-3046-59d8-af1f-724f1ed6b671') order_uuid = uuid.uuid5(ORDERS_NS, order_number)
A namespace derived from a URL
Reproducible from documentation alone: the namespace is uuid5 of your site URL.
import uuid MY_NS = uuid.uuid5(uuid.NAMESPACE_URL, 'https://example.com/ids')
Examples
1. NAMESPACE_DNS
import uuid
uuid.NAMESPACE_DNS
Returns
UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8')2. All four as text
import uuid
[str(n) for n in (uuid.NAMESPACE_DNS, uuid.NAMESPACE_URL, uuid.NAMESPACE_OID, uuid.NAMESPACE_X500)]
Returns
['6ba7b810-9dad-11d1-80b4-00c04fd430c8', '6ba7b811-9dad-11d1-80b4-00c04fd430c8', '6ba7b812-9dad-11d1-80b4-00c04fd430c8', '6ba7b814-9dad-11d1-80b4-00c04fd430c8']3. They are version 1 UUIDs
import uuid
uuid.NAMESPACE_URL.version
Returns
14. A domain under NAMESPACE_DNS
import uuid
uuid.uuid5(uuid.NAMESPACE_DNS, 'example.com')
Returns
UUID('cfbff0d1-9375-5685-968c-48ce8b15ae17')5. A URL under NAMESPACE_URL
import uuid
uuid.uuid5(uuid.NAMESPACE_URL, 'https://example.com/')
Returns
UUID('dd2c1780-811a-5296-81c5-178a0ef488bc')6. Equal to the parsed RFC value
import uuid
uuid.UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8') == uuid.NAMESPACE_DNS
Returns
TruePitfalls
1. Same name, different namespace
The namespace is part of the hash. If two systems must agree, they must agree on the namespace too.
DNS vs URL
import uuid uuid.uuid5(uuid.NAMESPACE_DNS, 'example.com') == uuid.uuid5(uuid.NAMESPACE_URL, 'example.com')
False
one agreed namespace
import uuid uuid.uuid5(uuid.NAMESPACE_DNS, 'example.com') == uuid.uuid5(uuid.NAMESPACE_DNS, 'example.com')
True
2. Expecting a namespace for every kind of name
Only DNS, URL, OID and X500 exist.
NAMESPACE_EMAIL
import uuid uuid.NAMESPACE_EMAIL
AttributeError: module 'uuid' has no attribute 'NAMESPACE_EMAIL'. Did you mean: 'NAMESPACE_OID'?
URL with mailto:
import uuid uuid.uuid5(uuid.NAMESPACE_URL, 'mailto:ada@example.com').version
5
When to use
Use it
- uuid5/uuid3 of domain names, URLs, OIDs or X.500 names: IDs other systems can reproduce
Reach for something else
- Your own record types → a private namespace UUID, so your IDs cannot collide with other people's uuid5(NAMESPACE_URL, ...)
Notes
CPython impl
Module constants at the end of Lib/uuid.py: NAMESPACE_DNS = UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8') and so on
Origin
Listed in RFC 4122 Appendix C (and RFC 9562); the same values are built into UUID libraries in other languages
CLI
python -m uuid accepts @dns, @url, @oid and @x500 as shortcuts for these four (3.12+)
FAQ
A constant UUID, 6ba7b810-9dad-11d1-80b4-00c04fd430c8, defined by RFC 4122 for naming domain names with uuid3/uuid5. uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org') is the same in every language.