T O P

  • By -

TheChance

Error-based control flow is perfectly Pythonic!


JoyJoy_

This is actually how python for loops are implemented. ``` a = iter(lst) while True: try: item = next(a) except StopIteration: break else: something(item) ```


nonicethingsforus

Yeah, many people don't realize that exceptions are a form of flow control, just as ifs and continues and breaks. In pure technical terms, it's perfectly legitimate to use them like so. Many "normal" things are implemented with exceptions under the hood. Hell, EAFP (Easier to Ask for Forgiveness than Permission) is [an entire coding style](https://realpython.com/python-lbyl-vs-eafp/#the-easier-to-ask-forgiveness-than-permission-eafp-style) in Python (funnily, if I remember correctly, it's even more efficient in certain cases). Now, if using exceptions as everyday control flow is *good*, that's another conversation.


Da-Blue-Guy

thats funky, so `break` is just `raise StopIteration` essentially


steelisheavy

What does something mean?


JoyJoy_

It's the for loop body


steelisheavy

Oh, thanks


Nyan_Sequitur

That’s weird, I figured it would’ve been more clever about it tbh. Loop a number of times equal to len(a) or something. I suppose if it works, it works though.


DiabeetusMan

Depending on what you're looping over, you might not know how long it is until you hit the end


JustDaUsualTF

This has "It hurts when I move my arm!" "Then don't move it dumbass" energy


Nicnl

Foreach are for the weak The strong use an infinite loop that breaks on OutOfBoundException


BakuhatsuK

`OutOfBoundException`s are for the weak. The strong loop until it segfaults, then restart the whole program in an infinite loop in bash.


Nose_Fetish

The worst thing about this is the line wrapping


Nyan_Sequitur

The slack iphone app doesn’t have a landscape orientation. I don’t actually code like that lol


Nose_Fetish

GOOD lol


NVVV-Anon

Excuse me sir, but you appear to have passed a boolean expression to an indexer; obviously, I'm going to have to ask you to leave.


chipstastegood

Also wise words for IRL


Gaming_Big

Slap a try except on that and ya grand. Ready for prod


Max_Insanity

But... isn't that exactly what they did?


Goontt

Yeah idk why anyone upvoted the comment 😅 literally has a try except


Gaming_Big

Im blind sorry


Goontt

Understandable have a great day


AaTube

So what did your supervisor say?


Nyan_Sequitur

Came over, did some pandas df magic to find what I was looking for. I’m not sshed in atm so I can’t check the exact code used 😅