【发布时间】:2020-09-18 08:07:27
【问题描述】:
基本上,我有 3 个独立的功能。我想先运行geopy_parse,如果它返回None,那么我将运行libpostal_parse,如果返回None,我将最后运行最后一个函数google_maps_parse。函数的最终返回应该是最后运行的子函数的结果。我已经创建了一个函数来完成这个,但我想以一种更简洁、更 Pythonic 的方式重写它。
代码示例:
def parse_address(address_str):
# if no address string provided, return None result
if not address_str:
return (None, None, None)
# execute geopy parser
res = geopy_parse(address_str)
if not res:
res = libpostal_parse(address_str)
if not res:
res = google_maps_parse(address_str)
return {
'nation': res['country'],
'state': res['state'],
'city': res['city'] or res['town'] or res['county']
}
【问题讨论】:
标签: python function dictionary if-statement