I often use id () when writing temporary files to disk. This is a very easy way to get a pseudo random number.
Let's say that during data processing I come up with some intermediate results that I want to save for later use. I simply create the file name using the corresponding object identifier.
fileName = "temp_results_" + str(id(self)).
Although there are many other ways to create unique file names, this is my favorite. In CPython, an identifier is the memory address of an object. That way, if multiple objects are created, I will never have a name conflict. This is all for 1 search address. Other methods that I know for getting a unique string are much more intense.
A concrete example is a word processing application in which every open document is an object. I could periodically save progress to a disk with several open files using this naming convention.
John sallay
source share