Python3有没有理由执行一条语句两次?
我有一个功能:Python3有没有理由执行一条语句两次?,python,pdb,statements,Python,Pdb,Statements,我有一个功能: 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("Placi
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行的语句执行了两次。为什么会这样?该行不会再次执行。
> /home/ryan/Snake/snake.py(51)__init__()->None
这意味着:这是函数的返回点,因为您没有添加返回值
(因为\uuuu init\uuuu
方法无论如何都应该只返回无)
如果您检查字节码,它将在末尾显示类似的内容:
28 LOAD_CONST 1 (None)
31 RETURN_VALUE
这意味着即使未指定,函数也将实际返回None
因此,
pdb
告诉您函数正在返回到它的调用者,它将显示所述函数的最后一行来表示它。是self.direction
属性吗?它只是函数的最后一行,它将返回None
,因为没有指定return
。这条线不行again@Mehrdad它的目的是成为一个变量。如果我的语法错误,这可能是解释器的一个属性,但我对此表示怀疑。从现在起,我已经发布了当前课程的全部内容。