Why python compile the source to bytecode before interpreting?

Why python compile the source to bytecode before interpreting?

Why not interpret from the source directly?


Asked by: Sienna472 | Posted: 06-12-2021






Answer 1

Nearly no interpreter really interprets code directly, line by line – it's simply too inefficient. Almost all interpreters use some intermediate representation which can be executed easily. Also, small optimizations can be performed on this intermediate code.

Python furthermore stores this code which has a huge advantage for the next time this code gets executed: Python doesn't have to parse the code anymore; parsing is the slowest part in the compile process. Thus, a bytecode representation reduces execution overhead quite substantially.

Answered by: Connie976 | Posted: 07-01-2022



Answer 2

Because you can compile to a .pyc once and interpret from it many times.

So if you're running a script many times you only have the overhead of parsing the source code once.

Answered by: Maya148 | Posted: 07-01-2022



Answer 3

Because interpretting from bytecode directly is faster. It avoids the need to do lexing, for one thing.

Answered by: Patrick603 | Posted: 07-01-2022



Answer 4

Re-lexing and parsing the source code over and over, rather than doing it just once (most often on the first import), would obviously be a silly and pointless waste of effort.

Answered by: Melissa549 | Posted: 07-01-2022



Answer 5

Although there is a small efficiency aspect to it (you can store the bytecode on disk or in memory), its mostly engineering: it allows you separate parsing from interpreting. Parsers can often be nasty creatures, full of edge-cases and having to conform to esoteric rules like using just the right amount of lookahead and resolving shift-reduce problems. By contrast, interpreting is really simple: its just a big switch statement using the bytecode's opcode.

Answered by: Chester568 | Posted: 07-01-2022



Answer 6

I doubt very much that the reason is performance, albeit be it a nice side effect. I would say that it's only natural to think a VM built around some high-level assembly language would be more practical than to find and replace text in some source code string.

Edit:

Okay, clearly, who ever put a -1 vote on my post without leaving a reasonable comment to explain knows very little about virtual machines (run-time environments).

http://channel9.msdn.com/shows/Going+Deep/Expert-to-Expert-Erik-Meijer-and-Lars-Bak-Inside-V8-A-Javascript-Virtual-Machine/

Answered by: Tara281 | Posted: 07-01-2022



Similar questions

compiler construction - Checking Python code correctness

In C++ I have compiler that tell me if something wrong with my code after refactoring. How to make sure that Python code is at least correct after changes? There may be some stupid error like wrong function name etc. that pretty easy to find in compile time. Thanks


compiler construction - Does Python have a "compile only" switch like Perl's -c?

Perl has the -c switch to compile the code without running it. This is convenient for debugging compile errors in Perl. Does Python have a similar switch?


compiler construction - Easy Python ASync. Precompiler?

imagine you have an io heavy function like this: def getMd5Sum(path): with open(path) as f: return md5(f.read()).hexdigest() Do you think Python is flexible enough to allow code like this (notice the $): def someGuiCallback(filebutton): ... path = filebutton.getPath() md5sum = $getMd5Sum() showNotification("Md5Sum of file: %s" % md5sum) ...


How to find class of bound method during class construction in Python 3.1?

i want to write a decorator that enables methods of classes to become visible to other parties; the problem i am describing is, however, independent of that detail. the code will look roughly like this: def CLASS_WHERE_METHOD_IS_DEFINED( method ): ??? def foobar( method ): print( CLASS_WHERE_METHOD_IS_DEFINED( method ) ) class X: @foobar def f( self, x ): return x ** 2 my pr...


python - Inner Classes: How can I get the outer-class object at construction time?

Consider the following Python (runs in 2.x or 3.x): class Outer(object): pass class Inner(object): def __init__(self): print("Inner.self", self) o = Outer() i = o.Inner() I want to get my hands on o while inside Inner.__init__(). But: I don't want o to be an explicit parameter to Inner. I want


compiler construction - Is there a standard lexer/parser tool for Python?

Closed. This question does not meet Stack Overflow guid...


compiler construction - install python modules on shared web hosting

I am using a shared hosting environment that will not give me access to the command line. Can I download the python module on my computer, compile it using python setup.py installand then simply upload a .py file to the web host? If yes, where does the install statement place the compiled file?


compiler construction - assign operator to variable in python?

Usual method of applying mathematics to variables is a * b Is it able to calculate and manipulate two operands like this? a = input('enter a value') b = input('enter a value') op = raw_input('enter a operand') Then how do i connect op and two variables a and b? I know I can compare op to +, -,


python - Setting attributes of a class during construction from **kwargs

Python noob here, Currently I'm working with SQLAlchemy, and I have this: from __init__ import Base from sqlalchemy.schema import Column, ForeignKey from sqlalchemy.types import Integer, String from sqlalchemy.orm import relationship class User(Base): __tablename__ = "users" id = Column(Integer, primary_key=True) username = Column(String, unique=True) email = Column(String) pass...


Question about python construction

A friend of mine that is a programmer told me that "Python is written in Python" or something like that. He meant that Python interpreter is written in Python (I think). I've read in some websites that Python interpret in real time ANY programming language (even C++ and ASM). Is this true? Could someone explain me HOW COULD IT BE? The unique explanation that I came up with after thinking a bit is: python is...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top