
Python goto implemented with system trace function (2017) - luu
https://gist.github.com/georgexsh/ede5163a294ced53c3e2369ccaa392cc
======
CivBase
This uses sys._getframe()[0], a private function which appears to be
officially supported by CPython but is not guaranteed to exist in other
interpreters. Needless to say, you probably shouldn't use this in any serious
capacity, even if you're using CPython.

That said... I wonder what it would take to add support for labels. Jumping
back to a previous location would be trivial; just create a sister function to
associate the line with some unique string. But is there any way to register a
label for a line that hasn't been executed?

I know CPython normally precompiles modules instead of literally interpreting
them line-by-line, but I've never seen any code that interfaces with the
precompiler itself. Does CPython support anything like preprocessor commands
that might make it possible to support future labels?

You could easily wrap the interpreter with a custom preprocessor, but that's
boring.

[0]
[https://docs.python.org/3/library/sys.html#sys._getframe](https://docs.python.org/3/library/sys.html#sys._getframe)

~~~
kzrdude
Python being python, can't you swap out the import machinery and change which
compile function is called, alternatively call your own preprocessor before
compiling?

~~~
rcxdude
Yup. You can basically write your own language with different syntax and get
python to treat it like any other python code. Hy is a language which works
this way: [https://github.com/hylang/hy](https://github.com/hylang/hy) (see
this part of their documentation for how the user can use this:
[https://docs.hylang.org/en/master/language/interop.html#usin...](https://docs.hylang.org/en/master/language/interop.html#using-
hy-from-python))

I think goto has been implemented in a few different ways in python. Here's
another example which abuses existing syntax and edits the bytecode of the
function after its compiled: [https://pypi.org/project/goto-
statement/](https://pypi.org/project/goto-statement/)

------
nathancahill
I had to click Report Abuse on this Gist.

------
pjc50
Now do the COME FROM function from INTERCAL.

~~~
pmiller2
[http://entrian.com/goto/](http://entrian.com/goto/)

    
    
        from goto import comefrom

------
dec0dedab0de
I tried to read through this at least 4 times today, and my brain keeps going
between "Woah thats neat!" and "WTF JUST DONT DO THAT!"

------
google234123
goto to a line number is not the very nice... adding a new line will mess up
all the previous gotos

~~~
seisvelas
You're getting downvotes from the people who don't know that you are 100%
right: goto is usually with _labels_ , not line number, for exactly the reason
you point out.

This Gist is still super cool, though.

~~~
pjc50
Further back, BASIC lets you go to a line number .. but the line numbers are
explicit and not required to be consecutive. In fact you'd routinely leave
holes in the numbering in order to insert an extra line without breaking it.
Better systems offered a "renumber" function. Others .. didn't.

~~~
seisvelas
>BASIC lets you go to a line number

QBasic let you use either a line number _or_ a label, I believe they only kept
line numbers after labels were introduced for backwards compatibility.

I know I'm veering way off topic but for as much disdain as BASICs receive, I
really do feel like coding up a simple game was much easier for teenage-me in
QBasic than it was in Python with pygame, for whatever reason.

~~~
pjc50
I think a lot of people have reported having that experience, and I think it's
been deeply under-investigated by programming language researchers.

~~~
jbay808
My first programming experience was QBASIC. Even if it's not a good
programming pattern, I think GOTO is a great way to let a novice programmer
"discover" the idea of loops and functions for themselves. Although it's error
prone, it's still very intuitive for someone with no programming background.

------
calibwam
I consider this harmful.

------
DSingularity
So cute. What are some use-cases for these reflective features?

~~~
captainmuon
I once implemented the "command pattern" in order to have undo in my app.
Basically you have to ensure that you only modify your model from command
objects, and never directly from UI callbacks. To ensure this, I created a
decorator that walked the stack, and asserted that the call originated in a
subclass of `Command`. Then I wrapped all setters on the model with this
decorator. This was really helpful during development for weeding out cases
where Undo didn't work correctly. In the release, you could disable the check
so there would be no performance penalty.

------
varelaz
It's fairly easy to implement goto using exceptions. At least it will be all
pythonic (I don't think it will become less ugly). So I don't know why this
implementation has to be considered at all.

How to do this: create your ExceptionGoTo class which will be handled on high
level function and add callback as exception parameter, in catch you can call
it.

I'm not saying that it will be exactly goto to certain line, but logically it
will behave as you suppose to use goto.

------
josh_fyi
Github has a "Report abuse" button. I really need to press it :-)

