Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The pass statement...
#1
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
Reply
#2
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.
Reply
#3
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
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...

:)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to pass encrypted pass to pyodbc script tester_V 0 853 Jul-27-2023, 12:40 AM
Last Post: tester_V
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,553 Sep-07-2020, 08:02 AM
Last Post: perfringo
  Pass by reference vs Pass by value leodavinci1990 1 2,199 Nov-20-2019, 02:05 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020