I am writing a script that polls a repository store (file system or such) for incoming entities (user input files), which it then processes by parsing and then persists them in the database. Pretty standard stuff. However, I am somewhat constrained by Python's limitation to a single constructor (__init__
) per class because I would like to instantiate new objects in two modes:
- New -- not yet persisted in the DB, which happens as I process an incoming new entity, which involved many arguments, and
- Existing -- persisted in the DB, retrieved by the PK or another set of uniquely identifying attributes
So, currently, and I would like to change that, I have a big constructor that covers both modes of object instancing and within it, I have logic to determine which of the two modes of creation to use. I hate it, to say the least.
My question is how to elegantly split these two modes of creation so I am looking for a design suggestion. Python is my secondary language, I come from Java, which supports multiple class constructors and also has better ORM
frameworks.
For example, should I limit the __init__
to just deal with new object creation while have something like a static factory method
function that takes the PK parameter for pulling the data from the DB and returning the object?
Aucun commentaire:
Enregistrer un commentaire