Python 'clean_ops=[str.strip,remove_标点符号,str.title]的模式`

Python 'clean_ops=[str.strip,remove_标点符号,str.title]的模式`,python,Python,我学会了将函数作为参数传递的技巧 def remove_punctuation(value): return re.sub('[!#?]', '', value) clean_ops = [str.strip, remove_punctuation, str.title] def clean_strings(strings, ops): result = [] for value in strings: for function in ops:

我学会了将函数作为参数传递的技巧

def remove_punctuation(value):
    return re.sub('[!#?]', '', value)

clean_ops = [str.strip, remove_punctuation, str.title]

def clean_strings(strings, ops):
    result = []
    for value in strings:
        for function in ops:
            value = function(value)
        result.append(value)
    return result

print(clean_strings(states, clean_ops))
我发现将所需函数封装到一个列表中是一件令人惊奇的事情,这个列表甚至有助于思考过程


这种实践的模式是什么?

@jpp设计模式。顺便说一句,我认为他们正在寻找一个。作为一个单独的(相关的?)问题,最好将函数合并成一个,而不是在
for
loop.ty中顺序应用它们。我很难想出一个抽象的概念来描述这样的背景@jppDavid Beazley在这个问题上做了一次精彩的演讲。您可以在此处找到幻灯片: