Creational patterns deal with the object instantiation mechanism. Such a pattern might define a way for object instances to be created or even how classes are constructed.
These are very important patterns in compiled languages such as C or C++, since it is harder to generate types on demand at runtime.
But creating new types at runtime is pretty straightforward in Python. The built-in type function lets you define a new type object by code:
>>> MyType = type('MyType', (object,), {'a': 1}) >>> ob = MyType() >>> type(ob) <class '__main__.MyType'> >>> ob.a 1 >>> isinstance(ob, object) True
Classes and types are built-in factories. We have already dealt with the creation of new class objects, and you can interact with class and object generation using metaclasses. These features are the basics to implement the factory design pattern, but we won't describe it further in this section because we extensively covered the topic of class and object creation in Chapter 3, Modern Syntax Elements - Below the Class Level.
Besides factory, the only other creational design pattern from the GoF that is interesting to describe in Python is singleton.