Python语法解释:

Python syntax explanation:

你能解释一下为什么这个 Python 代码对 C 程序员有效吗?

这个惯用法用于递归地检索文件列表

[os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(dir)) for f in fn]

我在 Recursive os.listdir? 中找到了这种风格。它没有标点符号,也没有嵌套。我不明白为什么会这样。你能不能用我能理解的更乏味的风格来写,这样我就不会感到那么无助? :)

人们通常说"give us an example of why you use that",我将向您展示整个程序。我很惊讶它竟然起作用了,我在博客上写了一张图片 http://pj.freefaculty.org/blog/?p=285

这是 Ubuntu 14.10 的 XFCE4 桌面和 Compiz window manager/compositor。它将通过 gsettings 与 Dconf 交互来替换视口上的背景墙纸。

下面random.choice()里面的用法我不明白:

#!/usr/bin/env python3

import argparse
import subprocess
import sys
import os
import random

parser = argparse.ArgumentParser()
parser.add_argument("-d", "--dir",  help = "directory path", 
                default = "/usr/local/share/Backgrounds")
parser.add_argument("-w", "--workspace", 
                help = "workspace number, 0, 1-n, or > n", default = "-1", 
                type = int)
parser.add_argument("-schema", help = "gsettings shema", 
                metavar = "SCHEMA", default = "org.compiz.wallpaper:/org/compiz/profiles/Default/plugins/wallpaper/")
parser.add_argument("-key", help = "gsettings key", metavar = "KEY", default = "bg-image")
args = parser.parse_args()

array = eval(subprocess.check_output(["gsettings", "get", args.schema, args.key]))

## print(array)
arraylen = len(array)

filename = random.choice([os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(args.dir)) for f in fn])
print("The newly found filename is:")
print(filename)

ws = args.workspace - 1

## If ws 0 or smaller, we are going to reset whole collection back to
## just one image. if ws > N of images, then add a new image.
if ws < 0:
    array=[str(filename)]
    subprocess.call(["gsettings", "set", args.schema, args.key, str(array)])
    subprocess.call(["gsettings", "set", args.schema, "bg-fill-type", str("[0]")])
    subprocess.call(["gsettings", "set", args.schema, "bg-image-pos", str("[0]")])
    subprocess.call(["gsettings", "set", args.schema, "bg-color1", str("['#000000ff']")])
    subprocess.call(["gsettings", "set", args.schema, "bg-color2", str("['#000000ff']")])
elif ws < arraylen:
    array[ws]=str(filename)
    subprocess.call(["gsettings", "set", args.schema, args.key, str(array)])
else:
    array.append(str(filename))
    subprocess.call(["gsettings", "set", args.schema, args.key, str(array)])
    arraylen = len(array)
    subprocess.call(["gsettings", "set", args.schema, "bg-fill-type", str([0]*arraylen)])
    subprocess.call(["gsettings", "set", args.schema, "bg-image-pos", str([0]*arraylen)])
    subprocess.call(["gsettings", "set", args.schema, "bg-color1", str(['#000000ff']*arraylen)])
    subprocess.call(["gsettings", "set", args.schema, "bg-color2", str(['#000000ff']*arraylen)])

subprocess.call(["gsettings", "set", args.schema, args.key, str(array)])

print("HELLO, corrected image array is:")               
print('\n '.join(array))

如果你说 "we don't understand what you don't understand," 我明白了。下面是我想问的一些具体问题

  1. 为什么dp,dn,fn两边没有括号
  2. 末尾的for语句"for f in fn"没有语句"inside it".
  3. 当 dp 和 f 在该语句之前不存在时,os.path.join(dp, f) 如何工作?

当人们将 if 语句放在行尾而不是开头时,我对 Perl 有同样的过敏感觉。

当你 运行:

a=[[1,2],[3,4]]
[x for y in a for x in y]

你得到:

[1,2,3,4]

所以换句话说,它 运行s y 对于 a 中的每个条目,而不是将 x 设置为 y 中的每个条目,所以它相当于:

for y in a:
    for x in y:
        code with x

您可以将代码重写为:

result=[]
for dp, dn, fn in os.walk(os.path.expanduser(args.dir)):
   for f in fn:
       result.append(os.path.join(dp, f))

重要的是,第一个总是循环的左边,所以可以这样使用变量。

至于中括号,可以写在那里,但隐含明确必须是元组,所以没必要写。