Tip: Use string literals instead of the pass statement?
The pass statement is simply used where the Python syntax requires a statement, but the application doesn’t need any logic. Typically it is accompanied by a comment explaining why it is there. A simple example:
my custom exception
class MyException(Exception): pass if condition: pass # we don’t need this right now - testing
More often than not I will use a triple quoted string in its place. This makes it more self documenting and looks cleaner to me. My preferred usage:
class MyException(Exception):
"""my custom exception"""
if condition:
"we don't need this right now - testing"
What do you prefer?
July 26, 2008 | Filed Under python
Comments
10 Responses to “Tip: Use string literals instead of the pass statement?”
Leave a Reply





I prefer the “pass”, just because it’s a TEENY bit clearer that nothing is going on.
If you just have the triple-quote string, there’s a chance that someone will have to pause and wonder “how come that’s not bombing out?”
Usually the only time I use the pass statement is when I’m defining methods that I haven’t implemented yet. When I do need to have empty classes/functions, I use a docstring and a pass statement.
I prefer the pass statement.
It’s easy to spot, It’s easy to search for, and you can’t mistake what its there for.
And if I saw your condition (or function or class or …) without a pass keyword I would assume It was an error.
“we don’t need this right now - testing”, is a lot longer to read than pass, and it doesn’t add any extra information than pass.
Its not that your way is worse in absolute sense, but it comes down to convention and most programmers use and expect pass statement to be there. I think its bad habit to break this unless there is good gain to be had, and I just don’t see it here.
Luka
My standard base exception class (for what its worth)
>>> class MyE(Exception): … “Exception String” … def str(self): … res = super(MyE, self).str() … if not res: return self.doc … return res … >>> >>> >>> raise MyE Traceback (most recent call last): File “”, line 1, in main.MyE: Exception String >>> raise MyE, “foo” Traceback (most recent call last): File “”, line 1, in main.MyE: foo >>>
While the exception is pretty standard (it’s a class docstring), the second form doesn’t appeal to me. Looks a little lost inside this code, extracting it is non-obvious (I think) and, most of all, Pylint will complain (”statement has no effect”). YMMV.
Docstrings for empty classes. Pass + comment for empty statement bodies. (Ab)using strings for comments inside functions just feels wrong to me.
For the class, I would prefer to do both. The doc string is documentation, the pass is the visual clue that nothing is actually happening.
In the case of the if statement, I’d simply remove it.
+1 for pass, otherwise it feels like something’s missing. Also, explicit is better than implicit — with “pass” I can see that the code there is meant to do nothing, instead of wondering if some code is missing at that point.
And as somebody else pointed out, it’s easier to search/grep for “pass”.
I prefer both pass and a string literal explaining why you are passing. It just doesn’t feel right to implicitly pass… plus it violates the zen of python
+1 for pass from me too, especially after if. If I saw a string being used like that, I’d assume it was a Ruby/functional language refugee who forgot that you need return in Python to return values.
For the exception, it’s not too bad, but I’d probably add an explicit pass there too.