Skip to content

Instantly share code, notes, and snippets.

View DocGarbanzo's full-sized avatar

DocGarbanzo DocGarbanzo

  • London
  • 06:21 (UTC -12:00)
View GitHub Profile
@DocGarbanzo
DocGarbanzo / README.md
Last active January 30, 2022 21:36
Python factory method - creating objects from dictionaries

Python factory method

This is a simple approach for a factory that makes objects out of dictionaries. The only two ingredients are

  • a metaclass Factory to manage the class registration and export a make function
  • a base class Creatable that implements the creation funtion by calling the initializer

The user only has to derive classes from the base class Creatable and then has access to a dictionary-based creation of instances of such classes.

Please note

In the file where Factory.make() is called, it is necessary to import the Creatable concrete class otherwise the metaclass initialisation will not run.