I have a function clans()
, that accepts 8 arguments. I need to add key and value pair to dictionary if argument is not None
. There is my solution:
def clans(self, name: str = None, warFrequency: str = None, location: str = None,
minMembers: int = None, maxMembers: int = None,
minClanPoints: int = None, minClanLevel: int = None,
labels: list[str] = None):
params = {} # <- the dictionary to add to
# very much if-conditions
if name is not None:
params['name'] = name
if warFrequency is not None:
params['warFrequency'] = warFrequency
... # <- other function arguments and if conditions
if labels is not None:
params['labels'] = labels
return params
I think it is not the most optimal solution due to the large amount of code and poor readability, how can I improve that code? I have an idea like (but I dont know how to realize it):
params = {}
for arg in args: # <- args is list of function arguments
if arg is not None:
params[arg.key()] = arg.value() # <- arg.key() is argument name, arg.value() is value