[Tutor] compiled Python Files ?? and a little OO

dman dsh8290@rit.edu
Sun, 9 Dec 2001 15:25:18 -0500


On Sun, Dec 09, 2001 at 10:54:10AM -0800, Frank Peavy wrote:
...
| After I ran this, I noticed that I had two new versions of F1 and F2; the 
| directory showed "Compiled Python file" as a description and they had .pyc 
| extensions. Why didn't I see this happening with my other tests? Is it 
| because the other tests imported Tkinter?

When a module is imported, the bytecodes are stored in a .pyc file.
This allows for faster loading the next time the module is imported,
if the source hasn't changed.

| I understand I can run the .pyc files, instead of the .py files. Is
| this true?

I believe you can remove the .py file from the directory and the
program would run the same.  I have never had a need to do this, so I
haven't tested it out.

| Why didn't I get a FX.pyc file also... ? The first two files compiled but 
| the third file that imported the other two didn't.

This is because ran that file directly, instead of importing it.  A
typical layout is to have a file named for the application (with no
.py extension) file that looks like

--------
#!/usr/bin/env python
import main
main.main()
--------

then have a main.py that includes a main() function[1] where the
program's logic begins.

-D


[1]  Note :  unlike C, C++, and Java there is no requirement on the
     name given, "main" just happens to be quite common (no doubt as a
     result of practice with the above languages).

-- 

"Don't use C;  In my opinion,  C is a library programming language
 not an app programming language."  - Owen Taylor (GTK+ developer)