如何设置输出的优先级
1.应该充分利用'if return
。
放在前面的if 优先级高,因为一旦满足某个if
,说明,后面的if 已经不会执行,直接return
。
2.name for name in api_names if name not in deprecated_api_names
3.下面代码参考的是tensorflow的代码
def get_canonical_name(api_names, deprecated_api_names):
"""
Get preferred endpoint name.
Args:
api_names:API names iterable.
deprecated_api_names: Deprecated API names preference.
Returns:
Returns one of the following in decreasing preference.按降低的优先级返回
-first non-deprecated endpoint
-first endpoint
-None
"""
non_deprecated_name = next((name for name in api_names if name not in deprecated_api_names),
None)
if non_deprecated_name:
return non_deprecated_name
if api_names:
return api_names[0]
return None
实验
a = [1,2,3,4,5]
b = [4,5,6,7,8]
print(name for name in a if name not in b)
c = (name for name in a if name not in b)
print("the type of c:",type(c))
print("the output of next() function:",next(c,None))