Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6227999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:18:48+00:00 2026-05-24T09:18:48+00:00

I must use goto in Python. I found entrians goto but my Python implementation

  • 0

I must use goto in Python. I found entrians goto but my Python implementation (CPython 2.7.1 on Mac) does not have this module, so it doesn’t seem to be portable. It should at least work in all Python implementations which support CPython bytecode (esp. I care about CPython and PyPy). Then there is this related question, and cdjc’s goto. And the ones given by answers below.

I could go and build up the bytecode manually (i.e. write my own Python compiler) because there is such an instruction (JUMP_ABSOLUTE and friends). But I wonder if there is an easier way. Is it possible via inspect or so to call a single bytecode instruction? I also thought about compiling via Python and then automatically patching the generated Python bytecode.


Of course, people will ask why and will not give me any helpful answer if I don’t explain why I really really need this. So in short my use case: I am translating a C AST to Python AST and compiling this. I can map every logical flow (all the loops and other stuff) in some way to equivalent Python code. Everything except goto. Related projects: PyCParser (see interpreter.py), PyCPython, PyLua.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:18:49+00:00Added an answer on May 24, 2026 at 9:18 am

    I know what everybody is thinking:

    xkcd GOTO

    However, there might be some didactic cases where you actually need a goto.

    This python recipe provides the goto command as a function decorator.

    The goto decorator (Python recipe by Carl Cerecke)

    This is the recipe for you if you are sick of the slow speed of the
    existing goto module http://entrian.com/goto/. The goto in this
    recipe is about 60x faster and is also cleaner (abusing sys.settrace
    seems hardly pythonic). Because this is a decorator, it alerts the
    reader which functions use goto. It does not implement the comefrom
    command, although it is not difficult to extend it to do so (exercise
    for the reader). Also, computed gotos aren’t supported; they’re not
    pythonic.

    • Use dis.dis(fn) to show the bytecode disassembly of a function.
    • The bytecodes of a function are accessed by fn.func_code.co_code.
      This is readonly so:
    • The decorated function is created exactly the same as the old one,
      but with the bytecode updated to obey the goto commands.
    • This is 2.x only; the new module is not in python 3.x (another
      exercise for the reader!)

    Usage

    @goto
    def test1(n):
        s = 0
    
        label .myLoop
    
        if n <= 0:
            return s
        s += n
        n -= 1
    
        goto .myLoop
    
    >>> test1(10)
    55
    

    Update

    Here’re two additional implementations compatible with Python 3:

    • https://github.com/cdjc/goto
    • https://github.com/snoack/python-goto
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read that to prevent SQL Injection one must use PreparedStatement. Does that
FxCop must base itself on some internal dictionary. Does it use Word dictionaries if
This must be a classic .NET question for anyone migrating from Java. .NET does
Just wondering if this is considered a clear use of goto in C#: IDatabase
I have a situation where I must use Windows API to retrieve text from
I have an iPhone app that must use external cloud db to sync data
There is a particular website I must use for work which is absolutely heinous
On some Microsoft Access queries, I get the following message: Operation must use an
Requirements: Must be able to use C strings as well as C++ strings Fast
I must be getting daft, but I can't seem to find how to read

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.