In Python, what is the difference between pass and return

Question:

I have seen some code in Pinax and other django apps that instead of pass, an empty return statement is used. What is the difference and would it have any effect on, for example, the django code below that I am running? The code is a signal method that automatically saves the hashtags into taggit Tag objects for a tweet object.

I saw a question here about whether having or not having a return statement in PHP makes a difference in the interpreted bytecode, but I am not sure if it is relevant to Python.

import re
TAG_REGEX = re.compile(r'#(?P<tag>w+)')

def get_tagged(sender, instance, **kwargs):
    """
    Automatically add tags to a tweet object.
    """
    if not instance:
        return # will pass be better or worse here?
    post = instance
    tags_list = [smart_unicode(t).lower() for t in list(set(TAG_REGEX.findall(post.content)))]
    if tags_list:
        post.tags.add(*tags_list)
        post.save()
    else:
        return # will a pass be better or worse here?
post_save.connect(get_tagged, sender=Tweet)
Asked By: Li Xiong

||

Answers:

Return exits the current function or method. Pass is a null operation and allows execution to continue at the next statement.

Answered By: ʇsәɹoɈ
if not instance:
    return # will pass be better or worse here?

a pass would continue the execution of the method. A return would terminate the execution of the method. Regarding the following lines, you want to put return here.

else:
    return # will a pass be better or worse here?

here it wouldn’t make a difference. Even removing the whole two lines wouldn’t change anything.

But anyway, imagine you would elaborate the method further. It’s better to choose the right flow from the beginning.

Answered By: eumiro
if not instance:
    return # will pass be better or worse here?

Worse. It changes the logic. pass actually means: Do nothing. If you would replace return with pass here, the control flow would continue, changing the semantic of the code.

The purpose for pass is to create empty blocks, which is not possible otherwise with Python’s indentation scheme. For example, an empty function in C looks like this:

void foo()
{
}

In Python, this would be a syntax error:

def foo():

This is where pass comes handy:

def foo():
    pass
Answered By: Ferdinand Beyer

See the python docs on pass and return. In your current code there isn’t really a difference (in fact, I would leave the else away entirely).

The difference is mainly in the semantics: pass can be used where a statement is syntactically required, but not (yet) needed. On the other hand, return fulfills a sort of contract for a function, providing an explicit result.

Answered By: jro

This is more of a comment than an answer, but it won’t fit as a comment.

if not instance:
    return # will pass be better or worse here?

As explained in the accepted answer, pass would be incorrect.

else:
    return # will a pass be better or worse here?

Here, since we are at the end of the function, pass would do the same thing: nothing. Even better would be to leave off the else clause entirely, since there is nothing to do. (It’s true that “explicit is better than implicit” in the Zen, but this rarely applies to explicitly doing nothing – it’s clear what happens in the else case, to anyone who actually even wonders about it in the first place.)

(It’s also not clear why you rename instance to post within the function – you could just change the parameter name? – or what the sender argument is supposed to be for. Oh, and you don’t actually need to convert a set to a list to iterate over it in a list comprehension. 🙂 )

Answered By: Karl Knechtel

This illustrates some earlier answers.

def p():
  "Executes both blocks."
  if True:
    print(1)
    pass
  if True:
    print(2)
    pass

def r():
  "Executes only the first block."
  if True:
    print(1)
    return
  if True:
    print(2)
    return
if not instance:
    return # will pass be better or worse here?

It’ll be worse, cause in that case it’ll execute the following line of codes in your defined function even when the there is no instance.

pass has it’s own essence, it is used to define empty blocks, it shouldn’t be considered as an alternative to a return statement.

Answered By: YDF
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.