mnowotka mnowotka - 1 year ago 136
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 Source

Like this:

host = connectionDetails.get('host','someDefault')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download