I have a dictionary which is going to be passed to multiple functions. In each of the functions, I need to extract all the values of the dictionary.
For example,
def foo(mas_details):
# Extract mas details
if 'ip' in mas_details.keys():
mas_ip = mas_details['ip']
else:
logger_object.critical("MAS IP not provided!")
return False
if 'username' in mas_details.keys():
mas_username = mas_details['username']
else:
mas_username = 'root'
if 'password' in mas_details.keys():
mas_password = mas_details['password']
else:
mas_password = 'root'
if 'protocol' in mas_details.keys():
mas_protocol = mas_details['protocol']
else:
mas_protocol = "http"
if 'timeout' in mas_details.keys():
mas_timeout = mas_details['timeout']
else:
mas_timeout = 120
I have about 15 functions where I will be passing this mas_details dictionary. However, extracting the values in each function makes the code repetitive.
I want to put the extraction of the values into a function in itself. However, if I do that, the variables won't be accessible in the parent functions, unless I make all the variables global.
What is the best way of going about this?
returnthe extracted values as a tuple?