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

📄 125.html

📁 国外python经典教材,python爱好者的首选
💻 HTML
📖 第 1 页 / 共 2 页
字号:

<!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 Essential Reference, Second Edition -&gt; Converting Data from Python to C</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyM.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="2.html" class="navtitle">Linux/Unix</a> &gt; <a href="0735710910.html" class="navtitle">Python Essential Reference, Second Edition</a> &gt; <a href="121.html" class="navtitle">B. Extending and Embedding Python</a> &gt; <span class="nonavtitle">Converting Data from Python to C</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="Compilation of Extensions"><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=0735710910&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="Converting Data from C to Python"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+9%3A09%3A04+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238111161105095080112008053028057</font><a href="read5.asp?bookname=0735710910&snode=125&now=5%2F28%2F2002+9%3A09%3A04+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Converting Data from Python to C</h3>
<p>The following functions are used to convert arguments passed from Python to C.</p>

<PRE>

<B>int PyArg_ParseTuple(PyObject *</b> <b><i>args</i></B><B>, char *</B> <B><i>format</i></b><b>, ...);</B> </PRE>

<p>Parses a tuple of objects in <i><tt class="monofont">args</tt></i>
  into a series of C variables. <i><tt class="monofont">format</Tt></i>
  is a format string containing zero or more of the specifier strings from Table B.1, which describes the expected contents of <I><tt cLass="monofont">args</tT></i>
. All the remaining arguments contain the addresses of C variables into which the results will be placed. The order and types of these arguments must match the specifiers used in <i><tT CLAss="monofont">format</tt></I>
  and use the C datatypes listed in <A HRef="125#2.html">Table B.1</a>. Zero is returned if the arguments could not be parsed.</p>

<PRE>

<B>int PyArg_ParseTupleAndKeywords(PyObject *</b> <b><i>args</i></B><B>, PyObject *</B> <B><i>kwdict</i></b><b>,</b> 
                                <b>char *</b> <b><i>format</i></b><b>, char **</b> <b><i>kwlist</i></b><b>, ...);</b> </pRe>

<p>Parses both a tuple of arguments and a dictionary containing keyword arguments contained in <I><tt cLass="monofont">kwdict</tT></i>
  . <i><tT CLAss="monofont">format</tt></I>
 has the same meaning as for <TT Class="monofont">PyArg_ParseTuple()</TT>. The only difference is that <I><Tt claSS="monofont">kwlist</TT></i>
  is a null-terminated list of strings containing the names of all the arguments. Returns <tt class="monofont">1</tt>  on success, <tt class="monofont">0</tt> on error.</p>

<P><i>Note:</i> In Table B.1, results of a conversion (for example, in <Tt clAss="monofont">char *</tt><I><tt cLASS="monofont">r</tt></i>
 or <tT CLAss="monofont">char **</tt><I><TT Class="monofont">r</TT></I>
) are always placed in the parameter labeled <I><tt class="monofont">r</tt></i>
. When applicable, a length is stored in <i><tt class="monofont">len</tt></I>
.</p>

<a Name="2"></A><p><tabLe boRDER="1" cellSPACing="0" cELLPaddiNG="1" WIdth="100%">
<caption><h5>Table燘.1.  Base Format Specifiers and Associated C Datatypes for <tt class="monofont">PyArg_Parse*</tt></H5></caPtioN><colgRoup SPAN="3">
<tr>
<th VALIgn="top">
<foNT SIze="2">
<p><b>Format</B></P>
</FOnt></th>
<th valign="top">
<font size="2">
<P><b>Python Type</b></P>
</fonT></th>
<th ValiGN="top">
<FOnt siZE="2">
<P><B>C Type</b></p>
</foNT></TH>
</tr>
<tr>
<TD VAlign="top">
<font size="2">
<p><tt class="monofont">"s"</Tt></p>
</Font></Td>
<td vAligN="top">
<FONt sizE="2">
<P>String or Unicode</P>
</Font></tD>
<TD ValigN="top">
<FONt size="2">
<p><tt class="monofont">char **</tt><i><tt clAss="monofont">r</Tt></i>
</p>
</Font></tD>
</tr>
<tR>
<TD ValigN="top">
<FONt sizE="2">
<P><TT clasS="monofont">"s#"</TT></P>
</font></td>
<td valign="top">
<font siZe="2">
<p>String or Unicode</P>
</fonT></td>
<td ValiGN="top">
<FOnt siZE="2">
<P><Tt claSS="monofont">char **</TT><i><tt cLASS="monofont">r</tt></i>
<tt class="monofont">, int *</tt><i><tt class="monofont">len</Tt></i>
</P>
</fonT></td>
</tr>
<Tr>
<td VALIgn="top">
<foNT SIze="2">
<p><tT CLAss="monofont">"z"</tt></P>
</FONt></td>
<td valign="top">
<font size="2">
<p>String, Unicode, or None</P>
</foNt></td>
<Td valIgn="top">
<fONT Size="2">
<p><TT CLass="monofont">char **</tT><I><TT clasS="monofont">r</TT></I>
</p>
</font></td>
</tr>
<tr>
<td valign="top">
<fOnt Size="2">
<P><tt clAss="monofont">"z#"</tT></P>
</FOnt></td>
<TD VAlign="top">
<FONT size="2">
<P>String, Unicode, or None</P>
</FOnt></td>
<td valign="top">
<font size="2">
<P><tt ClasS="monofont">char **r, int *</tt><i><tT claSS="monofont">len</TT></i>
</p>
</foNT></TD>
</tr>
<tr>
<TD VAlign="top">
<FONT size="2">
<p><tt class="monofont">"u"</tt></p>
</font></tD>
<td ValiGn="top">
<fonT sizE="2">
<P>Unicode</P>
</Font></tD>
<TD ValigN="top">
<FONt sizE="2">
<P><TT class="monofont">Py_UNICODE **</tt><i><tt class="monofont">r</tt></i>
</p>
</fOnt></Td>
</tr>
<Tr>
<td vAligN="top">
<FONt sizE="2">
<P><TT clasS="monofont">"u#"</TT></P>
</font></TD>
<TD valign="top">
<font size="2">
<p>Unicode</p>
</font></Td>
<tD valIgn="top">
<foNt siZE="2">
<P><Tt claSS="monofont">Py_UNICODE **</TT><i><tt cLASS="monofont">r</tt></i>
<tT CLAss="monofont">, int *</tt><i><tt class="monofont">len</tt></i>
</p>
</font></Td>
</tR>
<tr>
<tD valiGn="top">
<foNT SIze="2">
<p><tT CLAss="monofont">"es"</tt></P>
</FONt></td>
<tD VALign="top">
<font size="2">
<p>String, Unicode, or buffer</p>
</font></td>
<tD vaLign="top">
<Font sIze="2">
<p><TT CLass="monofont">const char *</tT><I><TT clasS="monofont">enc</TT></I>
<tt clASS="monofont">, char **</Tt><i><tt class="monofont">r</tt></i>
</p>
</font></td>
</tR>
<tr>
<Td vaLign="top">
<fOnt sIZE="2">
<P><tt clASS="monofont">"es#"</Tt></p>
</foNT></TD>
<td vaLIGN="top">
<font size="2">
<p>String, Unicode, or buffer</p>
</font></td>
<td vaLigN="top">
<fonT size="2">
<P><tt cLASS="monofont">const char *</tt><i><tT CLAss="monofont">enc</tt></I>
<TT Class="monofont">, char **</TT><I><Tt class="monofont">r</tt></i>
<tt class="monofont">, int *</tt><i><tT clAss="monofont">len</tT></i>
</p>
</foNt></td>
</TR>
<TR>
<td vaLIGN="top">
<font SIZE="2">
<p><tt cLASS="monofont">"b"</tt></p>
</font></td>
<td valign="top">
<fonT siZe="2">
<p>Integer</p>
</Font></tD>
<td vALIGn="top">
<fonT SIZe="2">
<p><tt CLASs="monofont">char *</tt><i><TT CLass="monofont">r</tt></i>
</p>
</font></td>
</tr>
<tr>
<td vAliGn="top">
<foNt sizE="2">
<p><tt CLASs="monofont">"h"</tt></p>
</FONT></td>
<td VALIgn="top">
<foNT SIze="2">
<p>Integer</p>
</font></td>
<td valign="top">
<foNt sIze="2">
<p><Tt claSs="monofont">short *</tt><I><TT Class="monofont">r</TT></I>
</P>
</font></TD>
</TR>
<tr>
<td VALIgn="top">
<font size="2">
<p><tt class="monofont">"i"</tt></P>
</foNt></td>
<Td valIgn="top">
<fONT Size="2">
<p>Integer</P>
</FONt></td>
<tD VALign="top">
<fONT Size="2">
<p><tt class="monofont">int *</tt><i><tt clasS="monofont">r</tt></I>
</p>
</foNt></td>
</tR>
<tr>
<tD VALign="top">
<fONT Size="2">
<p><TT CLass="monofont">"l"</tT></P>
</FOnt></td>
<td valign="top">
<font size="2">
<P>Integer</p>
</fOnt></tD>
<td vaLign="top">
<FONT size="2">
<P><TT Class="monofont">long int *</TT><I><Tt claSS="monofont">r</TT></i>
</p>
</font></td>
</tr>
<tr>
<td valign="top">
<FonT sizE="2">
<p><tt cLass="monofont">"c"</TT></P>
</Font></tD>
<TD ValigN="top">
<FONt sizE="2">
<P>String of length 1</P>
</Font></td>
<td valign="top">
<font sizE="2">
<p><tT claSs="monofont">char *</tt><i><Tt clASS="monofont">r</Tt></i>
</p>
</fONT></Td>
</tr>
<tR>
<TD ValigN="top">
<FONt size="2">
<p><tt class="monofont">"f"</tt></p>
</font></Td>
<tD valIgn="top">
<foNt siZE="2">
<P>Float</P>
</font></TD>
<TD valiGN="top">
<FOnt siZE="2">
<P><Tt class="monofont">float *</tt><i><tt class="monofont">r</tt></i>
</p>
</FonT></td>
</tR>
<tr>
<td ValiGN="top">
<FOnt siZE="2">
<P><Tt claSS="monofont">"d"</TT></p>
</fonT></TD>
<Td valign="top">
<font size="2">
<p>Float</p>
</fonT></td>
<Td vaLign="top">
<fOnt sIZE="2">
<P><tt clASS="monofont">double *</Tt><i><tt CLASs="monofont">r</tt></i>
</P>
</FONt></td>
</tr>
<tr>

⌨️ 快捷键说明

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