How to create a GUID/UUID in Python
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Understanding GUID/UUID
A GUID (Globally Unique Identifier) or UUID (Universally Unique Identifier) is a 128-bit number used to uniquely identify information in computer systems. The term "UUID" is more commonly used in programming, whereas "GUID" is often synonymous in the context of Microsoft's implementation. UUIDs are standardized by the Open Software Foundation (OSF) and serve as unique identifiers for objects and entities.
UUID Versions
UUIDs can be categorized into several versions, but the most common ones are:
- Version 1: Time-based with the MAC address of the machine that generated the UUID.
- Version 3: Name-based and uses MD5 hashing.
- Version 4: Randomly generated UUID.
- Version 5: Name-based and uses SHA-1 hashing.
Creating UUIDs in Python
Python provides a built-in module called uuid to generate these identifiers. It's versatile and easy to use. Here’s a deeper dive into how you can generate different types of UUIDs using this module.
Importing the uuid Module
Before generating UUIDs, you need to import the module:
Generating Version 1 UUID
To generate a time-based version 1 UUID, you can use:
- Mechanism: Combines the current timestamp with the machine's network card MAC address to produce a unique identifier.
Generating Version 3 UUID
Use MD5 hashing for generating a name-based version 3 UUID:
- Mechanism: Combines a namespace identifier with some name using MD5 hashing. The combination is cryptographically hashed to produce the UUID.
Generating Version 4 UUID
For a random-based version 4 UUID, use:
- Mechanism: This is generated using random numbers. The likelihood of duplicates is extremely low given a high-quality random number generator.
Generating Version 5 UUID
Use SHA-1 hashing for generating a name-based version 5 UUID:
- Mechanism: Similar to version 3, but utilizes SHA-1 instead of MD5 for hashing the namespace and name.
Summary Table of UUID Versions
Below is a table summarizing key aspects of different UUID versions:
| UUID Version | Method in Python | Generation Mechanism | Use Case Example |
| Version 1 | uuid1() | Time & MAC address | Identifying system nodes |
| Version 3 | uuid3() | MD5 hashing (namespace + name) | Database keys |
| Version 4 | uuid4() | Randomly generated | General unique identifiers |
| Version 5 | uuid5() | SHA-1 hashing (namespace + name) | Stronger name-based unique keys |
Considerations When Using UUIDs
- Uniqueness: While UUIDs are designed to be unique, they are not guaranteed. Proper consideration must be given to the use case, especially in distributed systems or databases.
- Version Choice: Depending on the application, you may prefer one version over another. For instance, Version 1 might reveal host information, which could be a privacy concern.
- Performance: Version 1 and 4 are typically faster to generate as they rely on readily available data (system time, MAC address, or random number generator) instead of hashing.
- Security: For cryptographic purposes, neither MD5 (Version 3) nor SHA-1 (Version 5) are considered secure. Random generation (Version 4) is preferred when security is a concern.
Conclusion
The uuid module in Python makes it convenient to generate various types of UUIDs depending on your needs. Whether you're managing databases, ensuring the uniqueness of identifiers across distributed systems, or simply need a way to identify objects uniquely, understanding and implementing UUID generation is crucial. Always consider the context and requirements of your application when deciding on the UUID version to use.

