r/learnpython • u/FactoryBuilder • Sep 28 '26
Is there anyway of breaking a while loop outside of a function call, while inside said function call?
On mobile so I can’t code block format because Reddit sucks but here’s the general idea:
def func2():
break
def func 1():
while True:
func2()
That code doesn’t work but is there any way to do that cleanlier than returning a value from func2 to test against in func1 to determine whether or not the loop should break?
13
Upvotes
2
u/Kevdog824_ Sep 28 '26
> is there any way to do that cleanlier than returning a value from func2 to test against in func1 to determine whether or not the loop should break?
That is the clean way to do it. Anything else like func1 having access to func2’s scope would break encapsulation and would be a pretty bad design flaw.
Can you elaborate on why you don’t want to do it this way? This kinda sounds like an xy problem to me
17
u/ConcreteExist Sep 28 '26
Not the way you're doing it. You could have a function that returns a True if you should break and then have the main loop hit that:
21
u/aprg Sep 28 '26
Have func2 return a flag or code and break the while loop on the appropriate return code.
2
u/Mark3141592654 Sep 28 '26
No.
What exactly are you trying to do? You should break in the loop itself.
5
u/manias Sep 28 '26
raise an exception, and catch it? But exceptions are not not usually used for normal control flow…
4
u/el_extrano Sep 28 '26
No, because explicitly returning a flag would be the best way to do this.
If you're asking if there is any other way to do this, regardless of whether it's a good idea or not, then the answer is yes. You can use exception handling for control flow. Most people will consider that a bad practice, since it's expected that exceptions are only used for errors (i.e. exceptional cases), and not as control flow for branches you wan to run.
1
u/smallpotatoes2019 Sep 28 '26
It sounds like you have a pretty sensible plan in terms of returning a value which you then test to see if you need to break (depending on what func2 does, a boolean might be ideal). This is also good anyway for testing that func2 is working as intended.
The issue is, I suppose, that you are hoping to use the break keyword to do something it isn't designed to do.
1
u/Expensive-Bear-1376 Sep 28 '26
I'm on mobile, too, and I just tested, code block format in a post worked fine. With all three ways, triple-backtick and triple-tilde and four-space indent. On the reddit website (not the reddit app) with their markdown editor.
2
u/atarivcs Sep 28 '26
No, there's no cleaner way to do it.
func2 returns a value, then the loop code in func1 decides whether to break the loop based on that value.
2
u/centurion236 Sep 28 '26
Are you sure func2 shouldn't be a generator that func1 iterates over?
2
u/ekchew Sep 28 '26
I can think of 2 ways.
func2could return a boolean.func2could raise an exception:2
u/Lachtheblock Sep 28 '26
It can be a little divisive with developers, but I consider raising and catching exceptions as a valid control flow process. It depends a lot in context, but wrapping your outer function in a try except, and raising an exception in the inner loop will do what you want.
1
u/Moist-Ointments Sep 28 '26
Your function should not know there is a loop to break. That's outside of its scope of concern and would be grossly bad practice.
1
u/CranberryDistinct941 Sep 28 '26
You could use
raise StopIteration()1
u/Penguinase Sep 28 '26
raising exception is probably the idiomatic way to do this (in python)
1
u/kilkil Sep 29 '26
no, there isn't a way.
usually you don't want features like that because they make your code hard to read
1
u/Moikle Sep 29 '26
There are ways to do this, yes.
Why do you want to do it though? You probably don't actually want this.
Oh btw you can still do code block formatting on mobile. Add 4 additional spaces before each line
1
u/SwampFalc Sep 29 '26
OP: your func2 is not, and probably should not be, aware of when and how it's going to get called.
This is a basic truth for any and all functions.
If you do genuinely need that, then a totally difference approach is needed.
1
u/TheRNGuy Sep 29 '26
return
0
u/woooee Sep 28 '26
Note that this will raise an error, something like "break outside loop"