mingxiao
mingxiao

Reputation: 1802

why does a pass statement get executed in this if/else?

When I run the following if/else in debug mode

if True:
  print 'here'
else:
  print 'there'
  pass  # breakpoint here

The debugger stops on the pass statement. Why is it that the pass statement gets executed? I know the pass is irrelevant, but it's inside the else.

I'm running python 2.7.5 on Pycharm 2.7.3

UPDATE

If the pass statement is the last line of the program, and there's a break point on it, the debugger will stop at that pass statement. I know it stopped because I can see the current stack trace and variables.

However if pass is not the last line, the debugger will not stop there.

Upvotes: 1

Views: 265

Answers (1)

David Wolever
David Wolever

Reputation: 154504

The debugger doesn't break on the pass statement. You can verify this by adding a statement after it:

$ cat test.py
if True:
  print 'here'
else:
  print 'there'
  pass  # breakpoint here
print 'done'
$ python -m pdb test.py
> test.py(1)<module>()
-> if True:
(Pdb++) list
  1  -> if True:
  2       print 'here'
  3     else:
  4       print 'there'
  5       pass  # breakpoint here
  6     print 'done'
[EOF]
(Pdb++) break 5
Breakpoint 1 at test.py:5
(Pdb++) continue
here
done
The program finished and will be restarted

The debugger may appear to break there because it's the last line in the file?

Upvotes: 6

Related Questions