Python's uuid module provides immutable UUID objects and functions for generating various versions of UUIDs. UUID stands for Universally Unique Identifier, and it's used to generate unique IDs that can be utilized across different systems.
This function generates a UUID based on the host's MAC address and the current time.
import uuid
# Generate a UUID using uuid1()
result = uuid.uuid1()
print(result)
35c8204e-1d15-11ea-a5f1-0800200c9a66
Using the uuid3()
method, a UUID can be generated based on the MD5 hash of a namespace and name.
namespace = uuid.NAMESPACE_DNS
name = "example.com"
# Generate a UUID using uuid3()
result = uuid.uuid3(namespace, name)
print(result)
6fa459ea-ee8a-3ca4-894e-db77e160355e
The uuid4()
method creates a random UUID, as shown below.
# Generate a UUID using uuid4()
result = uuid.uuid4()
print(result)
f50ec0b7-f960-400d-91f0-c42a6d44e3d7
Similar to uuid3()
, the uuid5()
method generates a UUID based on the SHA-1 hash of a namespace and name.
namespace = uuid.NAMESPACE_DNS
name = "example.com"
# Generate a UUID using uuid5()
result = uuid.uuid5(namespace, name)
print(result)
c74a196f-f19d-5ea9-bffd-a2742432fc9c
Universally Unique Identifiers are crucial for ensuring the uniqueness of objects across systems. The Python uuid module provides a straightforward way to generate different types of UUIDs using functions like uuid1()
, uuid3()
, uuid4()
, and uuid5()
. The use of UUIDs is widespread and beneficial for various applications and services.
str()
function to convert a UUID object into a string.CloneCoding
Innovation Starts with a Single Line of Code!