When to use UUID?
Use UUID (Universally Unique Identifier) when you need a unique identifier across distributed systems, databases, or applications, especially when there's a risk duplication with traditional ID methods. UUIDs are ideal for ensuring global uniqueness without central coordination, making them useful for generating unique keys in databases, identifying resources in APIs, and tracking objects in large systems.
Structure of UUID
The UUID contains hexadecimal digits, which means it includes numbers from 1 to 9 and alphabets from A to F. The structure of Python UUID can be represented as 8-4-4-4-12, containing a total of 32 characters.
Let’s discuss briefly the structure of Python UUID.
UUID = time_low "-" time_mid "-"time_high_and_version "-"clock_seq_and_reserved_And_clock_seq_low"-" Node.
Following are the parameters used in the structure of UUID
- time_low: The first 8 characters or 32 bits of the ID are represented by “time_low”. It gives the low 32 bits of the time.
- time_mid: The following 4 characters or 16 bits of the ID are represented by “time_mid”. It gives the mid 16 bits of the time.
- time_high_and_version: The following 4 characters or 16 bits of the ID are represented by “time_high_and_version”. It gives a 4-bit "version" in the most significant bits, followed by the high 12 bits of the time.
- clock_seq_and_reserved_And_clock_seq_low: It represents the next 16 bits of the ID giving the 1-3 bit variant in the most significant bits and 13 to 15 bits of clock sequence.
- Node: It represents the last 48 bits of the Python ID.
An example of a Python UUID is
ab2d0fc0-7224-11ec-8ef2-b658b885fb3
Also read, Python filename extensions
Generating Python UUID
Let’s learn more about Python UUID with the help of some examples.
Example 1:
Using uuid1() for creating a Python uuid.
import uuid
print("1st Python UUID generated using uuid1: ", uuid.uuid1())
print("2nd Python UUID generated using uuid1: ", uuid.uuid1())
You can also try this code with Online Python Compiler
Run Code
Output
1st Python UUID generated using uuid1: dadc8e42-e516-11ed-8ff1-8713f965a59e
2nd Python UUID generated using uuid1: dadc91bc-e516-11ed-8ff1-8713f965a59e
Explanation:
As you can see in the above example, in output, some parts of the Python uuid generated are the same as uuid1() generates a unique ID using date-time and the MAC address. The MAC address for the device is the same, so it reflects in the output.
Example 2:
Generating Python uuid using uuid4().
import uuid
print("1st Python UUID generated using uuid4: ", uuid.uuid4())
print("2nd Python UUID generated using uuid4: ", uuid.uuid4())
You can also try this code with Online Python Compiler
Run Code
Output
1st Python UUID generated using uuid4: 48558b3c-3263-4e93-be1e-2b64bf107649
2nd Python UUID generated using uuid4: 76427134-49c2-4c54-aa24-8c2ffadebe61
Explanation:
In this example, the two Python UUIDs generated do not have any component in common because the uuid4() generates random numbers. We prefer to use uuid4() instead of uuid1() in case of secure privacy.
Example 3:
Use of uuid3() and uuid5() in Python
Both the identifiers, uuid3() and uuid5(), generate UUIDs by hashing namespace identifiers with the given name. The major difference between them is the hashing technique they implement. The uuid3() uses MD5 hashing technique; in contrast, the uuid5() uses SHA-1 as hashing technique.
Syntax:
uuid3(namespace_identifier, name/string)
uuid5(namespace_identifier, name/string)
Following are the different namespace identifiers used with UUID3 and UUID5,
- NAMESPACE_DNS generates UUIDs representing the domain name,
- NAMESPACE_URL generates UUIDs representing the URL,
- NAMESPACE_OID is specified as the string ISO OID, and
- NAMESPACE_X500.
Let’s generate some UUIDs using the above-mentioned identifiers.
For example,
import uuid
Page = "www.codingninjas.com"
print(uuid.uuid3(uuid.NAMESPACE_DNS, Page))
print(uuid.uuid3(uuid.NAMESPACE_URL, Page))
print(uuid.uuid3(uuid.NAMESPACE_OID, Page))
print(uuid.uuid3(uuid.NAMESPACE_X500, Page))
You can also try this code with Online Python Compiler
Run Code
Output:
3488a56f-4399-39a9-a63d-5105d9f928f5
45ef213c-0751-35eb-b892-1803e266f63f
b52c87d8-ab3e-3cc0-b95e-7c318764369f
98ab6268-d965-3e07-a286-f1f5b6d2f234
Explanation:
Every time we use the above commands to get the ID for www.codingninjas.com using the different identifiers, it will produce the same as shown above, as a unique identifier is assigned to that particular domain.
Also see, How to Check Python Version in CMD
UUID 3 and UUID 5 to Create a Name-Based UUID
UUID 3 and UUID 5 are both used to generate name-based UUIDs, where the uniqueness of the UUID is determined by a combination of a namespace and a name. The main difference between them lies in the hashing algorithm used:
- UUID 3: Uses the MD5 hashing algorithm.
- UUID 5: Uses the SHA-1 hashing algorithm, which is considered more secure.
How Name-Based UUIDs Work?
A name-based UUID is generated by combining a namespace (which could be a domain, URL, or any identifier) with a specific name (a string, for example). The namespace ensures that the same name can produce different UUIDs in different contexts, while the name ensures that the same input consistently generates the same UUID.
Example of UUID 3 and UUID 5
Here’s an example using Python's uuid module to create UUID 3 and UUID 5:
import uuid
# Define a namespace and a name
namespace = uuid.NAMESPACE_DNS # Using DNS namespace
name = "example.com"
# Generate UUID 3
uuid3 = uuid.uuid3(namespace, name)
# Generate UUID 5
uuid5 = uuid.uuid5(namespace, name)
print("UUID 3:", uuid3)
print("UUID 5:", uuid5)
You can also try this code with Online Python Compiler
Run Code
Output
Running the above code will generate two UUIDs:
UUID 3: 5df41881-3aed-3515-88a7-2f4a814cf09e
UUID 5: 9073926b-929f-5f2c-8e4f-bb9f8eaf9c23
You can also try this code with Online Python Compiler
Run Code
Explanation
- Namespace: uuid.NAMESPACE_DNS is a predefined namespace for domain names.
- Name: "example.com" is the name we are using to generate the UUID.
- UUID 3: Generates a UUID using MD5 (uuid3).
- UUID 5: Generates a UUID using SHA-1 (uuid5).
How to convert string to UUID and UUID to string in Python?
In Python, you can easily convert a string representation of a UUID to a UUID object, and vice versa. This is useful when you need to work with UUIDs in different formats.
1. Converting a String to UUID
To convert a string to a UUID object, you can use the uuid.UUID() constructor. This is particularly useful when you receive a UUID in string format and need to perform operations on it as a UUID object.
Example:
import uuid
# A UUID in string format
uuid_string = "12345678-1234-5678-1234-567812345678"
# Convert the string to a UUID object
uuid_obj = uuid.UUID(uuid_string)
print("UUID Object:", uuid_obj)
You can also try this code with Online Python Compiler
Run Code
Output:
UUID Object: 12345678-1234-5678-1234-567812345678
You can also try this code with Online Python Compiler
Run Code
Explanation:
- uuid_string: A string representing the UUID.
- uuid.UUID(uuid_string): Converts the string to a UUID object.
2. Converting a UUID to String
To convert a UUID object back to its string representation, you can use the str() function. This is useful when you need to store or display the UUID as a string.
Example:
import uuid
# Create a UUID object
uuid_obj = uuid.uuid4() # Generates a random UUID
# Convert the UUID object to a string
uuid_string = str(uuid_obj)
print("UUID String:", uuid_string)
You can also try this code with Online Python Compiler
Run Code
Output:
UUID String: e.g., 85a25160-3c02-4a95-91b7-08b1e8e35773
You can also try this code with Online Python Compiler
Run Code
Explanation:
- uuid.uuid4(): Generates a random UUID object.
- str(uuid_obj): Converts the UUID object to a string.
Generate Reproducible UUIDs
To generate reproducible UUIDs, you can use UUID versions that rely on a specific input to ensure that the same input will always generate the same UUID. The most commonly used versions for this purpose are UUID 3 and UUID 5, which create name-based UUIDs using hashing algorithms.
UUID 3 and UUID 5
- UUID 3: Uses the MD5 hashing algorithm.
- UUID 5: Uses the SHA-1 hashing algorithm.
Both versions require a namespace and a name. The combination of these will always produce the same UUID if the namespace and name are unchanged.
Example
Here's how you can generate reproducible UUIDs using UUID 3 and UUID 5 in Python:
import uuid
# Define a namespace and a name
namespace = uuid.NAMESPACE_DNS # Using DNS namespace
name = "example.com"
# Generate UUID 3
uuid3 = uuid.uuid3(namespace, name)
print("UUID 3:", uuid3)
# Generate UUID 5
uuid5 = uuid.uuid5(namespace, name)
print("UUID 5:", uuid5)
You can also try this code with Online Python Compiler
Run Code
Output:
UUID 3: 6ba7b810-9dad-11d1-80b4-00c04fd430c8
UUID 5: 9073926b-929f-5f2c-8e4f-bb9f8eaf9c23
You can also try this code with Online Python Compiler
Run Code
Explanation:
- uuid.NAMESPACE_DNS: A predefined namespace for domain names.
- "example.com": The name used to generate the UUID.
- uuid.uuid3(namespace, name): Generates a UUID using MD5.
- uuid.uuid5(namespace, name): Generates a UUID using SHA-1.
How It Works
- Namespace: Provides context for the UUID, ensuring that the same name in different contexts (or namespaces) can produce different UUIDs.
- Name: The specific identifier or string used within the namespace to generate the UUID.
Advantages and Disadvantages of UUID
After discussing the structure and use case examples of Python UUID, let's see some advantages and disadvantages of Python UUID.
Advantages of UUID
Some of the advantages of Python UUIDs are as follows:
- The UUID depends on factors like date, time, MAC address, and name. This creates these IDs universally unique.
- The Python UUIDs are very easy to use.
- Python has a built-in module to generate UUIDs, so we don’t need to download any other packages. This built-in support is one of the significant advantages of Python uuid.
- There are different versions of UUIDs, from which users can select any according to their needs.
Disadvantages of UUID
Some of the disadvantages of Python UUIDs are as follows:
- Our MAC address is associated with the UUIDs we generate using uuid1(), so this can negatively impact in case of privacy and security issues.
- It takes up much more space than other identifiers.
- UUID is database dependent if generated using a database function.
- These IDs can only be understood with additional information, as these are not in human-readable form.
- Unlike other unique keys, the Python UUIDs are not generated in sequential order. These random generations create difficulties during the sorting of these IDs.
Related Article MySQL UUID and Django Model data types and fields list.
Frequently Asked Questions
What are the different versions of UUIDs?
Different types of UUIDs are versions 1,2,4,3, and 5. Version 1 generated uuid using date-time and MAC address, Version 2 also generates using the same components, but it is kept as reserved. Version 4 randomly generates UUIDs. Version 3 and 5 generates using hashing identifier with names.
What is the use of UUIDs?
The generation of UUIDs can be used in web applications and database systems. In web applications, uuid helps in managing by creating unique session keys. In the case of a database, it is used as a unique database key.
What are the hashing algorithms used by UUID versions 3 and 5?
The UUID version 3 uses the MD5 hashing algorithm. The MD5 algorithm stands for the message-digest algorithm. Whereas version 5 uses the Secure Hash Algorithm, also known as the SHA-1 algorithm. They generate UUIDs by hashing a namespace identifier and name.
Conclusion
We hope this article was insightful and you learned something new. In this blog, we learned about UUIDs which we can use to identify data in computer systems. We discussed its different versions and learned their syntaxes with the help of some examples. It also has a few disadvantages, which we discussed at the end.
If you want to learn more about UUID, do visit
Do visit our website to read more such blogs, refer to our Code360 platform.