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)
Worse. It changes the logic.
passactually means: Do nothing. If you would replacereturnwithpasshere, the control flow would continue, changing the semantic of the code.The purpose for
passis to create empty blocks, which is not possible otherwise with Python’s indentation scheme. For example, an empty function in C looks like this:In Python, this would be a syntax error:
This is where
passcomes handy: