zds*_*_cn 16 python python-3.x
鉴于此示例函数:
def writeFile(listLine,fileName):
'''put a list of str-line into a file named fileName'''
with open(fileName,'a',encoding = 'utf-8') as f:
for line in listLine:
f.writelines(line+'\r\n')
return True
Run Code Online (Sandbox Code Playgroud)
这个return True
陈述有用吗?
它和没有它有什么区别?如果没有返回功能会发生什么?
Tra*_*ear 28
如果函数未指定返回值,则返回None
.
在if/then条件语句中,None
求值为False.所以理论上你可以检查这个函数的返回值是否成功/失败.我说"理论上"因为对于这个问题中的代码,函数不会捕获或处理异常,并且可能需要额外的强化.