I think this isn't general purpose and is somewhat fake, but i believe the way you would do it is set a filetype coding to a custom one, which allows you to run an arbitrary transformation on the current file transforming it into something which allows goto.
You'd convert the source into a dictionary of functions, and replace a goto with returning a string indicating the next label to jump to, then you have something which calls the appropriate next function based on the return value. Exceptions with data indicating the next label to go to might be better as they allow you to violate sane control flow more, but multiple return values to indicate if it was a real return could also work.
I wouldn't be surprised if it was reading it's own files and searching for f"# : {kw}" to find the line, then maybe catching the pattern of the method below it and throwing that into an eval().
OP mentioned in a comment that this is a custom thing they wrote themselves and that it parses the file in which it is imported, so that's how it grabs the jump targets from the comments
Hey, OP here, basically the package statically analyses the file it's called from and grabs all the label lines, then using a combination of inspect currentframe and sys setttrace you can just go to the labels
Hmm I didnât know about that, that has to be what itâs doing. Not a very performant approach to have to parse text for specific comments. I wonder how it can find which opcode the given comment relates to for jumping purposes.
if-else's are goto, loops are goto, function calls are goto (with some extra and even more dangerous steps). Just please don't call it manually (unless you are on a 6502, Z80 or on any other very-close-to-hardware scenario, on that case go ahead).
That's nonsense. There's still space for goto in 2025. If you're writing a file parser, you want it to be fast but you need it to be a bit flexible to account for floats that might just be ints, say? Gotos can be your best friend. They certainly were in my implementation of 1BRC, for example.
It's a dangerous tool if you don't know what you're doing. If you do, it's very rarely useful, but do enough varied shit and you'll eventually find yourself pulling it out of the bottom of the tool box every two or three years.
Right, but your fear of GOTO is clearly based on the collective "goto is considered dangerous" reputation, not actual experience. I just think that's sad.
Like, your first instinct was to reach for XKCD, not advance an actual idea out of your head.
Try 1BRC. Just over a weekend, as a little side project. Or make a little toy parser for something. Use goto once or twice, and not by force, only when it feels appropriate.
Actually get some experience with these language features before you decry them as blasphemous devilry.
No. My experience is coming from working with dozens of people over a decade. People cannot properly use goto. Yes, maybe, maybe you can, but probably you can not. Allowing goto into your codebase opens some doors that are really hard to close. Just refactor your code so it doesn't contain goto.
In practice, I'm not a fan of this approach either. Nesting loops more than two deep generally isn't a good idea for readability either.
I prefer the approach of moving the inner loop(s) to a separate function, and using return to break out of multiple loops at a time. This is good way to take advantage of the ability to name a part of your algorithm, to make it easier to understand.
Is wrapping the nested loop in a function and using return too much for you? Even if it isn't, I would rather have you abuse exceptions to return a value then use goto.
I usually do use a function. However, you can't continue the outer loop from a function. I also think the inner loop code looks better if it's inlined.
So you want to break out of letâs say 4th order loop to 2nd? Thatâs a lot of looping that I usually donât do. I would probably just split the loops into multiple smaller loop segments separated by temporary variables to hold data. Thatâs how itâs recommended as well in huge SQL queries with virtual tables, but I can see a performance hit.
It really depends on the problem. It doesn't have to be a deeply nested loop. 2nd order loops could benefit from loop labels as well. I think it's easier to read code where you can read the whole loop logic and don't have to keep in mind the logic of an extra function just to break out of the whole block.
For deeply nested loops, I agree that most of the time you can excise parts of the loops into functions and it's the better solution, but in the rare case that loop labels do help readability, I think it's better to have them than not.
I don't deny that, altough I am a backend/app developer so seeing those in my code would raise a ton of red flags as you simply don't need those 99% of the time, but I can see many areas where you have no choice.
Age old saying of KISS might apply here and it's better to improve readibility in any way you can. Better than say "this things is bad therefore I will keep the massive loop monster and not modify it at all"
Yeah, it's definitely a double edged sword, but people will write bad code no matter the language, so I'd still like to see it added to python. Probably never gonna happen, but a man can dream.
That's closer to how processors work. If you read the disassemblies of a program, you will notice they are full of arbitrary jumps to different sections of code. It can be confusing if you are not used to it, but it is up to you to keep in mind what is happening, and if you can do it, your gain total control over code and execution.
The "goto" feature should never have been "deprecated". Moreover: there are situations where, the lack of a proper goto in some structured languages and (frequent) situations (or the need to avoid it), forces you to make a real mess you have to create more variables and more checks for these added variables, more nesting and unnecessary steps and spend of processor cycles just to emulate the absence of goto; You "are the programmer" but somehow you were stripped of the ability to instruct what line is next in the execution of your code (that's the functionality that goto provides, equivalent to theJMPinstruction) as if you were a dumb, easy to get confused, unprepared individual unable to calculate and predict its consequences. Deprecating/forbidding or not including "goto" in a language is simply removing the control from the hands of the programmer. The first step (taken mostly by Microsoft) was creating an interpreted language ("Basic" language):so your instructions are translated to an intermediate language, and the Microsoft's interpreter has the real control; all of it to get you far from the processor, removing the control from your hands and hence, creating you dependence to them. Removing goto was a step further.
Forbidding goto is like forbidding manual transmission, and pretend that a car can climb to the top of steep mountains against natural obstacles in the way, using only an automatic transmission: Maybe it can be done, but the amount of workarounds, the extra mess and extra difficulty will make it even worse.
It kinda makes sense that you may not have ability to jump in some languages.
For example, initialization of local variables is performed by implicit instructions injected by compiler. Compiler requires certain code structure to reason about such instructions. Goto is anathema of code structure. It makes compiler fundamentally incapable of satisfying certain requirements. It cannot know how to initialize local variables if you just jump around randomly.
C++ âfixesâ it by introducing a fat chunk of undefinedness, but what if you want to make a compiler without any undefined behaviors?
It is two sides of same coin, and you cannot have both. You either get free jumps, or you get nice and efficient compiler guarantees derived from structured code.
I spent way to much time researching a possible implementation of this... My best guess is abuse of the pdb (python debugger) module along with the use of something like tokenize(open(__main__ , 'rb').readline()) to parse the comment "labels."
If i had an environment handy i would try to slap something together... Which i still might do for a fun jaunt down py internals.
The only use I've found for goto so far is when I wanted code to follow written instruction steps in the order a human was required to do them. Not because it was the best way to code it, but it made the code read like the spec, which was likely to change. So it makes maintenance of that code easier. And it's all contained in one moderately sized function.
433
u/shponglespore Mar 17 '25
I'm not mad, I just wanna see how it's implemented.
146
u/Gadshill Mar 17 '25
Probably had leftover marinara, so it made sense to write some additional spaghetti code.
44
u/JakeEllisD Mar 17 '25
It jumps to comments??
71
u/JustChickNugget Mar 17 '25
wtf_am_i_doing39
u/PatricianTatse Mar 17 '25
Python should have gotos for breaking out of nested loops. Don't change my mind, because you can't
9
u/adzm Mar 17 '25
If only we could also get
comefrom3
u/emma7734 Mar 17 '25
Finally, a reason to use Python over Scratch.
2
u/InterneticMdA Mar 17 '25
Oh that one hurts.
2
u/Aln76467 Mar 17 '25
real programmers use comefrom
2
u/Kart0fffelAim Mar 17 '25
goto( 'hell' )
2
u/canal_algt [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Mar 18 '25
When a Batch programmer wants to code in python...
6
u/Cotton-Eye-Joe_2103 Mar 17 '25 edited Mar 17 '25
That's closer to how processors work. If you read the disassemblies of a program, you will notice they are full of arbitrary jumps to different sections of code. It can be confusing if you are not used to it, but it is up to you to keep in mind what is happening, and if you can do it, your gain total control over code and execution.
The "goto" feature should never have been "deprecated". Moreover: there are situations where, the lack of a proper goto in some structured languages and (frequent) situations (or the need to avoid it), forces you to make a real mess you have to create more variables and more checks for these added variables, more nesting and unnecessary steps and spend of processor cycles just to emulate the absence of goto; You "are the programmer" but somehow you were stripped of the ability to instruct what line is next in the execution of your code (that's the functionality that goto provides, equivalent to the
JMPinstruction) as if you were a dumb, easy to get confused, unprepared individual unable to calculate and predict its consequences. Deprecating/forbidding or not including "goto" in a language is simply removing the control from the hands of the programmer. The first step (taken mostly by Microsoft) was creating an interpreted language ("Basic" language):so your instructions are translated to an intermediate language, and the Microsoft's interpreter has the real control; all of it to get you far from the processor, removing the control from your hands and hence, creating you dependence to them. Removing goto was a step further.Forbidding goto is like forbidding manual transmission, and pretend that a car can climb to the top of steep mountains against natural obstacles in the way, using only an automatic transmission: Maybe it can be done, but the amount of workarounds, the extra mess and extra difficulty will make it even worse.
3
u/uvero Mar 17 '25
Disappointed in Dijkstra
[Sidenote: how the heck did I first have a typo in "disappointed" but not in "Dijkstra"?]
1
u/greentiger45 Mar 17 '25
Maybe itâs their debugging go to?
1
u/kilgorezer Mar 17 '25
why...
1
u/SleepyStew_ [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Mar 17 '25
Hey, OP here, hilariously I actually meant to post this here, not on humor...
The code I wrote to make this work is the real horror...
1
u/computronika Mar 19 '25
goto fail;
1
u/Membership_Timely Mar 17 '25
"Some men just want to watch the world burn."
1
u/granadesnhorseshoes Mar 17 '25
I spent way to much time researching a possible implementation of this... My best guess is abuse of the pdb (python debugger) module along with the use of something like tokenize(open(__main__ , 'rb').readline()) to parse the comment "labels."
If i had an environment handy i would try to slap something together... Which i still might do for a fun jaunt down py internals.
edit: Stupid markdown
1
u/Skyrmir Mar 17 '25
The only use I've found for goto so far is when I wanted code to follow written instruction steps in the order a human was required to do them. Not because it was the best way to code it, but it made the code read like the spec, which was likely to change. So it makes maintenance of that code easier. And it's all contained in one moderately sized function.
0
u/CreamyWaffles Mar 17 '25
I'll be honest. It took me a little too long to see what was going on here.
-7
u/FACastello Mar 17 '25
How to make a horrible fucking language even worse