Python 3.2文档是指Collin Winter的functional模块,其中包含以下功能compose:
compose()函数实现了函数组合.换句话说,它返回外部和内部callables周围的包装器,这样内部的返回值直接送到外部.
不幸的是,这个模块自2006年7月以来一直没有更新; 我想知道是否有任何更换.
现在,我只需要compose功能.以下原始functional.compose定义是否仍适用于Python 3?
def compose(func_1, func_2, unpack=False):
"""
compose(func_1, func_2, unpack=False) -> function
The function returned by compose is a composition of func_1 and func_2.
That is, compose(func_1, func_2)(5) == func_1(func_2(5))
"""
if not callable(func_1):
raise TypeError("First argument to compose must be callable")
if not callable(func_2):
raise TypeError("Second argument to compose must be callable")
if unpack:
def composition(*args, **kwargs):
return func_1(*func_2(*args, **kwargs))
else:
def …Run Code Online (Sandbox Code Playgroud) python functional-programming function function-composition python-3.x
我正在使用etree通过xml文件进行递归.
import xml.etree.ElementTree as etree
tree = etree.parse('x.xml')
root = tree.getroot()
for child in root[0]:
for child in child.getchildren():
for child in child.getchildren():
for child in child.getchildren():
print(child.attrib)
Run Code Online (Sandbox Code Playgroud)
在python中避免这些嵌套for循环的惯用方法是什么.
getchildren() ? list of Element instances [#]
Returns all subelements. The elements are returned in document order.
Returns:
A list of subelements.
Run Code Online (Sandbox Code Playgroud)
我在SO中看到了一些帖子,比如 避免嵌套for循环 但是没有直接转换为我的使用.
谢谢.