Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="ext.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="ext.html" title='Extending and Embedding the Python Interpreter' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='last' href='about.html' title='About this document...' /> | |
10 | <link rel='help' href='about.html' title='About this document...' /> | |
11 | <link rel="next" href="refcounts.html" /> | |
12 | <link rel="prev" href="parseTupleAndKeywords.html" /> | |
13 | <link rel="parent" href="intro.html" /> | |
14 | <link rel="next" href="refcounts.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>1.9 Building Arbitrary Values | |
17 | </title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="1.8 Keyword Parameters for" | |
25 | href="parseTupleAndKeywords.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="1. Extending Python with" | |
28 | href="intro.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="1.10 Reference Counts" | |
31 | href="refcounts.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Extending and Embedding the Python Interpreter</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><img src='../icons/blank.png' | |
38 | border='0' height='32' alt='' width='32' /></td> | |
39 | <td class='online-navigation'><img src='../icons/blank.png' | |
40 | border='0' height='32' alt='' width='32' /></td> | |
41 | </tr></table> | |
42 | <div class='online-navigation'> | |
43 | <b class="navlabel">Previous:</b> | |
44 | <a class="sectref" rel="prev" href="parseTupleAndKeywords.html">1.8 Keyword Parameters for</A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="intro.html">1. Extending Python with</A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="refcounts.html">1.10 Reference Counts</A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H1><A NAME="SECTION003900000000000000000"></A><A NAME="buildValue"></A> | |
55 | <BR> | |
56 | 1.9 Building Arbitrary Values | |
57 | ||
58 | </H1> | |
59 | ||
60 | <P> | |
61 | This function is the counterpart to <tt class="cfunction">PyArg_ParseTuple()</tt>. It is | |
62 | declared as follows: | |
63 | ||
64 | <P> | |
65 | <div class="verbatim"><pre> | |
66 | PyObject *Py_BuildValue(char *format, ...); | |
67 | </pre></div> | |
68 | ||
69 | <P> | |
70 | It recognizes a set of format units similar to the ones recognized by | |
71 | <tt class="cfunction">PyArg_ParseTuple()</tt>, but the arguments (which are input to the | |
72 | function, not output) must not be pointers, just values. It returns a | |
73 | new Python object, suitable for returning from a C function called | |
74 | from Python. | |
75 | ||
76 | <P> | |
77 | One difference with <tt class="cfunction">PyArg_ParseTuple()</tt>: while the latter | |
78 | requires its first argument to be a tuple (since Python argument lists | |
79 | are always represented as tuples internally), | |
80 | <tt class="cfunction">Py_BuildValue()</tt> does not always build a tuple. It builds | |
81 | a tuple only if its format string contains two or more format units. | |
82 | If the format string is empty, it returns <code>None</code>; if it contains | |
83 | exactly one format unit, it returns whatever object is described by | |
84 | that format unit. To force it to return a tuple of size 0 or one, | |
85 | parenthesize the format string. | |
86 | ||
87 | <P> | |
88 | Examples (to the left the call, to the right the resulting Python value): | |
89 | ||
90 | <P> | |
91 | <div class="verbatim"><pre> | |
92 | Py_BuildValue("") None | |
93 | Py_BuildValue("i", 123) 123 | |
94 | Py_BuildValue("iii", 123, 456, 789) (123, 456, 789) | |
95 | Py_BuildValue("s", "hello") 'hello' | |
96 | Py_BuildValue("ss", "hello", "world") ('hello', 'world') | |
97 | Py_BuildValue("s#", "hello", 4) 'hell' | |
98 | Py_BuildValue("()") () | |
99 | Py_BuildValue("(i)", 123) (123,) | |
100 | Py_BuildValue("(ii)", 123, 456) (123, 456) | |
101 | Py_BuildValue("(i,i)", 123, 456) (123, 456) | |
102 | Py_BuildValue("[i,i]", 123, 456) [123, 456] | |
103 | Py_BuildValue("{s:i,s:i}", | |
104 | "abc", 123, "def", 456) {'abc': 123, 'def': 456} | |
105 | Py_BuildValue("((ii)(ii)) (ii)", | |
106 | 1, 2, 3, 4, 5, 6) (((1, 2), (3, 4)), (5, 6)) | |
107 | </pre></div> | |
108 | ||
109 | <P> | |
110 | ||
111 | <DIV CLASS="navigation"> | |
112 | <div class='online-navigation'> | |
113 | <p></p><hr /> | |
114 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
115 | <tr> | |
116 | <td class='online-navigation'><a rel="prev" title="1.8 Keyword Parameters for" | |
117 | href="parseTupleAndKeywords.html"><img src='../icons/previous.png' | |
118 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
119 | <td class='online-navigation'><a rel="parent" title="1. Extending Python with" | |
120 | href="intro.html"><img src='../icons/up.png' | |
121 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
122 | <td class='online-navigation'><a rel="next" title="1.10 Reference Counts" | |
123 | href="refcounts.html"><img src='../icons/next.png' | |
124 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
125 | <td align="center" width="100%">Extending and Embedding the Python Interpreter</td> | |
126 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
127 | href="contents.html"><img src='../icons/contents.png' | |
128 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
129 | <td class='online-navigation'><img src='../icons/blank.png' | |
130 | border='0' height='32' alt='' width='32' /></td> | |
131 | <td class='online-navigation'><img src='../icons/blank.png' | |
132 | border='0' height='32' alt='' width='32' /></td> | |
133 | </tr></table> | |
134 | <div class='online-navigation'> | |
135 | <b class="navlabel">Previous:</b> | |
136 | <a class="sectref" rel="prev" href="parseTupleAndKeywords.html">1.8 Keyword Parameters for</A> | |
137 | <b class="navlabel">Up:</b> | |
138 | <a class="sectref" rel="parent" href="intro.html">1. Extending Python with</A> | |
139 | <b class="navlabel">Next:</b> | |
140 | <a class="sectref" rel="next" href="refcounts.html">1.10 Reference Counts</A> | |
141 | </div> | |
142 | </div> | |
143 | <hr /> | |
144 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
145 | </DIV> | |
146 | <!--End of Navigation Panel--> | |
147 | <ADDRESS> | |
148 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
149 | </ADDRESS> | |
150 | </BODY> | |
151 | </HTML> |