Pass and return

Discussion in 'Python' started by iMath, Dec 21, 2012.

  1. iMath

    iMath Guest

    Pass and return
    Are these two functions the same ?

    def test():
    return

    def test():
    pass
    iMath, Dec 21, 2012
    #1
    1. Advertising

  2. 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/
    Mitya Sirenef, Dec 21, 2012
    #2
    1. Advertising

  3. 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/
    Mitya Sirenef, Dec 21, 2012
    #3
  4. On Fri, Dec 21, 2012 at 4:23 PM, iMath <> 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
    Chris Angelico, Dec 21, 2012
    #4
  5. 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
    Steven D'Aprano, Dec 21, 2012
    #5
  6. On 12/21/2012 03:52 AM, Duncan Booth wrote:
    > Mitya Sirenef <> 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/
    Mitya Sirenef, Dec 21, 2012
    #6
  7. iMath

    iMath Guest

    在 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ï¼
    iMath, Dec 22, 2012
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QW5nZWw=?=

    Pass a Web Address and return a File System Address

    =?Utf-8?B?QW5nZWw=?=, Sep 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    523
    =?Utf-8?B?U3RldmUgQy4gT3Jy?=
    Sep 21, 2005
  2. metaperl
    Replies:
    4
    Views:
    543
    John J. Lee
    Sep 8, 2006
  3. Greenhorn
    Replies:
    15
    Views:
    783
    Keith Thompson
    Mar 6, 2005
  4. blufox
    Replies:
    2
    Views:
    532
  5. venkatagmail
    Replies:
    11
    Views:
    658
    James Kanze
    Oct 3, 2007
Loading...

Share This Page