why pass statement?

Hans Nowak hans at zephyrfalcon.org
Tue Sep 16 16:23:41 EDT 2003


M-a-S wrote:

> Why is there the pass statement? I think, the expression statement would be enough:
> 
> class C:
>     None
> 
> while True:
>     None

'pass' is a no-op statement that, according to the tutorial, is used when a 
statement is required syntactically but the program requires no action.

Your code, using None, has the same effect.  However, there's a slight 
difference in the bytecode that is generated:

 >>> def f(): pass
...
 >>> def g(): None
...
 >>> import dis
 >>> dis.dis(f)
   1           0 LOAD_CONST               0 (None)
               3 RETURN_VALUE
 >>> dis.dis(g)
   1           0 LOAD_GLOBAL              0 (None)
               3 POP_TOP
               4 LOAD_CONST               0 (None)
               7 RETURN_VALUE

This is probably irrelevant, but it wouldn't be correct to say that using pass 
is exactly the same as using None.

HTH,

-- 
Hans (hans at zephyrfalcon.org)
http://zephyrfalcon.org/







More information about the Python-list mailing list