假设 connectionDetails
是一个 Python 字典,那么像这样重构代码的最好、最优雅、最“pythonic”的方式是什么?
if "host" in connectionDetails:
host = connectionDetails["host"]
else:
host = someDefaultValue
最佳答案
像这样:
host = connectionDetails.get('host', someDefaultValue)
关于python - 字典和默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9358983/