My first thought was that I'm "serializing" the complex object, but from what I understand that means I'm reducing it down to a string or binary format which could be passed over a network. While what I am actually doing is converting a complex object to more primitive types like list
, dict
, str
, int
, float
, etc. The context here is python, and I'm trying to rename my function to_dict(...)
to something which uses a more idiomatic/industry accepted term.
Currently I'm doing this:
class SomeComplexClass: ... my_complex_obj = SomeComplexClass(...) simple_obj = to_dict(my_complex_obj) simple_obj['foo'] = 'bar' return to_json(simple_obj)
As you see, I'll typically do some final adjustments to the object, then pass this to a library which knows how to convert this into a JSON string, which is passed over the network as HTTP response. This function is used 608 times in our code base.
Here to_dict(...)
is doing some heavy lifting, by recursively walking through the complex object and handling corner cases etc., it even supports multiple types of complex objects, even lists of objects.
It is this last part which makes me question the name to_dict(...)
, because I at some time extended it to support lists as the top level object type. It feels a little silly to do list_of_simple_objects = to_dict(my_list_of_complex_objects)
, because it then returns a list of dicts.
to_primitives
, ortransform_to_primitives
?SomeComplexClass
, or several types, which is not clear to me from this question. If it can convert almost any kind of object, a generic name liketo_dict
is fine. Otherwise, I would recommend to specify the source type (or category of types) in the name.nameOfBaseType_to_dict
? That should make it suffiently clear.entity_to_dict()
, orentity_to_primitives()
. Thanks.