5 ms·
Python goto implemented with system trace function (2017)
- DSingularity 7y agoSo cute. What are some use-cases for these reflective features?
- amelius 7y agoTranspiled code?
- oefrha 7y agoOne obvious use case invoked all the time: tracebacks are generated through introspection. https://github.com/python/cpython/blob/3.8/Lib/traceback.py https://github.com/python/cpython/blob/3.8/Lib/traceback.py
- captainmuon 7y agoI 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.
- riazrizvi 7y agoPrototyping condensed state machines. Probably not the code you'd leave in the release version, but can provide an elegant form of experimentation.
- nathancahill 7y agoI had to click Report Abuse on this Gist.
- google234123 7y agogoto to a line number is not the very nice... adding a new line will mess up all the previous gotos
- seisvelas 7y agoYou'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 7y agoFurther 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 7y ago>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 7y agoI think a lot of people have reported having that experience, and I think it's been deeply under-investigated by programming language researchers.
- jbay808 7y agoMy 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 7y agoI consider this harmful.
- pjc50 7y agoNow do the COME FROM function from INTERCAL.
- pmiller2 7y agohttp://entrian.com/goto/ http://entrian.com/goto/ from goto import comefrom
- CivBase 7y agoThis 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 7y agoPython 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 7y agoYup. 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#using-hy-from-python https://docs.hylang.org/en/master/language/interop.html#usin...) 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/
- sitkack 7y agohttps://github.com/lihaoyi/macropy https://github.com/lihaoyi/macropy
- 7y ago
- josh_fyi 7y agoGithub has a "Report abuse" button. I really need to press it :-)
- dec0dedab0de 7y agoI 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!"
- varelaz 7y agoIt'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.