作为 Mathematica 用户,我喜欢自动“遍历列表”的函数(正如 Mathematica 人所说的那样 - 请参阅http://reference.wolfram.com/mathematica/ref/Listable.html)。这意味着如果给函数一个列表而不是单个值,它会自动使用每个列表条目作为参数并返回结果列表 - 例如
myfunc([1,2,3,4]) -> [myfunc(1),myfunc(2),myfunc(3),myfunc(4)]
我在 Python 中实现了这个原理,如下所示:
def myfunc(x):
if isinstance(x,list):
return [myfunc(thisx) for thisx in x]
#rest of the function
这是一个好方法吗?你能想到这个实施或整体战略的任何缺点吗?