2

Based on the answers to this question, the pass keyword in Python does absolutely nothing. It simply indicates that "nothing will be done here."

Given this, I don't understand the use of it. For example, I'm looking at the following block of code while trying to debug a script someone else wrote:

def dcCount(server):
    ssh_cmd='ssh user@subserver.st%s' % (server)
    cmd='%s "%s"' % (ssh_cmd, sub_cmd)
    output=Popen (cmd, shell=True, stdout=PIPE)
    result=output.wait()
    queryResult=""
    if result == 0:
        queryResult = output.communicate()[0].strip()
    else:
        pass
    takeData(server, "DC", queryResult)

Is there any purpose at all to having else: pass here? Does this in any way change the way the function runs? It seems like this if/else block could be rewritten like the following with absolutely no change:

if result == 0:
    queryResult = output.communicate()[0].strip()
takeData(server, "DC", queryResult)

... or am I missing something? And, if I'm not missing something, why would I ever want to use the pass keyword at all?

Community
  • 1
  • 1
asteri
  • 11,402
  • 13
  • 60
  • 84
  • 1
    I have used it in if elif elif else situations in which I need one the of possibilities to be tested for but not processed so that it does not get included in the else case (which must do something). For example lets say that you had to pass result == 2, but the else case required a function to be called. – sabbahillel Mar 24 '14 at 14:51
  • @sabbahillel I don't see that as a valid use-case, because instead of having `else: your_func()` you could simply have `if result != 2: your_func()`. In if/else, you can just write your conditions in a reasonable way rather than using this `pass` keyword. – asteri Mar 24 '14 at 15:05
  • I meant, that let us say you have a number of circumstances, for the various values of a flag and you have to be careful that the else case does not include one of the circumstances. It is like the switch situation in c. Flag can be several values, one of which nothing can be done. You cannot have it as part of the else (default case) because something else needs to be done. so you would have elif flage ==x: pass as well as a number of other checks. that is what I meant. – sabbahillel Mar 25 '14 at 01:59

4 Answers4

11

It is indeed useless in your example.

It is sometimes helpful if you want a block to be empty, something not otherwise allowed by Python. For instance, when defining your own exception subclass:

class MyException(Exception):
    pass

Or maybe you want to loop over some iterator for its side effects, but do nothing with the results:

for _ in iterator:
    pass

But most of the time, you won't need it.

Remember that if you can add something else that isn't a comment, you may not need pass. An empty function, for instance, can take a docstring and that will work as a block:

def nop():
    """This function does nothing, intentionally."""
RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79
  • 2
    +1 I also think extending exceptions is the more useful usage (except for debugging and delaying implementation). – Paulo Bu Mar 24 '14 at 14:23
4

pass is used when you want to do nothing, but are syntatically required to have something. I most commonly use it with try...except blocks when I want to simply skip over lines which have an error, as shown below

>>> try:
...    1/0
...
  File "<stdin>", line 3

    ^
SyntaxError: invalid syntax
>>> try:
...    1/0
... except:
...
  File "<stdin>", line 4

    ^
IndentationError: expected an indented block
>>> try:
...    1/0
... except:
...    pass
...
>>>
wnnmaw
  • 5,444
  • 3
  • 38
  • 63
  • Ah, so it's primarily used to swallow exceptions? That seems like a bad practice. – asteri Mar 24 '14 at 14:22
  • In the particular case I demonstrated it, yes, that's what its doing. Generally its a way for programmers to get around syntactical requirements for some simple cases – wnnmaw Mar 24 '14 at 14:23
  • @JeffGohlke it would be a bad practice if used with catch-all exceptions as in these examples, but it's reasonable with specific exceptions. – kojiro Mar 24 '14 at 14:24
1

Empty classes

Empty functions

or, like the other guy said - empty clauses like "except"

vish
  • 1,046
  • 9
  • 26
0

There is no point to the use of pass you have shown, it can be removed along with the else. Still, I have found pass useful occasionally, such as when commenting out the body of a conditional, or when implementing a class which has no members:

class dummy:
    pass
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Except when doing quick-and-dirty checks in a REPL, I would argue that a class with no members should still have a docstring. (And a docstring would obviate the need for `pass` in this case.) – kojiro Mar 24 '14 at 14:26
  • @kojiro: absolutely, perhaps I should have said that I rarely use `pass` in production--even more rarely than during development! – John Zwinck Mar 24 '14 at 14:26