The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by rainark]

From an MS-DOS window:
> python test.py -O
will "optimize" the python code to run faster (even just a bit faster?).

But is there a way to set the optimization within the code itself?
if __name__ == '__main__': test() -O ???

I'm hooked on Python, and I love the Quick Python book! ...especially the
interfacing with C++, Java, and HTML.

Any help appreciated,
Chris
import-bot (20211) [Avatar] Offline
#2
Re: Optimizations
[Originally posted by daryl harms]

Hi Chris,

> From an MS-DOS window:
> > python test.py -O
> will "optimize" the python code to run faster (even just a bit faster?).
>
> But is there a way to set the optimization within the code itself?
> if __name__ == '__main__': test() -O ???
>
I am not aware of a way to do this. What the interpreter does when the -O
option is specified is to use (and create/compile if necessary) ".pyo" files
rather than the normal ".pyc" files for imported modules. You can optionally
set the PYTHONOPTIMIZE environment variable to get the same effect (as it will
be read upon startup of the intepreter). I don't know of way though, to turn
this on for specified modules only.

> I'm hooked on Python, and I love the Quick Python book! ...especially the
> interfacing with C++, Java, and HTML.
>
This is great to hear!

> Any help appreciated,
> Chris