简体   繁体   English

我应该使用哪种模式在同一个循环中调用不同的函数?

[英]Which pattern should I use to call different functions in the same loops?

I often use the same set of nested functions inside which I call different functions.我经常使用同一组嵌套函数,在其中调用不同的函数。 Something with analogous structure to this:与此结构类似的东西:

data = 'example'
...
for i in range(10):
    # use data here
    another_data='another'
    for j in range(10):
        some_func(i, j, data)

...

for i in range(10):
    # use data here
    another_data='another'
    for j in range(10):
        another_func(i, j, another_data)

Which pattern should I use to avoid coping the code?我应该使用哪种模式来避免处理代码?

I tried with decorator, but I didn't know how to pass arguments to some_func that were created in inside wrapper function and omit passing arguments them when calling some_func .我尝试使用装饰器,但我不知道如何将 arguments 传递给在内部包装器 function 中创建的some_func并在调用some_func时省略传递 arguments 它们。

You can pass a function as parameter.您可以将 function 作为参数传递。 No need for a special design pattern.不需要特殊的设计模式。

def some_func(i, j, data):
    pass

def another_func(i, j, data):
    pass

def process_data(data, processor_function):
    for i in range(10):
        # use data here
        another_data='another'
        for j in range(10):
            processor_func(i, j, data)

data = 'example'
process_data(data, some_func)
process_data(data, another_func)

You can loop through a list of functions:您可以遍历函数列表:

funcs = [some_func, another_func]

data = 'example'
for i in range(10):
    for j in range(10):
        for func in funcs:
            func(i, j, data)

I have finally used a generator:我终于使用了一个生成器:

def some_func(i, j, data):
    pass

def another_func(i, j, data):
    pass

def input_generator(data):
    for i in range(10):
        # use data here
        another_data='another'
        for j in range(10):
            yield i, j, data

data = 'example'

for i, j, data2 in input_generator(data):
    some_func(i, j, data2)
    another_func(i, j, data2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何命名在不同模块中执行相同操作的函数? - How should I name functions that do the same thing but in different modules? 我应该使用类来定义将使用相同参数的函数,还是应该使用全局变量? - Should I use a class to define functions that will use the same arguments, or should I use global variables? 使用argparse调用不同的函数 - Use argparse to call different functions 是否应将同一对象用于独立的不同数据库操作? - Should I use the same object for independent, different database operations? 对于不同功能的变量使用相同的名称? - Using the same name for variables which are in different functions? 我应该在每次调用父函数时还是仅在内部重写函数中使用super()吗? - Should I use super() every time I call parent function or only inside overridden functions? 我应该使用哪个Tkinter函数查找随机放置(在画布中)文本条目的坐标,以及如何? - Which Tkinter functions should i use to find the coordinates of a randomly placed(in the canvas) text entry and how? 同一个类中的同名函数 - 有没有一种优雅的方法来确定调用哪个? - Same name functions in same class - is there an elegant way to determine which to call? 相同的URL模式在Flask中调用不同的视图函数 - Same URL pattern calling different view functions in Flask 用于动态选择和排序过滤功能列表的哪种Python模式? - Which Python pattern to use for dynamically selecting and ordering a filtered list of functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM