Am new to python and I had to create an object only with certain attributes that are not None. Example:
if self.obj_one is None and self.obj_two is None:
return MyObj(name=name)
elif self.obj_one is not None and self.obj_two is None:
return MyObj(name=name, obj_one=self.obj_one)
elif self.obj_one is None and self.obj_two is not None:
return MyObj(name=name, obj_two=self.obj_two)
else:
return MyObj(name=name, obj_one=self.obj_one, obj_two=self.obj_two)
Coming from a Java land I know python is full of short hand so wanted to know if there is a cleaner way for writing the above? Of course my actual object has plenty more attributes. I tried searching but couldn't find anything helpful so am in doubt if its possible or not cause this doesn't scale if there are more than 2 variable attributes.