Question

Based on PEP8 documentation, I was not able to find any reference regarding if I should use pass for aesthetic reasons on code. Based on the example below, should I keep those else or can I erase them? Until now, the main reason I'm keeping it is based on the mantra "Explicit is better than implicit."

if fields:
    for i in foo:
        if i == 'something':
            print "something"
        else:
            pass
else:
    pass
Was it helpful?

Solution

Yes, you can/should remove them because they do nothing.

The Python community teaches "explicit is better than implicit" as long as the explicit code does something useful. Those else: pass's however contribute nothing positive to the code. Instead, all they do is pointlessly consume two lines each.

OTHER TIPS

You can safely remove those as there's no point in keeping code around that serves no purpose:

if fields:
    for i in foo:
        if i == 'something':
            print "something"

An else pass is dead code, you should remove it, as it adds unnecessary noise to the code and anyway the code will be clearer and easier to understand without it.

I can think of few cases where pass may be useful - the latter two are temporarily stubs:

  • When you want to do ignore an acceptable exception
  • When you need to insert a breakpoint at the end of function when debugging.
  • As a filler in a function whose implementation you want to postpone

I cannot imagine any other case where I will use pass

EDIT:

In some cases, when implementing if-elif-else chain, and you have some common condition that requires no action - along with rare conditions that do require specific actions - for the sake of execution efficiency, you may use pass after the first if:

if <some common condition>:
    pass
elif <rare condition>:
    <do something>
elif <another rare condition>:
    <do something else>
else:
   <do another stuff>

The thing about else is that they are not just a part of the if statements; it appears in try statements and for loops too. You don't see else being used (in this context) in those areas, do you?

try:
    raw_input("say my name")
except:
    print "Heisenberg"
# Meh, this is not needed.
else:
    pass

If we are looping over something and checking for some condition (with the if), then an else would add unnecessary lines.

Here's a loop for finding a folder:

for path in pathlist
    if os.path.isdir(path):
        print "Found a folder, yay!"
        break
    else:
        continue

Clearly, else is executed in every loop and is pointless. This could be avoided as implied in the PEP 8 itself:

But most importantly: know when to be inconsistent -- sometimes the style guide just doesn't apply. When in doubt, use your best judgment. Look at other examples and decide what looks best. And don't hesitate to ask!
When applying the guideline would make the code less readable, even for someone who is used to reading code that follows this PEP.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top