Files
hustoj/web/python-docs/c-api/sequence.html
2025-03-09 17:10:56 +08:00

392 lines
32 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>序列协议 &#8212; Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.8.20 文档 中搜索"
href="../_static/opensearch.xml"/>
<link rel="author" title="关于这些文档" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="copyright" title="版权所有" href="../copyright.html" />
<link rel="next" title="映射协议" href="mapping.html" />
<link rel="prev" title="数字协议" href="number.html" />
<link rel="canonical" href="https://docs.python.org/3/c-api/sequence.html" />
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<nav class="nav-content" role="navigation">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Logo"/>
</a>
<div class="version_switcher_placeholder"></div>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero"
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
</svg>
<input type="text" name="q" aria-label="快速搜索"/>
<input type="submit" value="转向"/>
</form>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<h4>上一个主题</h4>
<p class="topless"><a href="number.html"
title="上一章">数字协议</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="mapping.html"
title="下一章">映射协议</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/c-api/sequence.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
这个文档所针对的是一个已不再受支持的 Python 旧版本。
你应当升级版本,并阅读
<a href="/3/c-api/sequence.html"> Python 当前稳定版本的文档</a>.
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="mapping.html" title="映射协议"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="number.html" title="数字协议"
accesskey="P">上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python/C API 参考手册</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="abstract.html" accesskey="U">抽象对象层</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="sequence-protocol">
<span id="sequence"></span><h1>序列协议<a class="headerlink" href="#sequence-protocol" title="永久链接至标题"></a></h1>
<dl class="function">
<dt id="c.PySequence_Check">
int <code class="sig-name descname">PySequence_Check</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_Check" title="永久链接至目标"></a></dt>
<dd><p>如果对象提供序列协议,函数返回 <code class="docutils literal notranslate"><span class="pre">1</span></code>,否则返回 <code class="docutils literal notranslate"><span class="pre">0</span></code>。 请注意它将为具有 <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a> 方法的 Python 类返回 <code class="docutils literal notranslate"><span class="pre">1</span></code>,除非它们是 <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> 的子类,因为在一般情况下无法确定它所支持键类型。 此函数总是会成功执行。</p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_Size">
Py_ssize_t <code class="sig-name descname">PySequence_Size</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_Size" title="永久链接至目标"></a></dt>
<dt id="c.PySequence_Length">
Py_ssize_t <code class="sig-name descname">PySequence_Length</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_Length" title="永久链接至目标"></a></dt>
<dd><p id="index-0">成功时返回序列中*o*的对象数,失败时返回``-1``. 相当于Python的``len(o)``表达式.</p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_Concat">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PySequence_Concat</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o1</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o2</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_Concat" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>成功时返回 <em>o1</em><em>o2</em> 的拼接,失败时返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code>。 这等价于 Python 表达式 <code class="docutils literal notranslate"><span class="pre">o1</span> <span class="pre">+</span> <span class="pre">o2</span></code></p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_Repeat">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PySequence_Repeat</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em>, Py_ssize_t<em> count</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_Repeat" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>返回序列对象 <em>o</em> 重复 <em>count</em> 次的结果,失败时返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code>。 这等价于 Python 表达式 <code class="docutils literal notranslate"><span class="pre">o</span> <span class="pre">*</span> <span class="pre">count</span></code></p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_InPlaceConcat">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PySequence_InPlaceConcat</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o1</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o2</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_InPlaceConcat" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>成功时返回 <em>o1</em><em>o2</em> 的拼接,失败时返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code>。 在 <em>o1</em> 支持的情况下操作将 <em>原地</em> 完成。 这等价于 Python 表达式 <code class="docutils literal notranslate"><span class="pre">o1</span> <span class="pre">+=</span> <span class="pre">o2</span></code></p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_InPlaceRepeat">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PySequence_InPlaceRepeat</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em>, Py_ssize_t<em> count</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_InPlaceRepeat" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Return the result of repeating sequence object返回序列对象 <em>o</em> 重复 <em>count</em> 次的结果,失败时返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code>。 在 <em>o</em> 支持的情况下该操作会 <em>原地</em> 完成。 这等价于 Python 表达式 <code class="docutils literal notranslate"><span class="pre">o</span> <span class="pre">*=</span> <span class="pre">count</span></code></p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_GetItem">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PySequence_GetItem</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em>, Py_ssize_t<em> i</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_GetItem" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>返回 <em>o</em> 中的第 <em>i</em> 号元素,失败时返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code>。 这等价于 Python 表达式 <code class="docutils literal notranslate"><span class="pre">o[i]</span></code></p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_GetSlice">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PySequence_GetSlice</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em>, Py_ssize_t<em> i1</em>, Py_ssize_t<em> i2</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_GetSlice" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>返回序列对象 <em>o</em><em>i1</em><em>i2</em> 的切片,失败时返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code>。 这等价于 Python 表达式 <code class="docutils literal notranslate"><span class="pre">o[i1:i2]</span></code></p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_SetItem">
int <code class="sig-name descname">PySequence_SetItem</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em>, Py_ssize_t<em> i</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *v</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_SetItem" title="永久链接至目标"></a></dt>
<dd><p>将对象 <em>v</em> 赋值给 <em>o</em> 的第 <em>i</em> 号元素。 失败时会引发异常并返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code>;成功时返回 <code class="docutils literal notranslate"><span class="pre">0</span></code>。 这相当于 Python 语句 <code class="docutils literal notranslate"><span class="pre">o[i]</span> <span class="pre">=</span> <span class="pre">v</span></code>。 此函数 <em>不会</em> 改变对 <em>v</em> 的引用。</p>
<p>如果 <em>v</em><code class="docutils literal notranslate"><span class="pre">NULL</span></code>,元素将被删除,但是此特性已被弃用,应当改用 <a class="reference internal" href="#c.PySequence_DelItem" title="PySequence_DelItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySequence_DelItem()</span></code></a></p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_DelItem">
int <code class="sig-name descname">PySequence_DelItem</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em>, Py_ssize_t<em> i</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_DelItem" title="永久链接至目标"></a></dt>
<dd><p>删除对象 <em>o</em> 的第 <em>i</em> 号元素。 失败时返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code>。 这相当于 Python 语句 <code class="docutils literal notranslate"><span class="pre">del</span> <span class="pre">o[i]</span></code></p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_SetSlice">
int <code class="sig-name descname">PySequence_SetSlice</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em>, Py_ssize_t<em> i1</em>, Py_ssize_t<em> i2</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *v</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_SetSlice" title="永久链接至目标"></a></dt>
<dd><p>将序列对象 <em>v</em> 赋值给序列对象 <em>o</em> 的从 <em>i1</em><em>i2</em> 切片。 这相当于 Python 语句 <code class="docutils literal notranslate"><span class="pre">o[i1:i2]</span> <span class="pre">=</span> <span class="pre">v</span></code></p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_DelSlice">
int <code class="sig-name descname">PySequence_DelSlice</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em>, Py_ssize_t<em> i1</em>, Py_ssize_t<em> i2</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_DelSlice" title="永久链接至目标"></a></dt>
<dd><p>删除序列对象 <em>o</em> 的从 <em>i1</em><em>i2</em> 的切片。 失败时返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code>。 这相当于 Python 语句 <code class="docutils literal notranslate"><span class="pre">del</span> <span class="pre">o[i1:i2]</span></code></p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_Count">
Py_ssize_t <code class="sig-name descname">PySequence_Count</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_Count" title="永久链接至目标"></a></dt>
<dd><p>返回 <em>value</em><em>o</em> 中出现的次数,即返回使得 <code class="docutils literal notranslate"><span class="pre">o[key]</span> <span class="pre">==</span> <span class="pre">value</span></code> 的键的数量。 失败时返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code>。 这相当于 Python 表达式 <code class="docutils literal notranslate"><span class="pre">o.count(value)</span></code></p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_Contains">
int <code class="sig-name descname">PySequence_Contains</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_Contains" title="永久链接至目标"></a></dt>
<dd><p>确定 <em>o</em> 是否包含 <em>value</em>。 如果 <em>o</em> 中的某一项等于 <em>value</em>,则返回 <code class="docutils literal notranslate"><span class="pre">1</span></code>,否则返回 <code class="docutils literal notranslate"><span class="pre">0</span></code>。 出错时,返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code>。 这相当于 Python 表达式 <code class="docutils literal notranslate"><span class="pre">value</span> <span class="pre">in</span> <span class="pre">o</span></code></p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_Index">
Py_ssize_t <code class="sig-name descname">PySequence_Index</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_Index" title="永久链接至目标"></a></dt>
<dd><p>返回第一个索引*i*,其中 <code class="docutils literal notranslate"><span class="pre">o[i]</span> <span class="pre">==</span> <span class="pre">value</span></code>.出错时,返回 <code class="docutils literal notranslate"><span class="pre">-1</span></code>.相当于Python的``o.index(value)``表达式.</p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_List">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PySequence_List</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_List" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>返回一个列表对象,其内容与序列或可迭代对象 <em>o</em> 相同,失败时返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code>。 返回的列表保证是一个新对象。 这等价于 Python 表达式 <code class="docutils literal notranslate"><span class="pre">list(o)</span></code></p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_Tuple">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PySequence_Tuple</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_Tuple" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p id="index-1">返回一个元组对象,其内容与序列或可迭代对象 <em>o</em> 相同,失败时返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code>。 如果 <em>o</em> 为元组,则将返回一个新的引用,在其他情况下将使用适当的内容构造一个元组。 这等价于 Python 表达式 <code class="docutils literal notranslate"><span class="pre">tuple(o)</span></code></p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_Fast">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PySequence_Fast</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em>, const char<em> *m</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_Fast" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>将序列或可迭代对象 <em>o</em> 作为其他 <code class="docutils literal notranslate"><span class="pre">PySequence_Fast*</span></code> 函数族可用的对象返回。 如果该对象不是序列或可迭代对象,则会引发 <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> 并将 <em>m</em> 作为消息文本。 失败时返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code></p>
<p><code class="docutils literal notranslate"><span class="pre">PySequence_Fast*</span></code> 函数之所以这样命名,是因为它们会假定 <em>o</em> 是一个 <a class="reference internal" href="tuple.html#c.PyTupleObject" title="PyTupleObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyTupleObject</span></code></a><a class="reference internal" href="list.html#c.PyListObject" title="PyListObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyListObject</span></code></a> 并直接访问 <em>o</em> 的数据字段。</p>
<p>作为 CPython 的实现细节,如果 <em>o</em> 已经是一个序列或列表,它将被直接返回。</p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_Fast_GET_SIZE">
Py_ssize_t <code class="sig-name descname">PySequence_Fast_GET_SIZE</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_Fast_GET_SIZE" title="永久链接至目标"></a></dt>
<dd><p><em>o</em><a class="reference internal" href="#c.PySequence_Fast" title="PySequence_Fast"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySequence_Fast()</span></code></a> 返回且 <em>o</em> 不为 <code class="docutils literal notranslate"><span class="pre">NULL</span></code> 的情况下返回 <em>o</em> 的长度。 也可以通过在 <em>o</em> 上调用 <a class="reference internal" href="#c.PySequence_Size" title="PySequence_Size"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySequence_Size()</span></code></a> 来获取大小,但是 <a class="reference internal" href="#c.PySequence_Fast_GET_SIZE" title="PySequence_Fast_GET_SIZE"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySequence_Fast_GET_SIZE()</span></code></a> 速度更快,因为它可以假定 <em>o</em> 为列表或元组。</p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_Fast_GET_ITEM">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PySequence_Fast_GET_ITEM</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em>, Py_ssize_t<em> i</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_Fast_GET_ITEM" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: Borrowed reference.</em><p><em>o</em><a class="reference internal" href="#c.PySequence_Fast" title="PySequence_Fast"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySequence_Fast()</span></code></a> 返回且 <em>o</em><code class="docutils literal notranslate"><span class="pre">NULL</span></code>,并且 <em>i</em> d在索引范围内的情况下返回 <em>o</em> 的第 <em>i</em> 号元素。</p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_Fast_ITEMS">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>** <code class="sig-name descname">PySequence_Fast_ITEMS</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_Fast_ITEMS" title="永久链接至目标"></a></dt>
<dd><p>返回 PyObject 指针的底层数组。 假设 <em>o</em><a class="reference internal" href="#c.PySequence_Fast" title="PySequence_Fast"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySequence_Fast()</span></code></a> 返回且 <em>o</em> 不为 <code class="docutils literal notranslate"><span class="pre">NULL</span></code></p>
<p>请注意,如果列表调整大小,重新分配可能会重新定位items数组.因此,仅在序列无法更改的上下文中使用基础数组指针.</p>
</dd></dl>
<dl class="function">
<dt id="c.PySequence_ITEM">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PySequence_ITEM</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *o</em>, Py_ssize_t<em> i</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PySequence_ITEM" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>返回 <em>o</em> 的第 <em>i</em> 个元素或在失败时返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code>。 此形式比 <a class="reference internal" href="#c.PySequence_GetItem" title="PySequence_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySequence_GetItem()</span></code></a> 理馔,但不会检查 <em>o</em> 上的 <a class="reference internal" href="#c.PySequence_Check" title="PySequence_Check"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySequence_Check()</span></code></a> 是否为真值,也不会对负序号进行调整。</p>
</dd></dl>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h4>上一个主题</h4>
<p class="topless"><a href="number.html"
title="上一章">数字协议</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="mapping.html"
title="下一章">映射协议</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/c-api/sequence.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="mapping.html" title="映射协议"
>下一页</a> |</li>
<li class="right" >
<a href="number.html" title="数字协议"
>上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python/C API 参考手册</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="abstract.html" >抽象对象层</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
<br />
This page is licensed under the Python Software Foundation License Version 2.
<br />
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br />
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
<br />
最后更新于 12月 09, 2024.
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
</div>
</body>
</html>