Skip to content

singleton

This is a singleton metaclass that can be used to cache and re-use existing objects.

In the Iceberg codebase we have a lot of objects that are stateless (for example Types such as StringType, BooleanType etc). FixedTypes have arguments (eg. Fixed[22]) that we also make part of the key when caching the newly created object.

The Singleton uses a metaclass which essentially defines a new type. When the Type gets created, it will first evaluate the __call__ method with all the arguments. If we already initialized a class earlier, we'll just return it.

More information on metaclasses: https://docs.python.org/3/reference/datamodel.html#metaclasses