Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #35272
| References | <02b7f61c-6eef-4301-a0bc-6cf8473b6fa1@googlegroups.com> |
|---|---|
| Date | 2012-12-21 17:19 +1100 |
| Subject | Re: Pass and return |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1138.1356070778.29569.python-list@python.org> (permalink) |
On Fri, Dec 21, 2012 at 4:23 PM, iMath <redstone-cold@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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Pass and return iMath <redstone-cold@163.com> - 2012-12-20 21:23 -0800
Re: Pass and return Mitya Sirenef <msirenef@lightbird.net> - 2012-12-21 00:40 -0500
Re: Pass and return Mitya Sirenef <msirenef@lightbird.net> - 2012-12-21 00:45 -0500
Re: Pass and return Duncan Booth <duncan.booth@invalid.invalid> - 2012-12-21 08:52 +0000
Re: Pass and return Mitya Sirenef <msirenef@lightbird.net> - 2012-12-21 12:15 -0500
Re: Pass and return Chris Angelico <rosuav@gmail.com> - 2012-12-21 17:19 +1100
Re: Pass and return Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-21 08:27 +0000
Re: Pass and return iMath <redstone-cold@163.com> - 2012-12-22 07:11 -0800
csiph-web