Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading