Question

I have a function:

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

that creates instances of the following class:

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

When I run this code, pdb fires up, and I get the following sequence of 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")

The statement on line 51 is being executed twice. Why is this so?

Was it helpful?

Solution

The line is not being executed again.

> /home/ryan/Snake/snake.py(51)__init__()->None

It means: This is the return point of the function because you did not added a return (because __init__ methods should only return None anyway).

If you check the bytecode, it will show something like that at the end:

         28 LOAD_CONST               1 (None) 
         31 RETURN_VALUE

meaning the function will actually return None even if it is not specified.

So, pdb is telling you the function is returning to its caller and it will show the last line of said function to represent that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top