Managing global and singleton objects
The Python environment contains a number of implicit global objects. These objects provide a convenient way to work with a collection of other objects. Because the collection is implicit, we're saved from the annoyance of explicit initialization code.
One example of this is an implicit random number generating object that's part of the random
module. When we evaluate random.random()
, we're actually making use of an instance of the random.Random
class that's an implicit part of the random
module.
Other examples of this include the following:
- The collection of data encode/decode methods (
codecs
) available. Thecodecs
module lists the available encoders and decoders. This also involves an implicit registry. We can add encodings and decodings to this registry. - The
webbrowser
module has a registry of known browsers. For the most part, the operating system default browser is the one preferred by the user...