`return` 单独等同于 Python 3.8 中的 `return(None)` 吗?
Is `return` alone equivalent to `return(None)` in Python 3.8?
我刚看到下面的Python代码,我对第一个return
有点困惑。默认是 return None
吗?是否等同于return(None)
?如果第一个 return
被执行,函数 inner()
会自动结束而第二个 return
被保留吗?
def smart_check(f):
def inner(a,b):
if b==0:
print("illegit: b =", b)
return # the first return
return(f(a,b))
return(inner)
@smart_check
def divide(a,b):
return(a/b)
Does it return None by default? Is it equivalent to return(None)
是,参见docs:如果存在表达式列表,则对其求值,否则None是
取代.
If the first return is executed, will the function inner()
automatically end there and the second return be left alone?
是
如果您不想 return 任何事情,您甚至可以完全删除 return 语句:
def smart_check(f):
def inner(a,b):
if b != 0:
return f(a,b)
print("illegit: b =", b)
return(inner)
由于 print 没有 return 任何东西,您甚至可以将此函数重写为
def smart_check(f):
def inner(a,b):
return f(a,b) if b!=0 else print("illegit: b =", b)
return(inner)
我刚看到下面的Python代码,我对第一个return
有点困惑。默认是 return None
吗?是否等同于return(None)
?如果第一个 return
被执行,函数 inner()
会自动结束而第二个 return
被保留吗?
def smart_check(f):
def inner(a,b):
if b==0:
print("illegit: b =", b)
return # the first return
return(f(a,b))
return(inner)
@smart_check
def divide(a,b):
return(a/b)
Does it return None by default? Is it equivalent to return(None)
是,参见docs:如果存在表达式列表,则对其求值,否则None是 取代.
If the first return is executed, will the function inner() automatically end there and the second return be left alone?
是
如果您不想 return 任何事情,您甚至可以完全删除 return 语句:
def smart_check(f):
def inner(a,b):
if b != 0:
return f(a,b)
print("illegit: b =", b)
return(inner)
由于 print 没有 return 任何东西,您甚至可以将此函数重写为
def smart_check(f):
def inner(a,b):
return f(a,b) if b!=0 else print("illegit: b =", b)
return(inner)