python中or语句的妙用,一行代替if/elif/else多行代码
程序员文章站
2024-01-06 13:41:28
...
import os
args = {'search_engine':'baidu'}
if args['search_engine']:
search_engine = args['search_engine']
elif os.getenv('HOWDOI_SEARCH_ENGINE'):
search_engine = os.getenv('HOWDOI_SEARCH_ENGINE')
else:
search_engine = 'bing'
print(search_engine)
import os
search_engine = args['search_engine'] or os.getenv('HOWDOI_SEARCH_ENGINE') or 'bing'
print(search_engine)
机制:第一个不存在则判断第二个是否存在,若第二个不存在则判读第三个是否存在