Why is this else: pass needed for processing to continue? [closed]

丶灬走出姿态 提交于 2020-04-07 03:44:13

问题


Can someone explain why the else: pass shown below is needed in order for the rest of the code (the final print 'processing... statement) to be executed? Note the print in the else was put there just so I could tell that execution was indeed taking that path.

It seems like that should happen whenever the continue isn't executed since code in the else does nothing. However, if I leave the else out, nothing further in the for loop appears to be executed when the condition is False -- when files with the extension do exist in the directory -- which doesn't make sense to me. The docs say continue "continues with the next cycle of the nearest enclosing loop", fine, but if one is not executed, shouldn't processing proceed to the next statement?

import os

source_dir = r'C:\Downloads'
ext = '.mp3'

for dirName, subdirList, fileList in os.walk(source_dir):
    if not any(os.path.splitext(fileName)[1].lower() == ext for fileName in fileList):
        print '  skipping "{}"'.format(dirName)
        continue
    else:  # why is this clause needed to continue this iteration of a loop?
        print 'contains   "{}"'.format(dirName)
        pass

    print 'processing "{}" which has "{}" files'.format(dirName, ext)

Mystery Solved

The seemingly strange behavior was due to an indentation problem which is not visible in the code above nor normally in my text editor. It turned out that the last print statement was indented by 3 spaces then a tab, which makes it appear to align with the else, but in fact it either follows the pass in the else if it's there, or follows the continue in the first part of the if. Obviously confusing me a great deal.

Here's a screenshot of the code in my text editor with its "show space/tabs" option turned on. The red dots represent spaces and the red right guillemets (») represents a tab character:


回答1:


You don't need it. I ran the following 2 scripts:

#test1.py
import os

source_dir = '.'
ext = '.txt'

for dirName, subdirList, fileList in os.walk(source_dir):
    if not any(os.path.splitext(fileName)[1].lower() == ext for fileName in fileList):
        print '  skipping "{}"'.format(dirName)
        continue
    else:  # why is this clause needed to continue this iteration of a loop?
        print 'contains   "{}"'.format(dirName)
        pass

    print 'processing "{}" which has "{}" files'.format(dirName, ext)

and

#test2.py
import os

source_dir = '.'
ext = '.txt'

for dirName, subdirList, fileList in os.walk(source_dir):
    if not any(os.path.splitext(fileName)[1].lower() == ext for fileName in fileList):
        print '  skipping "{}"'.format(dirName)
        continue
    #else:  # why is this clause needed to continue this iteration of a loop?
    #    print 'contains   "{}"'.format(dirName)
    #    pass

    print 'processing "{}" which has "{}" files'.format(dirName, ext)

I ran them as:

python test1.py > junk.log
python test2.py > junk.log2

Here's the first couple lines of junk.log:

test $ head junk.log
processing "." which has ".txt" files
  skipping "./new"
  skipping "./unum"
processing "./unum/kiv-unum-409befe069ac" which has ".txt" files
  skipping "./unum/kiv-unum-409befe069ac/build"
  skipping "./unum/kiv-unum-409befe069ac/build/bdist.macosx-10.3-fat"
  skipping "./unum/kiv-unum-409befe069ac/build/lib"
  skipping "./unum/kiv-unum-409befe069ac/build/lib/tests"
  skipping "./unum/kiv-unum-409befe069ac/build/lib/unum"
  skipping "./unum/kiv-unum-409befe069ac/build/lib/unum/units

Notice the presence of "processing" lines.

Then I diff the output:

diff junk.log junk.log2

with the following results:

0a1
> contains   "."
3a5
> contains   "./unum/kiv-unum-409befe069ac"
14a17
> contains   "./unum/kiv-unum-409befe069ac/docs"
16a20
> contains   "./unum/kiv-unum-409befe069ac/nose-1.2.1-py2.7.egg/EGG-INFO"
19a24
> contains   "./unum/kiv-unum-409befe069ac/nose-1.2.1-py2.7.egg/nose"
30a36
> contains   "./unum/kiv-unum-409befe069ac/Unum.egg-info"

Note that there are no differences on the "processing" lines.




回答2:


I'm going to answer my own question and eventually accept it. The seemingly odd behavior described was caused by an subtle indention problem, the possibility of which was first brought to my attention by user @delnan. Because it was invisible, initially I didn't think it could be the case, but eventually found it after more investigation. The details of which have been added to the end of my question.



来源:https://stackoverflow.com/questions/14900821/why-is-this-else-pass-needed-for-processing-to-continue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!