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?

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

Comments

10 Responses to “Tip: Use string literals instead of the pass statement?”

  1. Aaron on July 26th, 2008 9:16 pm

    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?”

  2. Benjamin Peterson on July 26th, 2008 10:27 pm

    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.

  3. Luka Marinko on July 27th, 2008 3:12 am

    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

  4. Doug Napoleone on July 27th, 2008 4:09 am

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

  5. k3rni on July 27th, 2008 4:46 am

    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.

  6. Marius Gedminas on July 27th, 2008 8:25 am

    Docstrings for empty classes. Pass + comment for empty statement bodies. (Ab)using strings for comments inside functions just feels wrong to me.

  7. Remco Gerlich on July 27th, 2008 8:30 am

    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.

  8. Roberto on July 27th, 2008 9:05 am

    +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”.

  9. Mike on July 27th, 2008 6:04 pm

    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 :-)

  10. Juri Pakaste on July 28th, 2008 3:53 am

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

Leave a Reply