2
我有一个函数:Python 3会执行两次语句吗?
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火了,我得到我的下列顺序/ 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行的声明正在执行两次。这是为什么?
'self.direction'属性? – Mehrdad 2013-04-21 00:36:03
它只是函数的最后一行,它将返回'None',因为没有指定'return'。有没有再次运行线 – JBernardo 2013-04-21 00:36:14
@Mehrdad它的目的是成为一个变量。如果我的语法错误,它可能是解释者的财产,但我对此表示怀疑。截至目前,我已经发布了整个课程。 – Ryan 2013-04-21 00:38:43