有没有办法让函数调用“继续”影响其调用者中的循环?

戈卡纳

我正在尝试重构由嵌套循环组成的代码。对于类似的重复代码片段,我正在尝试编写一个包含“继续”的函数。循环很长,我只想在函数中包含 if 语句。

def calc_indexes_for_rand_switch_on(self, switch_on, rand_time, max_free_spot, rand_window):
    if np.any(self.daily_use[switch_on:rand_window[1]] != 0.001):  # control to check if there are any other switch on times after the current one
        next_switch = [switch_on + k[0] for k in np.where(self.daily_use[switch_on:] != 0.001)]  # identifies the position of next switch on time and sets it as a limit for the duration of the current switch on
        if (next_switch[0] - switch_on) >= self.func_cycle and max_free_spot >= self.func_cycle:
            upper_limit = min((next_switch[0] - switch_on), min(rand_time, rand_window[1] - switch_on))
        elif (next_switch[0] - switch_on) < self.func_cycle and max_free_spot >= self.func_cycle:  # if next switch_on event does not allow for a minimum functioning cycle without overlapping, but there are other larger free spots, the cycle tries again from the beginning
            continue
        else:
            upper_limit = next_switch[0] - switch_on  # if there are no other options to reach the total time of use, empty spaces are filled without minimum cycle restrictions until reaching the limit
    else:
        upper_limit = min(rand_time, rand_window[1] - switch_on)  # if there are no other switch-on events after the current one, the upper duration limit is set this way
    return np.arange(switch_on, switch_on + (int(random.uniform(self.func_cycle, upper_limit)))) if upper_limit >= self.func_cycle \
        else np.arange(switch_on, switch_on + upper_limit)

这是我正在尝试编写的功能。if 语句是主代码中较大的 while 循环的一部分。但是,这里给出了一个错误,因为没有循环。我该如何解决这个问题?

胡安帕里维拉加

您可以在此处返回一个标记值,例如None并在调用者中处理它。

所以在你的功能中:

    elif (next_switch[0] - switch_on) < self.func_cycle and max_free_spot >= self.func_cycle:  # if next switch_on event does not allow for a minimum functioning cycle without overlapping, but there are other larger free spots, the cycle tries again from the beginning
        return None

并像这样使用它:

while some_condition():
    # do some stuff
    ...
    result = calc_indexes_for_rand_switch_on(switch_on, rand_time, max_free_spot, rand_window)
    if result is None:
        continue
    ...
    # do stuff with result

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

有没有办法在被调用类的方法中访问变量/获取调用者类的分配值?

有没有办法减少与其调用者通信的无限生成器中的产量?

有没有办法在ListView中调用函数?

在 for 循环中使用参数调用 javascript 函数:有没有办法暂停循环?

有没有办法调用部分函数

JavaScript中有没有办法减少递归函数调用中的调用栈大小?

有没有办法在调用 nodejs 中的控制器之前调用函数

有没有办法从方法中调用对象?

有没有办法使成员函数不能从构造函数中调用?

有没有办法在单独的 js 文件中为 jquery click() 函数调用函数?

有没有办法在javascript中调用对象内的所有函数?

有没有办法在继续代码之前等待异步应用程序脚本函数调用?

有没有办法在Kotlin中从内部调用匿名函数?

有没有办法以角度调用html属性值中的函数?

有没有办法在函数调用中更改对对象的原始引用

有没有办法从bash函数调用中强制回显命令行?

有没有办法知道超类构造函数中调用对象的子类?C ++

有没有办法一次在JavaScript中多次调用函数?使用木偶

有没有办法从python中的调用函数访问变量?

有没有办法包装函数调用或使函数调用过载?

有没有办法在调用“zsh”后继续执行脚本?

bash中的“继承”;有没有办法在覆盖函数中调用super(没有eval)?

如果不为其分配变量,有没有办法调用 tkinter 按钮?

函数名称与内核模块中的函数冲突。有没有办法先调用自己的函数?

有没有办法从Chartjs的click函数调用组件的函数

有没有办法让 Racket Web 应用程序中的按钮调用文件中定义的函数?

有没有办法在for循环中使用“点调用”?

有没有办法递归调用findMatch?

有没有办法让 eauto 正确调用 econstructor?