Python return语句之前的Print语句给出了正确的值,但函数返回的是Nonetype
我有一个对堆栈排序的函数的实现。函数sortStack正确地对堆栈进行排序,但当我打印堆栈时,它似乎在修改初始参数newStack2。为什么return语句返回None?它是否与按引用传递/别名有关Python return语句之前的Print语句给出了正确的值,但函数返回的是Nonetype,python,return,parameter-passing,pass-by-reference,alias,Python,Return,Parameter Passing,Pass By Reference,Alias,我有一个对堆栈排序的函数的实现。函数sortStack正确地对堆栈进行排序,但当我打印堆栈时,它似乎在修改初始参数newStack2。为什么return语句返回None?它是否与按引用传递/别名有关 class Stack(): def __init__(self): self.stack = [] def push(self,val): if len(self.stack)>=0: self.stack.appen
class Stack():
def __init__(self):
self.stack = []
def push(self,val):
if len(self.stack)>=0:
self.stack.append(val)
else:
return None
def popOut(self):
if len(self.stack) > 0:
item = self.stack[-1]
self.stack.pop()
return item
else:
return None
def peek(self):
if self.isEmpty() == True:
return None
else:
return self.stack[-1]
def isEmpty(self):
if len(self.stack) == 0:
return True
else:
return False
def sortStack(stack1,stack2):
if stack1.isEmpty() is False:
if stack2.isEmpty() is True:
item = stack1.popOut()
stack2.push(item)
sortStack(stack1,stack2)
elif stack1.peek() < stack2.peek():
item = stack1.popOut()
stack2.push(item)
sortStack(stack1,stack2)
else:
item = stack1.popOut()
stack1.push(stack2.popOut())
stack2.push(item)
sortStack(stack1,stack2)
else:
stack1 = stack2
return stack1
if __name__ == '__main__':
newStack = Stack()
newStack.push(5)
newStack.push(4)
newStack.push(6)
newStack2 = Stack()
print(sortStack(newStack,newStack2))
print(newStack2.stack)
插入一点基本的跟踪工具: 缩进=“” def sortStack(堆栈1、堆栈2): 全局缩进 打印(缩进+输入sortStack)、stack1.stack、stack2.stack) 缩进+=“” 这就是你的问题:只有你最里面的调用返回任何值;其余的人扔掉了最里面的电话,把它传回到电话线上。这将返回
None
修理 将预期返回值放入新变量中;在任何出口处归还 缩进=“” def sortStack(堆栈1、堆栈2): 全局缩进 打印(缩进+输入sortStack)、stack1.stack、stack2.stack) 缩进+=“”
Python从不使用pass-by-reference作为求值策略,因此不,它与此无关。无论如何,如果进入外部
else
子句,您只会在sortStack
中返回任何内容。这就是为什么它返回None
@juanpa.arrivillaga yes,但该分支——表面上是最终操作——返回stack1
,而不是递归调用返回的None
值。其思想是排序后的列表作为stack2
保持完整,然后分配给stack1
,并返回;我只是跟踪了执行情况。那个箱子是最里面的,不是最后一次退货。
None
[6, 5, 4]
if stack1.isEmpty() is False:
if stack2.isEmpty() is True:
item = stack1.popOut()
stack2.push(item)
sortStack(stack1,stack2)
print(indent + "LEAVE sortStack case A")
indent = indent[2:]
elif stack1.peek() < stack2.peek():
item = stack1.popOut()
stack2.push(item)
sortStack(stack1,stack2)
print(indent + "LEAVE sortStack case B")
indent = indent[2:]
else:
item = stack1.popOut()
stack1.push(stack2.popOut())
stack2.push(item)
sortStack(stack1,stack2)
print(indent + "LEAVE sortStack case C")
indent = indent[2:]
else:
print(indent + " sortStack at bottom", stack2.stack)
stack1 = stack2
print(indent + "LEAVE sortStack at bottom", stack1.stack)
indent = indent[2:]
return stack1
ENTER sortStack [5, 4, 6] []
ENTER sortStack [5, 4] [6]
ENTER sortStack [5] [6, 4]
ENTER sortStack [4] [6, 5]
ENTER sortStack [] [6, 5, 4]
sortStack at bottom [6, 5, 4]
LEAVE sortStack at bottom [6, 5, 4]
LEAVE sortStack case B
LEAVE sortStack case C
LEAVE sortStack case B
LEAVE sortStack case A
returned None
[6, 5, 4]
if stack1.isEmpty() is False:
if stack2.isEmpty() is True:
item = stack1.popOut()
stack2.push(item)
result = sortStack(stack1,stack2)
print(indent + "LEAVE sortStack case A")
indent = indent[2:]
elif stack1.peek() < stack2.peek():
item = stack1.popOut()
stack2.push(item)
result = sortStack(stack1,stack2)
print(indent + "LEAVE sortStack case B")
indent = indent[2:]
else:
item = stack1.popOut()
stack1.push(stack2.popOut())
stack2.push(item)
result = sortStack(stack1,stack2)
print(indent + "LEAVE sortStack case C")
indent = indent[2:]
else:
result = stack2
print(indent + "LEAVE sortStack at bottom", result.stack)
indent = indent[2:]
return result
ENTER sortStack [5, 4, 6] []
ENTER sortStack [5, 4] [6]
ENTER sortStack [5] [6, 4]
ENTER sortStack [4] [6, 5]
ENTER sortStack [] [6, 5, 4]
LEAVE sortStack at bottom [6, 5, 4]
LEAVE sortStack case B
LEAVE sortStack case C
LEAVE sortStack case B
LEAVE sortStack case A
returned [6, 5, 4]
[6, 5, 4]