Home >Backend Development >Python Tutorial >New Title: Python Quick Tip: How to Create a Globally Unique Identifier in Python
Before going on to describe how to create a Universally Unique Identifier (UUID) using Python, one may ask, What exactly is a UUID? So, let’s first define what UUID means and why to use it.
What is UUID?uniquely identifies an object (data). The number consists of 32 hexadecimal digits displayed in groups of five separated by hyphens. To see what a UUID looks like, see the following example:
022db29c-d0e2-11e5-bb4c-60f81dca7676The purpose of using such identifiers is to guarantee that we do not have similar identifiers, or at least guarantee that we have an identifier that is different from any UUID generated before 3400 AD!
This uniqueness is achieved by combining different components together, which will guarantee that the UUID is different. The identifier in this case will consist of the machine's network address, a timestamp, and a randomly generated component.
Thus, we can say that UUID is considered an algorithm for creating a unique string in a specific format that is composed of different components to ensure the uniqueness of the identifier.
UUID in Python
This module provides immutable
UUIDSo, guess what? As we learned inobjects (
UUIDclasses) and functions
uuid1(),
uuid3(),
uuid4(),
u uid5()are used to generate version 1, 3, 4 and 5 UUIDs specified in
RFC 4122.
Fluid Review Series, Python is all about making things simple, and creating UUIDs is no exception. for you!
import uuid print uuid.uuid4()Yes, that’s it! That's all you need to generate UUIDs in Python. On my machine I get the following output:
b77eafed-69ab-422d-8448-1ec1f0a2eb8cHow many hexadecimal digits can you see?
Don’t you think Python is trying to make our lives easier? !
The above is the detailed content of New Title: Python Quick Tip: How to Create a Globally Unique Identifier in Python. For more information, please follow other related articles on the PHP Chinese website!