Home > Blockchain >  Python: is there a way to implement break into a function that is meant to be looped?
Python: is there a way to implement break into a function that is meant to be looped?

Time:12-14

I have a class function that updates class instance parameters when a specific condition is met. It looks something like this:

def step_k(self):

        if probability > self.epsilon:

            self.k_dps = np.append(self.k_dps, new_dps)

            self.k  = 1

            self.update_pdf()

It is meant to be used in a loop up until the point where the condition is no longer fulfilled. I tried using 'break' in the hopes it would also work when executed in a function

def step_k(self):

        if probability > self.epsilon:

            self.k_dps = np.append(self.k_dps, new_dps)

            self.k  = 1

            self.update_pdf()
        else:
            break

but it doesn't. probability is calculated in the class so I can't just pull the condition out. Is there another way to do this?

CodePudding user response:

No, break is only allowed syntactically in the loop itself. Your function has to return a value which the caller can use to determine whether to execute a break statement or not.

  • Related