Python关闭无法正常工作

Python关闭无法正常工作,第1张

Python关闭无法正常工作

一种方法是执行此 *** 作:

def main():    files = [r'C:_localtest.txt', r'C:_localjunk.txt']    funcs = []    for f in files:        # create a new lambda and store the current `f` as default to `path`        funcs.append(lambda path=f: os.stat(path))    print funcs    # calling the lambda without a parameter uses the default value    funcs[0]()     funcs[1]()

否则

f
,在调用函数时将进行查找,因此您将获得当前(循环后)的值。

我更喜欢的方式:

def make_statfunc(f):    return lambda: os.stat(f)for f in files:    # pass the current f to another function    funcs.append(make_statfunc(f))

甚至(在python 2.5+中):

from functools import partialfor f in files:    # create a partially applied function    funcs.append(partial(os.stat, f))


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5653529.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存