Pass and return

I

iMath

Pass and return
Are these two functions the same ?

def test():
return

def test():
pass
 
M

Mitya Sirenef

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:
1 0 LOAD_CONST 0 (None)
3 RETURN_VALUE1 0 LOAD_CONST 0 (None)
3 RETURN_VALUE


So indeed they should be the same..

-m
 
M

Mitya Sirenef

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
 
C

Chris Angelico

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
 
S

Steven D'Aprano

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.
 
M

Mitya Sirenef

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
 
I

iMath

在 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ï¼
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top