我有一个功能:
def turn(self, keyEvent):
if (keyEvent.key == pygame.locals.K_UP) and \
(self.body[0].direction != Directions.DOWN):
self._pivotPoints.append(PivotPoint(self.body[0].location, \
Directions.UP))
print("Placing pivot point up")
#elif chain for the down left and right button presses omitted
#code is the same for each input
创建以下类的实例:
class PivotPoint:
def __init__(self, location, \
direction):
"""When a body part reaches a pivot point, it changes directions"""
pdb.set_trace()
self.location = location
self.direction = direction
当我运行此代码时,pdb启动,我得到以下I / O序列:
> /home/ryan/Snake/snake.py(50)__init__()
-> self.location = location
(Pdb) step
> /home/ryan/Snake/snake.py(51)__init__()
-> self.direction = direction
(Pdb) step
--Return--
> /home/ryan/Snake/snake.py(51)__init__()->None
-> self.direction = direction
(Pdb) step
> /home/ryan/Snake/snake.py(89)turn()
-> print("Placing pivot point right")
第51行的陈述正在执行两次。为什么会这样?
答案 0 :(得分:3)
该行未再次执行。
> /home/ryan/Snake/snake.py(51)__init__()->None
这意味着:这是函数的返回点,因为您没有添加return
(因为__init__
方法应该只返回None)。
如果你检查字节码,它会在最后显示类似的东西:
28 LOAD_CONST 1 (None)
31 RETURN_VALUE
意味着即使没有指定函数,函数也会实际返回None
。
所以,pdb
告诉你函数正在返回它的调用者,它将显示所述函数的最后一行来表示它。