I have a script which uses a required parameter and some optional. What would be the best way to call a function with these optional parameters?
Right now I have these if
, but I'm pretty sure there must be a better way to handle this situation. Otherwise, what would I do if instead of 4 optional parameters they are 100 (bad practice, just for the example)? Use an if
for each one?
def get_data(table, db='postgres', host='localhost', user='postgres', password=''):
....
....
....
kwargs = {}
if args.db:
kwargs['db'] = args.db
if args.host:
kwargs['host'] = args.host
if args.user:
kwargs['user'] = args.user
if args.password:
kwargs['password'] = args.password
data = get_data(args.table, **kwargs)