# Is there a library for recursive object creation using config objects?

**URL:** <https://discuss.python.org/t/is-there-a-library-for-recursive-object-creation-using-config-objects/8522>\
**Category:** Python Help\
**Created:** [May 3, 2021, 6:32am UTC](https://discuss.python.org/t/is-there-a-library-for-recursive-object-creation-using-config-objects/8522 "2021-05-03T06:32:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johsmi9933](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@johsmi9933](https://discuss.python.org/u/johsmi9933)\
**Post date:** [May 3, 2021, 6:32am UTC](https://discuss.python.org/t/is-there-a-library-for-recursive-object-creation-using-config-objects/8522/1 "2021-05-03T06:32:23Z")

</div>

I would like to be able to create objects of classes which inherit from something like `Configurable` through a factory method in addition to the normal constructor, where the factory method takes some kind of config object which is limited to JSON-serializable data types.

So in addition to `Class1(a=1, b="x")` it should be possible to also do `Class1.from_config(dict(a=1, b="x"))`.

This should work recursively so if the value of some initialization parameter is a configurable object it should get created from a nested config object through the `from_config` method, i.e. in addition to `Class1(c=Class2())` it should be possible to do `Class`.from\_config(dict(c=dict())`

Each configurable object should have a method to return the config object that can be used to create a clone, e.g. ` myobject.get_config()`.

My feeling is that this is such a basic approach that there should be many libraries already, but I could not really find one – could you point me to the right places?

The reason why I would like to find (or implement) this is because I would like to use this to duplicate my classes in other processes that way: send the config obejct of possibly nested complex classes to the other process to create a “twin” object to use there. One deeper reason for this is because that mechanism could then get extended to automatically handle the sharing of large datastructures when creating and using the config object to recreate twin objects in other processes.

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [May 3, 2021, 10:01pm UTC](https://discuss.python.org/t/is-there-a-library-for-recursive-object-creation-using-config-objects/8522/2 "2021-05-03T22:01:45Z")

</div>

We’ve been using factory-boy: [factory-boy · PyPI](https://pypi.org/project/factory-boy/)

Pretty good indeed.

Cheers,  
Cameron Simpson [cs@cskk.id.au](mailto:cs@cskk.id.au)

---

<div class="post-metadata">

**Author:** ![agoose77](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/agoose77/32/2845_2.png) [@agoose77](https://discuss.python.org/u/agoose77)\
**Post date:** [May 10, 2021, 7:37am UTC](https://discuss.python.org/t/is-there-a-library-for-recursive-object-creation-using-config-objects/8522/3 "2021-05-10T07:37:18Z")

</div>

There is also the [traitlets](https://traitlets.readthedocs.io/) library which is used by the Jupyter stack to provide a typed configuration system (as well as event listeners).

---

<div class="post-metadata">

**Author:** ![johsmi9933](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@johsmi9933](https://discuss.python.org/u/johsmi9933)\
**Post date:** [May 13, 2021, 8:21am UTC](https://discuss.python.org/t/is-there-a-library-for-recursive-object-creation-using-config-objects/8522/4 "2021-05-13T08:21:52Z")

</div>

Thanks, this seems to do part of what I am after (and then a lot more).
