本文介绍了在python中,如果一个函数没有return语句,它返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于此示例函数:

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

这个 return True 语句有什么用吗?

Does this return True statement do anything useful?

有它和没有它有什么区别?如果没有返回函数会怎样?

What's the difference between with it and without it? What would happen if there were no return function?

推荐答案

如果函数没有指定返回值,则返回 None.

If a function doesn't specify a return value, it returns None.

在 if/then 条件语句中,None 的计算结果为 False.所以理论上你可以检查这个函数的返回值是否成功/失败.我说理论上"是因为对于这个问题中的代码,该函数不会捕获或处理异常,并且可能需要额外的强化.

In an if/then conditional statement, None evaluates to False. So in theory you could check the return value of this function for success/failure. I say "in theory" because for the code in this question, the function does not catch or handle exceptions and may require additional hardening.

这篇关于在python中,如果一个函数没有return语句,它返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 09:47