[Pythonmac-SIG] Embedding Python in Cocoa

Andrew Trevorrow andrew at trevorrow.com
Tue Dec 4 00:17:45 CET 2007


Michael Terry wrote:

> Suppose you had a Cocoa program that executed Python scripts
> periodically. If global variables were set, they'd retain their values
> between script runs. What would be the best approach to make this
> happen?
> 
> Call the C APIs? If so, do I just need to initialize a Python
> interpreter when the program starts and not finalize it until the
> program closes--or will it be more complicated than that?

Yep, just call Py_Initialize once.  In fact, it's not a good idea
to call Py_Initialize and Py_Finalize multiple times because they
cause a memory leak of about 10K each time.  In our app we don't
even bother calling Py_Finalize at the end because it causes a
slight but annoying delay.

If you want to see the way we do things then go to
https://sourceforge.net/projects/golly/
and either browse CVS or download the source code and look at
wxpython.cpp.

Andrew


More information about the Pythonmac-SIG mailing list