Sophie

Sophie

distrib > Fedora > 14 > i386 > by-pkgid > aad95ed02015570e8e657e9b095a0226 > files > 562

python-docs-2.7-1.fc14.noarch.rpm



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Common Object Structures &mdash; Python v2.7 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.7',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.7 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.7 documentation" href="../index.html" />
    <link rel="up" title="Object Implementation Support" href="objimpl.html" />
    <link rel="next" title="Type Objects" href="typeobj.html" />
    <link rel="prev" title="Allocating Objects on the Heap" href="allocation.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="typeobj.html" title="Type Objects"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="allocation.html" title="Allocating Objects on the Heap"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.7 documentation</a> &raquo;</li>

          <li><a href="index.html" >Python/C API Reference Manual</a> &raquo;</li>
          <li><a href="objimpl.html" accesskey="U">Object Implementation Support</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="common-object-structures">
<span id="common-structs"></span><h1>Common Object Structures<a class="headerlink" href="#common-object-structures" title="Permalink to this headline">¶</a></h1>
<p>There are a large number of structures which are used in the definition of
object types for Python.  This section describes these structures and how they
are used.</p>
<p>All Python objects ultimately share a small number of fields at the beginning
of the object&#8217;s representation in memory.  These are represented by the
<a href="#id1"><span class="problematic" id="id2">:ctype:`PyObject`</span></a> and <a href="#id3"><span class="problematic" id="id4">:ctype:`PyVarObject`</span></a> types, which are defined, in turn,
by the expansions of some macros also used, whether directly or indirectly, in
the definition of all other Python objects.</p>
<p>These macros are used in the definition of <a href="#id5"><span class="problematic" id="id6">:ctype:`PyObject`</span></a> and
<a href="#id7"><span class="problematic" id="id8">:ctype:`PyVarObject`</span></a>:</p>
<p>The <tt class="xref py py-attr docutils literal"><span class="pre">ml_meth</span></tt> is a C function pointer.  The functions may be of different
types, but they always return <a href="#id9"><span class="problematic" id="id10">:ctype:`PyObject\*`</span></a>.  If the function is not of
the <a href="#id11"><span class="problematic" id="id12">:ctype:`PyCFunction`</span></a>, the compiler will require a cast in the method table.
Even though <a href="#id13"><span class="problematic" id="id14">:ctype:`PyCFunction`</span></a> defines the first parameter as
<a href="#id15"><span class="problematic" id="id16">:ctype:`PyObject\*`</span></a>, it is common that the method implementation uses a the
specific C type of the <em>self</em> object.</p>
<p>The <tt class="xref py py-attr docutils literal"><span class="pre">ml_flags</span></tt> field is a bitfield which can include the following flags.
The individual flags indicate either a calling convention or a binding
convention.  Of the calling convention flags, only <a class="reference internal" href="#METH_VARARGS" title="METH_VARARGS"><tt class="xref py py-const docutils literal"><span class="pre">METH_VARARGS</span></tt></a> and
<a class="reference internal" href="#METH_KEYWORDS" title="METH_KEYWORDS"><tt class="xref py py-const docutils literal"><span class="pre">METH_KEYWORDS</span></tt></a> can be combined (but note that <a class="reference internal" href="#METH_KEYWORDS" title="METH_KEYWORDS"><tt class="xref py py-const docutils literal"><span class="pre">METH_KEYWORDS</span></tt></a>
alone is equivalent to <tt class="docutils literal"><span class="pre">METH_VARARGS</span> <span class="pre">|</span> <span class="pre">METH_KEYWORDS</span></tt>). Any of the calling
convention flags can be combined with a binding flag.</p>
<dl class="data">
<dt id="METH_VARARGS">
<tt class="descname">METH_VARARGS</tt><a class="headerlink" href="#METH_VARARGS" title="Permalink to this definition">¶</a></dt>
<dd><p>This is the typical calling convention, where the methods have the type
<a href="#id17"><span class="problematic" id="id18">:ctype:`PyCFunction`</span></a>. The function expects two <a href="#id19"><span class="problematic" id="id20">:ctype:`PyObject\*`</span></a> values.
The first one is the <em>self</em> object for methods; for module functions, it
has the value given to <a href="#id21"><span class="problematic" id="id22">:cfunc:`Py_InitModule4`</span></a> (or <em>NULL</em> if
<a href="#id23"><span class="problematic" id="id24">:cfunc:`Py_InitModule`</span></a> was used).  The second parameter (often called
<em>args</em>) is a tuple object representing all arguments. This parameter is
typically processed using <a href="#id25"><span class="problematic" id="id26">:cfunc:`PyArg_ParseTuple`</span></a> or
<a href="#id27"><span class="problematic" id="id28">:cfunc:`PyArg_UnpackTuple`</span></a>.</p>
</dd></dl>

<dl class="data">
<dt id="METH_KEYWORDS">
<tt class="descname">METH_KEYWORDS</tt><a class="headerlink" href="#METH_KEYWORDS" title="Permalink to this definition">¶</a></dt>
<dd><p>Methods with these flags must be of type <a href="#id29"><span class="problematic" id="id30">:ctype:`PyCFunctionWithKeywords`</span></a>.
The function expects three parameters: <em>self</em>, <em>args</em>, and a dictionary of
all the keyword arguments.  The flag is typically combined with
<a class="reference internal" href="#METH_VARARGS" title="METH_VARARGS"><tt class="xref py py-const docutils literal"><span class="pre">METH_VARARGS</span></tt></a>, and the parameters are typically processed using
<a href="#id31"><span class="problematic" id="id32">:cfunc:`PyArg_ParseTupleAndKeywords`</span></a>.</p>
</dd></dl>

<dl class="data">
<dt id="METH_NOARGS">
<tt class="descname">METH_NOARGS</tt><a class="headerlink" href="#METH_NOARGS" title="Permalink to this definition">¶</a></dt>
<dd><p>Methods without parameters don&#8217;t need to check whether arguments are given if
they are listed with the <a class="reference internal" href="#METH_NOARGS" title="METH_NOARGS"><tt class="xref py py-const docutils literal"><span class="pre">METH_NOARGS</span></tt></a> flag.  They need to be of type
<a href="#id33"><span class="problematic" id="id34">:ctype:`PyCFunction`</span></a>.  When used with object methods, the first parameter is
typically named <tt class="docutils literal"><span class="pre">self</span></tt> and will hold a reference to the object instance.
In all cases the second parameter will be <em>NULL</em>.</p>
</dd></dl>

<dl class="data">
<dt id="METH_O">
<tt class="descname">METH_O</tt><a class="headerlink" href="#METH_O" title="Permalink to this definition">¶</a></dt>
<dd><p>Methods with a single object argument can be listed with the <a class="reference internal" href="#METH_O" title="METH_O"><tt class="xref py py-const docutils literal"><span class="pre">METH_O</span></tt></a>
flag, instead of invoking <a href="#id35"><span class="problematic" id="id36">:cfunc:`PyArg_ParseTuple`</span></a> with a <tt class="docutils literal"><span class="pre">&quot;O&quot;</span></tt> argument.
They have the type <a href="#id37"><span class="problematic" id="id38">:ctype:`PyCFunction`</span></a>, with the <em>self</em> parameter, and a
<a href="#id39"><span class="problematic" id="id40">:ctype:`PyObject\*`</span></a> parameter representing the single argument.</p>
</dd></dl>

<dl class="data">
<dt id="METH_OLDARGS">
<tt class="descname">METH_OLDARGS</tt><a class="headerlink" href="#METH_OLDARGS" title="Permalink to this definition">¶</a></dt>
<dd><p>This calling convention is deprecated.  The method must be of type
<a href="#id41"><span class="problematic" id="id42">:ctype:`PyCFunction`</span></a>.  The second argument is <em>NULL</em> if no arguments are
given, a single object if exactly one argument is given, and a tuple of
objects if more than one argument is given.  There is no way for a function
using this convention to distinguish between a call with multiple arguments
and a call with a tuple as the only argument.</p>
</dd></dl>

<p>These two constants are not used to indicate the calling convention but the
binding when use with methods of classes.  These may not be used for functions
defined for modules.  At most one of these flags may be set for any given
method.</p>
<dl class="data">
<dt id="METH_CLASS">
<tt class="descname">METH_CLASS</tt><a class="headerlink" href="#METH_CLASS" title="Permalink to this definition">¶</a></dt>
<dd><p id="index-0">The method will be passed the type object as the first parameter rather
than an instance of the type.  This is used to create <em>class methods</em>,
similar to what is created when using the <a class="reference internal" href="../library/functions.html#classmethod" title="classmethod"><tt class="xref py py-func docutils literal"><span class="pre">classmethod()</span></tt></a> built-in
function.</p>
<p class="versionadded">
<span class="versionmodified">New in version 2.3.</span></p>
</dd></dl>

<dl class="data">
<dt id="METH_STATIC">
<tt class="descname">METH_STATIC</tt><a class="headerlink" href="#METH_STATIC" title="Permalink to this definition">¶</a></dt>
<dd><p id="index-1">The method will be passed <em>NULL</em> as the first parameter rather than an
instance of the type.  This is used to create <em>static methods</em>, similar to
what is created when using the <a class="reference internal" href="../library/functions.html#staticmethod" title="staticmethod"><tt class="xref py py-func docutils literal"><span class="pre">staticmethod()</span></tt></a> built-in function.</p>
<p class="versionadded">
<span class="versionmodified">New in version 2.3.</span></p>
</dd></dl>

<p>One other constant controls whether a method is loaded in place of another
definition with the same method name.</p>
<dl class="data">
<dt id="METH_COEXIST">
<tt class="descname">METH_COEXIST</tt><a class="headerlink" href="#METH_COEXIST" title="Permalink to this definition">¶</a></dt>
<dd><p>The method will be loaded in place of existing definitions.  Without
<em>METH_COEXIST</em>, the default is to skip repeated definitions.  Since slot
wrappers are loaded before the method table, the existence of a
<em>sq_contains</em> slot, for example, would generate a wrapped method named
<a class="reference internal" href="../reference/datamodel.html#object.__contains__" title="object.__contains__"><tt class="xref py py-meth docutils literal"><span class="pre">__contains__()</span></tt></a> and preclude the loading of a corresponding
PyCFunction with the same name.  With the flag defined, the PyCFunction
will be loaded in place of the wrapper object and will co-exist with the
slot.  This is helpful because calls to PyCFunctions are optimized more
than wrapper object calls.</p>
<p class="versionadded">
<span class="versionmodified">New in version 2.4.</span></p>
</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="allocation.html"
                        title="previous chapter">Allocating Objects on the Heap</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="typeobj.html"
                        title="next chapter">Type Objects</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/c-api/structures.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="typeobj.html" title="Type Objects"
             >next</a> |</li>
        <li class="right" >
          <a href="allocation.html" title="Allocating Objects on the Heap"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.7 documentation</a> &raquo;</li>

          <li><a href="index.html" >Python/C API Reference Manual</a> &raquo;</li>
          <li><a href="objimpl.html" >Object Implementation Support</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2010, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Aug 09, 2010.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0b2.
    </div>

  </body>
</html>