Skip to main content
 首页 » 编程设计

python之Python 中没有到达返回语句的定义行为是什么

2025年05月04日9jpfss

给定以下 Python 代码:

def avg(a): 
  if len(a): 
    return sum(a) / len(a) 

a 的长度为零时 avg 的语言定义行为是什么,或者它的行为未被语言指定,因此不应在 Python 代码中依赖?

请您参考如下方法:

默认返回值为None。

来自documentation on Calls :

A call always returns some value, possibly None, unless it raises an exception. How this value is computed depends on the type of the callable object.