Pass and return

Mitya Sirenef msirenef at lightbird.net
Fri Dec 21 06:40:01 CET 2012


On 12/21/2012 12:23 AM, iMath wrote:
> Pass and return
> Are these two functions the same ?
>
> def test():
> 	return
>   
> def test():
> 	pass

I believe they are the same, but these statements have
different meanings in other circumstances, e.g.:

Class A(object): pass

def test():
   if x: return
   else: # do something

In first example, (in a class), return would be invalid.

In second example, return would return None from function,
pass would result in continuing execution after if/else block.

Btw you can use disassemble function to look into what
these functions do:

 >>> def a(): pass
 >>> def b():return
 >>> from dis import dis
 >>> dis(a)
   1           0 LOAD_CONST               0 (None)
               3 RETURN_VALUE
 >>> dis(b)
   1           0 LOAD_CONST               0 (None)
               3 RETURN_VALUE


So indeed they should be the same..

  -m

-- 
Lark's Tongue Guide to Python: http://lightbird.net/larks/




More information about the Python-list mailing list