,但是从我收集的内容来看,每个功能调用都是在此编码块中所必需的。我会错过什么?
Python函数默认情况下隐式返回return
。以下功能定义等效。
None
完全没有语句 - 这表明您在调用函数时不应将名称分配给返回值,OR
阐明def foo():
pass
def foo():
return
def foo():
return None
,指示可以返回有意义值的函数的“无结果”,仅使用
return
return None
return
,您是不应该发行的def switch_first_last_in_place(lst):
'switch the first and last elements of a list in-place'
lst[0], lst[-1] = lst[-1], lst[0]
示例情况2:None
该功能明确返回
result = switch_first_last_in_place([1, 2, 3])
未找到用户。作业喜欢
def get_user_info_from_database(username):
'fetches user info, returns None if user does not exist'
if user_exist(username):
return query_db(username)
else:
return None
期望。部分
None
不必要的,但我喜欢在
result = get_user_info_from_database('Bob')
是有意义的回报值的情况下明确说明。
示例3:else:
return None
,
None
仅用于脱离功能。
我不喜欢示例函数末尾的裸露。它不用于结束执行,这些功能无法返回其他值。我会删除它。
您误会了。警告不是谈论您正在调用的任何功能。它指的是您对
def assert_exists(thing, *containers):
'raise AssertionError if thing cannot be found in any container'
for container in containers:
if thing in container:
return
raise AssertionError
关键字的使用。
这个功能:
return
return
等于此功能:
return
def print_hello():
print("Hello")
return