I have a function with lot of variables passed into it and I need to create an object of it. How can I simplify this code?
obj_in = dict()
if foo:
obj_in['foo'] = foo
if bar:
obj_in['bar'] = bar
if john:
obj_in['john'] = john
UPD: My current idea is:
fields = ['foo', 'bar', 'john']
obj_in = dict()
for field in fields:
val = locals()[field]
if val:
enrich_dict(field, val, obj_in)
def enrich_dict(field: str, val, obj_in: dict):
obj_in[field] = val
But not really sure of using locals.