question about a command like 'goto ' in Python's bytecode or it's just a compiler optimization?

Tim Roberts timr at probo.com
Wed Jun 17 02:30:18 EDT 2009


higer <higerinbeijing at gmail.com> wrote:
>
>My Python version is 2.5.2; When I reading the bytecode of some pyc
>file, I always found that there are many jump command from different
>position,but to the same position. You can see this situation in
>following code(this bytecode is just from one .pyc file and I don't
>have its source .py file):
>...
>From the above bytecode,we know that line 574 is the point that many
>position jumps to.So,it just looks like the 'goto' function in C, but
>we know that there is none such function in Python.
>...
>But ,the question is, I have tried a lot of ways(e.g.for loop,while
>loop and mixed) to re-present 'goto' style bytecodes like this, but
>the result depressed me.
>So,I think maybe it is just a compiler optimization in Python2.5? I'm
>not sure,so I'm appreciated that if anyone can help me.

I can't figure out what you're asking here.  Are you annoyed that the byte
code contains GOTOs?  This is a silly thing to worry about.  The whole
purpose of a compiler is to translate a program express in high-level
constructs into a lower-level and more primitive format.  The simplest form
of an "if" statement or a "while" statement always includes unconditional
branches.

This exact same thing happens with every compiler.  Assembly languages, for
example, do not have "if/then/else" instructions, nor "while" instructions,
nor "switch" instructions.  If you write a C program with an "if"
statement, the resulting assembly program will contain a "goto" (usually
called "jump" or "branch").

Don't worry about it.
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list