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

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

  • 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, 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".

  • Remco Gerlich
    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.
  • Docstrings for empty classes. Pass + comment for empty statement bodies. (Ab)using strings for comments inside functions just feels wrong to me.
  • k3rni
    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.
  • Doug Napoleone
    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
    >>>
  • Luka Marinko
    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
  • 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", 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?"
blog comments powered by Disqus