Reputation: 28437
When creating an API in python, I'm not sure what approach to take in creating an instance of a basic object with init.
When creating an instance of the object in the API, the user will most likely want the object to be one of two things:
Is there an accepted design pattern for this? Should init just create an empty object, and then the consumer must call a get or set method? Or maybe init should take an argument as to if this is a get or set? Or lastly, should get or set be inferred from the arguments to init?
Upvotes: 4
Views: 143
Reputation: 47357
I can't think of a design pattern to match exactly what you are describing, since:
How about overloading the API to the following?
When invoked with no arguments:
custom_API()
then just create a new basic object with some reasonable default values and let the consumer use get / set to configure it.
When invoked with a lookup key of some sort:
custom_API("abc")
then return the corresponding object, or a list of objects of the lookup key is vague, or None
if no matching object can be found.
Upvotes: 2