Python Forum

Full Version: The pass statement...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is "pass" really a NOP ?
Is it compile into a real nop operation ?
or it is only interpreted as place holder ?
and that statement is jump over a runtime ...

My debugger cannot put a breakpoint on it...
But we usually are able to breakpoint a nop ...

Think
----------

More info:

from 32.12. dis — Disassembler for Python bytecode

General instructions
NOP
Do nothing code. Used as a placeholder by the bytecode optimizer.

So there is a real NOP in the VM so pass is sureley translate to something that can be breakpointed...

Im going to visit PyScripter WIKI to try to find something
Think
A simple test shows that there is no bytecode instruction in that case
>>> import dis
>>> 
>>> def foo(x):
...     if x:
...         pass
... 
>>> dis.dis(foo)
  2           0 LOAD_FAST                0 (x)
              3 POP_JUMP_IF_FALSE        6

  3     >>    6 LOAD_CONST               0 (None)
              9 RETURN_VALUE
There are only 4 instructions in this bytecode and nothing for the pass.
I think the optimization comes from the ast, but I'm not complete sure about this.
If there is a statement without effect, it's not executed. Also the ... (Ellipsis) is not present in bytecode.

import dis


def foo1(): ...
def foo2(): pass

dis.dis(foo1)
dis.dis(foo2)
Output:
1 0 LOAD_CONST 0 (None) 2 RETURN_VALUE 2 0 LOAD_CONST 0 (None) 2 RETURN_VALUE
Uuuhh I should have play with dis yesterday, but I went to bed :)
You're right not opcode are generated, so no breakpoint I guess...

I will still use
z = 0
as place holder instead of pass, at least I can stop there...

:)