为什么还要这样:需要传递才能继续处理?

2024-05-16 18:50:36 发布

您现在位置:Python中文网/ 问答频道 /正文

有人能解释为什么需要下面所示的else: pass来执行其余代码(最后的print 'processing...语句)吗?注意else中的print被放在那里,这样我就可以知道执行确实走了这条路。

似乎只要continue没有执行,就会发生这种情况,因为else中的代码什么也不做。但是,如果我不使用else,那么当条件为False时,for循环中的任何内容都不会执行--当目录中存在扩展名为do的文件时--这对我来说没有意义。文档说continue“继续最近的封闭循环的下一个循环”,很好,但是如果没有执行一个循环,那么处理不应该继续到下一个语句吗?

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)

谜团解开

这种看似奇怪的行为是由于缩进问题,在上面的代码中不可见,在我的文本编辑器中也不常见。原来,最后一个print语句缩进了3个空格,然后是一个制表符,这使得它看起来与else对齐,但事实上,它要么跟在else中的pass后面,要么跟在if第一部分的continue后面。显然让我很困惑。

这是我的文本编辑器中的代码截图,它的“显示空间/选项卡”选项处于打开状态。红色的点表示空格,红色的右guillemets(»)表示制表符:

screenshot of file in my editor showing bad indentation


Tags: 代码formatsourceforosdirpass语句
2条回答

你不需要它。我运行了以下两个脚本:

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

以及

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

我把它们当作:

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

这里是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

注意“处理”行的存在。

然后我diff输出:

diff junk.log junk.log2

结果如下:

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"

注意,在“处理”行上没有区别。

我会回答我自己的问题并最终接受它。所描述的看似奇怪的行为是由一个微妙的缩进问题引起的,用户@delnan首先引起了我的注意。因为它是看不见的,最初我认为不可能是这个案子,但最终经过更多的调查才找到它。我的问题的最后加上了这些细节。

相关问题 更多 >