Pass and return

Chris Angelico rosuav at gmail.com
Fri Dec 21 07:19:29 CET 2012


On Fri, Dec 21, 2012 at 4:23 PM, iMath <redstone-cold at 163.com> wrote:
> Pass and return
> Are these two functions the same ?
>
> def test():
>         return
>
> def test():
>         pass

They're different statements, but in this case they happen to
accomplish the same thing.

The pass statement means "do nothing". For instance:

while input("Enter 5 to continue: ")!="5":
  pass

The return statement means "stop executing this function now, and
return this value, or None if no value".

Running off the end of a function implicitly returns None.

So what you have is one function that stops short and returns None,
and another that does nothing, then returns None. The functions
accomplish exactly the same, as does this:

test = lambda: None

All three compile to the same short block of code - load the constant
None, and return it.

ChrisA



More information about the Python-list mailing list