⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 125.html

📁 Python Ebook Python&XML
💻 HTML
字号:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -&gt; Embedding</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> &gt; <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> &gt; <a href="117.html" class="navtitle">6. Extending and Embedding Python</a> &gt; <span class="nonavtitle">Embedding</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="124.html" title="Other Wrappers"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=125" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="125.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="126.html" title="Summary"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A36%3A07+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162147034164216207026027225</font><a href="read7.asp?bookname=0672319942&snode=125&now=5%2F31%2F2002+4%3A36%3A07+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT><h3>Embedding</h3>
				<p>We will now talk about how to embed Python inside other programs. Python offers a clean interface that allows embedding to occur.</p>

				<P>You might be asking yourself why would you want to do it. Well, the answer is quite simple; as a scripting language, Python can wire its interpreter into other programs to enable you to make calls to specific Python functions and execute particular Python statements from them.</P>

				<P>Those programs will have the capability to load Python scripts and execute Python services that belong to specific Python modules. You can also call Python functions directly from your C code and access the Python objects that are returned by them.</P>

				<p>In order to embed Python inside a program, you just need to use the Python API梩he Python <tt cLASS="monofont">EXE</tt> is not necessary.<a nAME="idx1073744114"></A><a name="idx1073744115"></a><a name="idx1073744116"></a><a name="idx1073744117"></a><a nAme="idx1073744118"></A><a naMe="idx1073744119"></a><a nAme="idx1073744120"></a><A NAMe="idx1073744121"></a><a nAME="idx1073744122"></A><a namE="idx1073744123"></A><A Name="idx1073744124"></a><A NAMe="idx1073744125"></a></p>

				<h4>Implementing Callback Functions</h4>
					<p>Embedding Python allows you to access and use the Python interpreter from inside your application. But what happens if you need to call back your application functions from inside Python?</p>

					<p>For this reason, it is a good practice to provide a module written in C that exposes an API related to the application. Therefore, when embedding Python within your routines, you can make your application communicate both ways with your Python program by accessing the Python extension modules.<a name="idx1073744126"></a><a name="idx1073744127"></a><A naMe="idx1073744128"></a></p>

				
				<H4>Embedding the Python Interpreter</h4>
					<p>The next example adds Python functionality to a C program.</p>

					<pRe>
						
// File: embedding.c

#include &lt;stdio.h&gt;
#include &lt;Python.h&gt;
int main(int argc, char **argv)
{
  Py_Initialize();
  PyRun_SimpleString("print 'Hello Python World'");
  printf("You are my visitor number %i", args);
  Py_Finalize();
  return(0);
}

					</prE>

					<P>Python provides a set of function calls that provide an interface to the Python interpreter. The most important ones are</P>

					<Ul>
<li><p><TT CLass="monofont">Py_Initialize

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -