Introduction

A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems. UUID is also known as the term globally unique identifier (GUID). UUIDs are, for practical purposes, unique. Probability that a UUID will be duplicated is not zero, it is close enough to zero to be negligible. UUID values are 128 bits long. They are useful for identifiers for documents, hosts, application clients, and other use cases where a unique value is necessary.

UUID module in Python provides immutable UUID objects and the functions to generate universally unique identifiers UUID. They are generated as specified in RFC 4122.

Generating UUID

Python UUID module has following functions based on the various version of UUIDs.

  • uuid1(node=None, clock_seq=None) : Generate a UUID using host ID, sequence number and the current time. If optional parameter node is not given, it uses hardware address (MAC). If clock_seq is given, it is used as the sequence number; otherwise a random 14-bit sequence number is chosen.
  • uuid3(namespace, name) : Generate a UUID based on the MD5 hash of a namespace identifier (which is a UUID) and a name (which is a string).
  • uuid4(): Generate a random UUID.
  • uuid5(namespace, name) : Generate a UUID based on the SHA-1 hash of a namespace identifier (which is a UUID) and a name (which is a string).

Following example demonstrate various ways to generate UUID using above function.

# Generating unique identifiers
import uuid

# Generate UUID 1 
print(uuid.uuid1())

# Output
# 8197bce8-f334-11ea-ae2d-acde48001122

# UUID using an MD5 hash of a namespace UUID and a name
print(uuid.uuid3(uuid.NAMESPACE_DNS, 'mymusing.co'))

# Ouput
# 6e9ad278-ee7d-3c61-b228-c88591b7e324

# Random UUID
print(uuid.uuid4())

# Output
# 802a4181-ab23-412b-b916-4b4612ccde81

# UUID using a SHA-1 hash of a namespace UUID and a name
print(uuid.uuid5(uuid.NAMESPACE_DNS, 'mymusing.co'))

# Output
# bd0c36d0-572e-5a48-92c0-ff3d5c990eb5

# UUID from a string of hex digits
print(uuid.UUID('{06010203-0805-0507-0809-0a0b0c0d0e0f}'))

# Output
# 06010203-0805-0507-0809-0a0b0c0d0e0f

Converting UUID

uuid module has following function to convert UUID in other format

  • UUID.bytes : UUID as a 16-byte string in big-endian byte order.
  • UUID.bytes_le : UUID as a 16-byte string in little-endian byte order.
  • UUID.hex : UUID as a 32-character hexadecimal string.
  • UUID.int : UUID as a 128-bit integer.
  • UUID.urn : UUID as a URN as specified in RFC 4122.
import uuid

# Generate UUID using uuid4(), it uses underlying os.urandom() function
result = uuid.uuid4()
print(result)

# Output
# fc545e90-ccf8-43a9-b2c4-830601f0f41f

# Get raw 16 bytes of the UUID
print(result.bytes)

# Output
# b'\xfcT^\x90\xcc\xf8C\xa9\xb2\xc4\x83\x06\x01\xf0\xf4\x1f'

# Get raw 16 bytes of the UUID
print(result.bytes_le)

# Output
# b'\x90^T\xfc\xf8\xcc\xa9C\xb2\xc4\x83\x06\x01\xf0\xf4\x1f'

# UUID as hex
print(result.hex)

# Output
# fc545e90ccf843a9b2c4830601f0f41f

# UUID as integer
print(result.int)

# Output
# 335403525892708945939394893068589134879

# UUID as URN
print(result.urn)

# Output
# urn:uuid:fc545e90-ccf8-43a9-b2c4-830601f0f41f

# Convert UUID to a string of hex digits
print(str(result))

# Output
# fc545e90-ccf8-43a9-b2c4-830601f0f41f

Use UUID.version to get the version number (1 through 5, meaningful only when the variant is RFC_4122). UUID.is_safe indicates whether the platform generated the UUID in a multiprocessing-safe way.

import uuid

# Generate UUID using uuid5()
result = uuid.uuid5(uuid.NAMESPACE_DNS, 'mymusing.co')
print(result)

# Get version
print(result.version)

# Output
# 5

# Safe for multiprocessing.
print(result.is_safe)

# Output
# SafeUUID.unknown