在 Python 中返回 NoneType 的函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19530528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Function Returning a NoneType in Python?
提问by BLU
Working on a Python project for CS1, and I have come accross a strange issue that neither I or my roomate can figure out. The general scope of the code is to fill in a grid of 0s with shapes of a certain size using numbers to fill the space, and we have to check along the way to make sure we arent putting shapes in places there there are already shapes. I have two functions here, both do virtually the same thing, but for whatever reason when falsechecker returns the list it returns it as a NoneType. Why is this happening?
在为 CS1 开发 Python 项目时,我遇到了一个我或我的室友都无法弄清楚的奇怪问题。代码的一般范围是使用数字填充具有特定大小形状的 0 网格来填充空间,并且我们必须沿途检查以确保我们没有将形状放在已经有形状的地方。我在这里有两个函数,它们几乎都做同样的事情,但是无论出于何种原因,当 falsechecker 返回列表时,它都会将其作为 NoneType 返回。为什么会这样?
def falseChecker(binList, r, c, size):
sCheck = isSpaceFree(binList, r, c, size)
if sCheck == True:
for x in range(c, c+size):
for y in range(r, r+size):
binList[x][y] = size
return binList
else:
c += 1
if c > len(binList):
c = 0
r += 1
if r > len(binList):
return binList
falseChecker(binList, r, c, size)
def iChecker(binList, blockList):
r = 0
c = 0
for i in blockList:
check = isSpaceFree(binList, r, c, i)
if check == True:
for x in range(c, c+i):
for y in range(r, r+i):
binList[x][y] = i
c += 1
if c > len(binList):
c = 0
r += 1
if r > len(binList):
return binList
else:
binList = falseChecker(binList, r, c, i)
return binList
main()
采纳答案by abarnert
In the case where sCheck == True
is false, you don't return
anything. And in Python, a function that doesn't explicitly return
anything returns None
.
在 wheresCheck == True
为 false的情况下,您什么都不做return
。而在 Python 中,一个没有显式return
返回任何内容的函数会返回None
.
If you were trying to recursively call yourself and return the result, you wanted this:
如果您试图递归调用自己并返回结果,您需要这样:
return falseChecker(binList, r, c, size)
回答by lvc
The recursive line:
递归行:
falseChecker(binList, r, c, size)
needs to be
需要是
return falseChecker(binList, r, c, size)
or the recursed function ends, and the outer function keeps goingsince it hasn't returned yet. It then finishes without returning, and so returns None
.
或者递归函数结束,外部函数继续运行,因为它还没有返回。然后它完成而不返回,因此返回None
。
回答by lvc
You need a return
at the end of falseChecker
:
你需要一个return
在结尾falseChecker
:
def falseChecker(binList, r, c, size):
sCheck = isSpaceFree(binList, r, c, size)
if sCheck == True:
for x in range(c, c+size):
for y in range(r, r+size):
binList[x][y] = size
return binList
else:
c += 1
if c > len(binList):
c = 0
r += 1
if r > len(binList):
return binList
#################################
return falseChecker(binList, r, c, size)
#################################
In Python, functions return None
by default if they come to the end of themselves without returning. Furthermore, when falseChecker
is run for the first time, if sCheck
is False
, then it will execute the else
block. This code block doesn't contain a return
. Because of this, the ultimate return value of falseChecker
will be None
.
在 Python 中,None
如果函数在没有返回的情况下到达自己的末尾,则默认情况下返回。此外,当falseChecker
第一次运行时,如果sCheck
是False
,那么它将执行该else
块。此代码块不包含return
. 因此,falseChecker
will的最终返回值是None
。