Is there a goto
or any equivalent in Python to be able to jump to a specific line of code?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
you can use User-defined Exceptions to emulate
goto
example:
I wanted the same answer and I didnt want to use
goto
. So I used the following example (from learnpythonthehardway)I have my own way of doing gotos. I use separate python scripts.
If I want to loop:
file1.py
file2.py
file3.py
(NOTE: This technique only works on Python 2.x versions)
I recently wrote a function decorator that enables
goto
in Python, just like that:I'm not sure why one would like to do something like that though. That said, I'm not too serious about it. But I'd like to point out that this kind of meta programming is actual possible in Python, at least in CPython and PyPy, and not only by misusing the debugger API as that other guy did. You have to mess with the bytecode though.
For a forward Goto, you could just add:
This only helps for simple scenarios though (i.e. nesting these would get you into a mess)
A working version has been made: http://entrian.com/goto/.
Note: It was offered as an April Fool's joke. (working though)
Needless to say. Yes its funny, but DONT use it.