有没有办法让函数调用 'continue' 影响其调用者中的循环?
Is there a way to have a function invoke 'continue' affecting a loop in its caller?
我正在尝试重构由嵌套循环组成的代码。对于类似的重复代码片段,我正在尝试编写一个由 'continue' 组成的函数。循环很长,我只想在函数中包含 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 循环的一部分。但是,这里会报错,因为没有循环。我该如何解决?
您可以在此处 return 标记值,例如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
我正在尝试重构由嵌套循环组成的代码。对于类似的重复代码片段,我正在尝试编写一个由 'continue' 组成的函数。循环很长,我只想在函数中包含 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 循环的一部分。但是,这里会报错,因为没有循环。我该如何解决?
您可以在此处 return 标记值,例如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