What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

UUID in Python: How to Generate random IDs? (with code)

  • Apr 11, 2023
  • 6 Minute Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Kusum Jain
UUID in Python: How to Generate random IDs? (with code)

When dealing with data, we use UUID in Python for making random IDs to easily identify a field. Let's learn how to generate UUID with an example, its various functions, and its advantages.

What is UUID in Python?

Generally, when coders work with a certain database, it’s a standard procedure to employ a sort of unique ID field that can provide each row of a table, with a special kind of identifier.

For example, consider a database for customers. Here, multiple fields like name, address, etc cannot be used as a unique identifier because two people can have the same name, same address, and more. This is where a unique identifier comes into play. With the help of a special identifier, identifying a field would be much more simpler and convenient.

UUID stands for Universally Unique Identifier. In Python, UUID is a 128-character string of alphanumeric variable type, that uniquely identifies an object, entity, or resource in both space and time of a table. The UUID module offers the ability to produce distinctive IDs in accordance with the RFC 4122 definition.

Is UUID inbuilt into Python? Yes, UUID is an inbuilt module and doesn't require any third-party libraries for installation. The module has a UUID class used to generate UUIDs of different versions: versions 1, 3, 4, and 5. 

Is UUID really unique? The chances of generating two identical UUIDs in Python are extremely low, making it highly unlikely that two different entities will have the same UUID. This is because these are generated using a combination of time, machine information, and some random data.

How to generate UUID in Python?

UUID Python generator module is implemented in accordance with RFC 4122, which includes all of the data and algorithms needed to produce all versions of the UUID. To generate a UUID in python, one can use the ‘uuid’ module that comes with a standard library. Let’s look at the steps to generate the same:

  1. Import the UUID module
  2. Use one of the functions in the uuid module to generate a UUID.
  3. The function uuid.uuid1() creates a UUID by utilizing the computer's MAC address and the current time.
  4. Creates a random UUID using uuid.uuid4().
  5. Creates a UUID based on a namespace and a name using the function uuid.uuid5(namespace, name).
  6. Use the UUID which was created in the given module and print it out to see the output. 

Here is the code for this purpose:

import uuid
random_uuid = uuid.uuid4()
print
(random_uuid)

 

Here, the output will be in the format ‘xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx’ where every x variable is considered as a hexadecimal digit.

Example of UUID generation

Python's UUID module must first be imported before it can be used. Here's an illustration:

import uuid

# generate a random UUID

random_uuid = uuid.uuid4()

# generate a UUID based on a namespace and a name

namespace = uuid.NAMESPACE_URL

name = 'https://example.com'

uuid_from_name = uuid.uuid5(namespace, name)

# print the UUIDs

print(random_uuid)

print(uuid_from_name)

 

Output:

9c773958-78b2-4b4a-bbee-1f0e70026cf9
4fd35a71-71ef-5a55-a9d9-aa75c889a6d0

 

Two UUIDs: one created at random and the other using the namespace and name provided—will be the generated output.

Various functions of the UUID module

UUID1(): Here, the MAC address and current time component are used to create a unique number that identifies a field in the table uniquely. 

import uuid

# Generate a UUID1

uuid1 = uuid.uuid1()

# Print the UUID1

print(uuid1)

 

Output:

5a91c607-c0e9-11ed-af63-8cc6818b4b8c

 

UUID3(): This UUID-based module generates a unique identifier based on a namespace and time. UUID 3 utilizes MD5 hashing.

import uuid

# Generate a UUID3 for a namespace and name

namespace = uuid.uuid3(uuid.NAMESPACE_DNS, 'example.com')

uuid3 = uuid.uuid3(namespace, 'some string')

# Print the UUID3

print(uuid3)

 

Output:

387d0c39-f671-333b-b6ea-33585a0bb24e

 

UUID4(): For safety purposes, this kind of UUID is the wisest option. Here, a pseudo-random number generator is used and thus, a random UUID is used in the databases. 

import uuid

# Generate a UUID4

uuid4 = uuid.uuid4()

# Print the UUID4

print(uuid4)

 

Output:

07d22f38-7561-464c-a461-0665f2da1a5b

 

UUID5(): To generate UUID, it employs cryptographic hashes and text strings supplied by the application. UUID 5 employs SHA-1 hashing. 

import uuid

# Generate a UUID5 for a namespace and name

namespace = uuid.uuid5(uuid.NAMESPACE_DNS, 'example.com')

uuid5 = uuid.uuid5(namespace, 'some string')

# Print the UUID5

print(uuid5)

 

Output:

b1267b47-9e39-5ae8-92bd-668a30547a35

 

Why use UUID instead of ID?

When talking about back-end development, an ID is simply a unique identifier for a data record. For instance, a web user will have a unique ID of their own. Moreover, if the website is a social media platform, then each post or update will also contain a unique ID of its own.

But a UUID is a special ID that uniquely identifies a record in the database universally. It cannot be the same and is not irreplaceable as well. No other ID can be found similar to a UUID. Additionally, using a UUID reduces the chances of future ID issues and eases the development of the architecture of any technology much easier.

The chances of the same identifier in UUID is zero, so there is no tension of an identifier being copied and it’s easy to locate files in the databases. 

Advantages of Python UUID Module

There are plenty of advantages for UUID module generators in databases, addressing, hashing, etc. Let’s look at a few of them. 

  1. Using UUID, guessing is a question with an answer out of the box. Since each ID produced is distinct and unique, it is difficult to guess them.
  2. When generating entities in the dataset, if auto-incrementing numbers are being used to create special identifiers, these objects must be saved right away before being returned so they're able to retain unique IDs. When using UUIDS, since the ids are already known before being saved to the server, they can be returned before being saved to the database.
  3. Using UUID, there is an easy distribution of databases. Across multiple servers, data can be sent, received, updated, and more, with the help of UUID. 
  4. Merging of records becomes less hectic. UUID provides a unique identifier, which makes accessing various databases more convenient. Since accessing becomes easy, merging is a piece of cake. Thus, UUID makes the merging of records easier and more reliable. 

Conclusion

In the end, all UUIDs are IDs but not all IDs are UUIDs. UUIDs in Python can be extremely beneficial if used wisely and for storing information in the right manner. Now you also know how to generate them and what are its advantages.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Kusum Jain
I am an avid coder on CodeChef, I partake in hackathons regularly. I am an independent and self-motivated student with a love for coding and experiencing new things.