Sujet : Re: Python recompile
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 06. Mar 2025, 21:28:41
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20250306115048.606@kylheku.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2025-03-06,
Muttley@DastardlyHQ.org <
Muttley@DastardlyHQ.org> wrote:
Compilation and runtime are the same thing in Python arn't they? Can you
precompile code? I'm not an expert on the language to any extent though I
can write simple code in it.
Python is ahead-of-time compiled into byte code. Python interpretation
runs the byte code.
In some situations, Python compiles .py file into .pyc files
which are stored in a __pycache__ directory, doing this
as it executes the files.
You can force ahead-of-time file compiling "python3 -m compileall
yourprogram.py".
When you have local imports: e.g. you have a foo.py which has
"import bar", referring to bar.py, and you run "python3 foo.py",
it will compile the bar inport and cache its byte code in a
file in __pycache__.
I suspect that in cases when a .pyc file is not being dropped,
Python does incremental ahead-of-time compiling, meaning that it reads
successive top-level statements of the .py file, compiles them to
bytecode in memory, and immediately executes them, rather than compiling
the whole file first. Quite possibly, it might do this even when it
*is* generating a .pyc file, in those cases when it's doing so
automatically (the foo and bar scenario described above).
When imports are being compiled automatically to .pyc files,
their statements are being executed; like if you have a top-level
print('hello'), you will see the hello output, and you will see
a .pyc file in __pycache__ for that file. Was that hello printed
after the .pyc file was produced, or during? I suspect during.
But anyway, Python obviously has compile time separate from run-time.
Or, should we say, compile times separate from run-times.
There is a time when source is being compiled to byte code, and
that is not the same time as when that byte code is executed.
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca