mnowotka mnowotka - 5 months ago 13
Python Question

python dictionary and default values

Assuming

connectionDetails
is a Python dictionary, what's the best, most elegant, most "pythonic" way of refactoring code like this:

if "host" in connectionDetails:
host = connectionDetails["host"]
else:
host = someDefaultValue

Answer

Like this:

host = connectionDetails.get('host','someDefault')