Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #35269 > unrolled thread
| Started by | iMath <redstone-cold@163.com> |
|---|---|
| First post | 2012-12-20 21:23 -0800 |
| Last post | 2012-12-22 07:11 -0800 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.python
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
| From | iMath <redstone-cold@163.com> |
|---|---|
| Date | 2012-12-20 21:23 -0800 |
| Subject | Pass and return |
| Message-ID | <02b7f61c-6eef-4301-a0bc-6cf8473b6fa1@googlegroups.com> |
Pass and return Are these two functions the same ? def test(): return def test(): pass
[toc] | [next] | [standalone]
| From | Mitya Sirenef <msirenef@lightbird.net> |
|---|---|
| Date | 2012-12-21 00:40 -0500 |
| Message-ID | <mailman.1136.1356068405.29569.python-list@python.org> |
| In reply to | #35269 |
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/
[toc] | [prev] | [next] | [standalone]
| From | Mitya Sirenef <msirenef@lightbird.net> |
|---|---|
| Date | 2012-12-21 00:45 -0500 |
| Message-ID | <mailman.1137.1356068710.29569.python-list@python.org> |
| In reply to | #35269 |
On 12/21/2012 12:23 AM, iMath wrote: > Pass and return > Are these two functions the same ? > > def test(): > return > > def test(): > pass From the point of style, of course, the latter is much better because that's the idiomatic way to define a no-op function. With a return, it looks like you might have forgotten to add the value to return or deleted it by mistake. -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/
[toc] | [prev] | [next] | [standalone]
| From | Duncan Booth <duncan.booth@invalid.invalid> |
|---|---|
| Date | 2012-12-21 08:52 +0000 |
| Message-ID | <XnsA1305A4D52D45duncanbooth@127.0.0.1> |
| In reply to | #35271 |
Mitya Sirenef <msirenef@lightbird.net> wrote: > On 12/21/2012 12:23 AM, iMath wrote: >> Pass and return >> Are these two functions the same ? >> >> def test(): >> return >> >> def test(): >> pass > > > From the point of style, of course, the latter is > much better because that's the idiomatic way > to define a no-op function. With a return, it > looks like you might have forgotten to add the > value to return or deleted it by mistake. > I would say it is *an* idiomatic way to define a no-op function. Another idiomatic way is to use a doc-string as the only body, that way you can also explain why you feel the need for an empty function. -- Duncan Booth http://kupuguy.blogspot.com
[toc] | [prev] | [next] | [standalone]
| From | Mitya Sirenef <msirenef@lightbird.net> |
|---|---|
| Date | 2012-12-21 12:15 -0500 |
| Message-ID | <mailman.1162.1356110144.29569.python-list@python.org> |
| In reply to | #35279 |
On 12/21/2012 03:52 AM, Duncan Booth wrote: > Mitya Sirenef <msirenef@lightbird.net> wrote: > >> On 12/21/2012 12:23 AM, iMath wrote: >>> Pass and return >>> Are these two functions the same ? >>> >>> def test(): >>> return >>> >>> def test(): >>> pass >> >> From the point of style, of course, the latter is >> much better because that's the idiomatic way >> to define a no-op function. With a return, it >> looks like you might have forgotten to add the >> value to return or deleted it by mistake. >> > I would say it is *an* idiomatic way to define a no-op function. > > Another idiomatic way is to use a doc-string as the only body, > that way you can also explain why you feel the need for an empty > function. > That's true, a docstring is preferable in many cases. -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2012-12-21 17:19 +1100 |
| Message-ID | <mailman.1138.1356070778.29569.python-list@python.org> |
| In reply to | #35269 |
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
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-12-21 08:27 +0000 |
| Message-ID | <50d41d82$0$11099$c3e8da3@news.astraweb.com> |
| In reply to | #35269 |
On Thu, 20 Dec 2012 21:23:58 -0800, iMath wrote:
> Pass and return
> Are these two functions the same ?
They are neither functions, nor are they the same.
Check if they are functions:
- can you pass them arguments?
- can you assign their result to a target?
No.
py> pass(23)
File "<stdin>", line 1
pass(23)
^
SyntaxError: invalid syntax
py> x = return
File "<stdin>", line 1
x = return
^
SyntaxError: invalid syntax
Are they the same? Try it with these two functions:
def test_pass():
for i in range(100):
pass
print i
def test_return():
for i in range(100):
return
print i
py> test_pass()
99
py> test_return()
py>
So what are they?
They are *statements*, not functions. You cannot pass them arguments, nor
do they assign a result to a target on the left hand side of = equals
sign.
"pass" is a do-nothing statement. It literally does nothing.
"return" exits a function and sets the return result. It is only legal
inside functions and generators, while "pass" is legal almost anywhere.
Normally you say "return some_value", but you can leave out the result
and Python will "return None".
If functions get all the way to the bottom without a return statement,
they will return None.
The example you give:
> def test():
> return
The body of the function immediately returns None. But functions return
None by default, so you could leave the "return" statement out. If you do
that, you will get a SyntaxError because there is nothing in the body:
py> def test():
...
...
File "<stdin>", line 3
^
IndentationError: expected an indented block
So if you put a "pass" statement in, just to satisfy the compiler, you
get the same result:
> def test():
> pass
Also a function which immediately exists and return None.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | iMath <redstone-cold@163.com> |
|---|---|
| Date | 2012-12-22 07:11 -0800 |
| Message-ID | <1f5141a8-0dc9-4559-a345-7337bcad39bf@googlegroups.com> |
| In reply to | #35269 |
在 2012年12月21日星期五UTC+8下午1时23分58秒,iMath写道: > Pass and return > > Are these two functions the same ? > > > > def test(): > > return > > > > def test(): > > pass you guys R so knowledgeable ,thanks!
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web