Files
google_speech_rs/docs/pyo3/prelude/trait.PyModuleMethods.html
T
2025-04-02 13:41:38 +00:00

145 lines
26 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Implementation of functionality for `PyModule`."><title>PyModuleMethods in pyo3::prelude - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../static.files/rustdoc-46132b98.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="pyo3" data-themes="" data-resource-suffix="" data-rustdoc-version="1.85.1 (4eb161250 2025-03-15)" data-channel="1.85.1" data-search-js="search-75f5ac3e.js" data-settings-js="settings-0f613d39.js" ><script src="../../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-044be391.svg"></head><body class="rustdoc trait"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../pyo3/index.html">pyo3</a><span class="version">0.24.1</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">PyModule<wbr>Methods</a></h2><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.add" title="add">add</a></li><li><a href="#tymethod.add_class" title="add_class">add_class</a></li><li><a href="#tymethod.add_function" title="add_function">add_function</a></li><li><a href="#tymethod.add_submodule" title="add_submodule">add_submodule</a></li><li><a href="#tymethod.add_wrapped" title="add_wrapped">add_wrapped</a></li><li><a href="#tymethod.dict" title="dict">dict</a></li><li><a href="#tymethod.filename" title="filename">filename</a></li><li><a href="#tymethod.gil_used" title="gil_used">gil_used</a></li><li><a href="#tymethod.index" title="index">index</a></li><li><a href="#tymethod.name" title="name">name</a></li></ul><h3><a href="#dyn-compatibility">Dyn Compatibility</a></h3><h3><a href="#implementors">Implementors</a></h3></section><div id="rustdoc-modnav"><h2><a href="index.html">In pyo3::<wbr>prelude</a></h2></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="../index.html">pyo3</a>::<wbr><a href="index.html">prelude</a></span><h1>Trait <span class="trait">PyModuleMethods</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../../src/pyo3/types/module.rs.html#209-426">Source</a> </span></div><pre class="rust item-decl"><code>pub trait PyModuleMethods&lt;'py&gt;: Sealed {
// Required methods
fn <a href="#tymethod.dict" class="fn">dict</a>(&amp;self) -&gt; <a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyDict.html" title="struct pyo3::types::PyDict">PyDict</a>&gt;;
<span class="item-spacer"></span> fn <a href="#tymethod.index" class="fn">index</a>(&amp;self) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyList.html" title="struct pyo3::types::PyList">PyList</a>&gt;&gt;;
<span class="item-spacer"></span> fn <a href="#tymethod.name" class="fn">name</a>(&amp;self) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyString.html" title="struct pyo3::types::PyString">PyString</a>&gt;&gt;;
<span class="item-spacer"></span> fn <a href="#tymethod.filename" class="fn">filename</a>(&amp;self) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyString.html" title="struct pyo3::types::PyString">PyString</a>&gt;&gt;;
<span class="item-spacer"></span> fn <a href="#tymethod.add" class="fn">add</a>&lt;N, V&gt;(&amp;self, name: N, value: V) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.unit.html">()</a>&gt;
<span class="where">where N: <a class="trait" href="../conversion/trait.IntoPyObject.html" title="trait pyo3::conversion::IntoPyObject">IntoPyObject</a>&lt;'py, Target = <a class="struct" href="../types/struct.PyString.html" title="struct pyo3::types::PyString">PyString</a>&gt;,
V: <a class="trait" href="../conversion/trait.IntoPyObject.html" title="trait pyo3::conversion::IntoPyObject">IntoPyObject</a>&lt;'py&gt;</span>;
<span class="item-spacer"></span> fn <a href="#tymethod.add_class" class="fn">add_class</a>&lt;T&gt;(&amp;self) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.unit.html">()</a>&gt;
<span class="where">where T: <a class="trait" href="../pyclass/trait.PyClass.html" title="trait pyo3::pyclass::PyClass">PyClass</a></span>;
<span class="item-spacer"></span> fn <a href="#tymethod.add_wrapped" class="fn">add_wrapped</a>&lt;T&gt;(
&amp;self,
wrapper: &amp;impl <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/ops/function/trait.Fn.html" title="trait core::ops::function::Fn">Fn</a>(<a class="struct" href="../marker/struct.Python.html" title="struct pyo3::marker::Python">Python</a>&lt;'py&gt;) -&gt; T,
) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.unit.html">()</a>&gt;
<span class="where">where T: IntoPyCallbackOutput&lt;'py, <a class="type" href="../type.PyObject.html" title="type pyo3::PyObject">PyObject</a>&gt;</span>;
<span class="item-spacer"></span> fn <a href="#tymethod.add_submodule" class="fn">add_submodule</a>(&amp;self, module: &amp;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'_, <a class="struct" href="../types/struct.PyModule.html" title="struct pyo3::types::PyModule">PyModule</a>&gt;) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.unit.html">()</a>&gt;;
<span class="item-spacer"></span> fn <a href="#tymethod.add_function" class="fn">add_function</a>(&amp;self, fun: <a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'_, <a class="struct" href="../types/struct.PyCFunction.html" title="struct pyo3::types::PyCFunction">PyCFunction</a>&gt;) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.unit.html">()</a>&gt;;
<span class="item-spacer"></span> fn <a href="#tymethod.gil_used" class="fn">gil_used</a>(&amp;self, gil_used: <a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.bool.html">bool</a>) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.unit.html">()</a>&gt;;
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Implementation of functionality for <a href="../types/struct.PyModule.html" title="struct pyo3::types::PyModule"><code>PyModule</code></a>.</p>
<p>These methods are defined for the <code>Bound&lt;'py, PyModule&gt;</code> smart pointer, so to use method call
syntax these methods are separated into a trait, because stable Rust does not yet support
<code>arbitrary_self_types</code>.</p>
</div></details><h2 id="required-methods" class="section-header">Required Methods<a href="#required-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="tymethod.dict" class="method"><a class="src rightside" href="../../src/pyo3/types/module.rs.html#211">Source</a><h4 class="code-header">fn <a href="#tymethod.dict" class="fn">dict</a>(&amp;self) -&gt; <a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyDict.html" title="struct pyo3::types::PyDict">PyDict</a>&gt;</h4></section></summary><div class="docblock"><p>Returns the modules <code>__dict__</code> attribute, which contains the modules symbol table.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="tymethod.index" class="method"><a class="src rightside" href="../../src/pyo3/types/module.rs.html#217">Source</a><h4 class="code-header">fn <a href="#tymethod.index" class="fn">index</a>(&amp;self) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyList.html" title="struct pyo3::types::PyList">PyList</a>&gt;&gt;</h4></section></summary><div class="docblock"><p>Returns the index (the <code>__all__</code> attribute) of the module,
creating one if needed.</p>
<p><code>__all__</code> declares the items that will be imported with <code>from my_module import *</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="tymethod.name" class="method"><a class="src rightside" href="../../src/pyo3/types/module.rs.html#222">Source</a><h4 class="code-header">fn <a href="#tymethod.name" class="fn">name</a>(&amp;self) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyString.html" title="struct pyo3::types::PyString">PyString</a>&gt;&gt;</h4></section></summary><div class="docblock"><p>Returns the name (the <code>__name__</code> attribute) of the module.</p>
<p>May fail if the module does not have a <code>__name__</code> attribute.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="tymethod.filename" class="method"><a class="src rightside" href="../../src/pyo3/types/module.rs.html#227">Source</a><h4 class="code-header">fn <a href="#tymethod.filename" class="fn">filename</a>(&amp;self) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyString.html" title="struct pyo3::types::PyString">PyString</a>&gt;&gt;</h4></section></summary><div class="docblock"><p>Returns the filename (the <code>__file__</code> attribute) of the module.</p>
<p>May fail if the module does not have a <code>__file__</code> attribute.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="tymethod.add" class="method"><a class="src rightside" href="../../src/pyo3/types/module.rs.html#260-263">Source</a><h4 class="code-header">fn <a href="#tymethod.add" class="fn">add</a>&lt;N, V&gt;(&amp;self, name: N, value: V) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.unit.html">()</a>&gt;<div class="where">where
N: <a class="trait" href="../conversion/trait.IntoPyObject.html" title="trait pyo3::conversion::IntoPyObject">IntoPyObject</a>&lt;'py, Target = <a class="struct" href="../types/struct.PyString.html" title="struct pyo3::types::PyString">PyString</a>&gt;,
V: <a class="trait" href="../conversion/trait.IntoPyObject.html" title="trait pyo3::conversion::IntoPyObject">IntoPyObject</a>&lt;'py&gt;,</div></h4></section></summary><div class="docblock"><p>Adds an attribute to the module.</p>
<p>For adding classes, functions or modules, prefer to use <a href="../types/trait.PyModuleMethods.html#tymethod.add_class" title="method pyo3::types::PyModuleMethods::add_class"><code>PyModuleMethods::add_class</code></a>,
<a href="../types/trait.PyModuleMethods.html#tymethod.add_function" title="method pyo3::types::PyModuleMethods::add_function"><code>PyModuleMethods::add_function</code></a> or <a href="../types/trait.PyModuleMethods.html#tymethod.add_submodule" title="method pyo3::types::PyModuleMethods::add_submodule"><code>PyModuleMethods::add_submodule</code></a> instead,
respectively.</p>
<h5 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="attr">#[pymodule]
</span><span class="kw">fn </span>my_module(module: <span class="kw-2">&amp;</span>Bound&lt;<span class="lifetime">'_</span>, PyModule&gt;) -&gt; PyResult&lt;()&gt; {
module.add(<span class="string">"c"</span>, <span class="number">299_792_458</span>)<span class="question-mark">?</span>;
<span class="prelude-val">Ok</span>(())
}</code></pre></div>
<p>Python code can then do the following:</p>
<div class="example-wrap"><pre class="language-python"><code>from my_module import c
print(&quot;c is&quot;, c)</code></pre></div>
<p>This will result in the following output:</p>
<div class="example-wrap"><pre class="language-text"><code>c is 299792458</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="tymethod.add_class" class="method"><a class="src rightside" href="../../src/pyo3/types/module.rs.html#303-305">Source</a><h4 class="code-header">fn <a href="#tymethod.add_class" class="fn">add_class</a>&lt;T&gt;(&amp;self) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.unit.html">()</a>&gt;<div class="where">where
T: <a class="trait" href="../pyclass/trait.PyClass.html" title="trait pyo3::pyclass::PyClass">PyClass</a>,</div></h4></section></summary><div class="docblock"><p>Adds a new class to the module.</p>
<p>Notice that this method does not take an argument.
Instead, this method is <em>generic</em>, and requires us to use the
“turbofish” syntax to specify the class we want to add.</p>
<h5 id="examples-1"><a class="doc-anchor" href="#examples-1">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="attr">#[pyclass]
</span><span class="kw">struct </span>Foo { <span class="comment">/* fields omitted */ </span>}
<span class="attr">#[pymodule]
</span><span class="kw">fn </span>my_module(module: <span class="kw-2">&amp;</span>Bound&lt;<span class="lifetime">'_</span>, PyModule&gt;) -&gt; PyResult&lt;()&gt; {
module.add_class::&lt;Foo&gt;()<span class="question-mark">?</span>;
<span class="prelude-val">Ok</span>(())
}</code></pre></div>
<p>Python code can see this class as such:</p>
<div class="example-wrap"><pre class="language-python"><code>from my_module import Foo
print(&quot;Foo is&quot;, Foo)</code></pre></div>
<p>This will result in the following output:</p>
<div class="example-wrap"><pre class="language-text"><code>Foo is &lt;class &#39;builtins.Foo&#39;&gt;</code></pre></div>
<p>Note that as we havent defined a <a href="https://pyo3.rs/v0.24.1/class.html#constructor">constructor</a>, Python code cant actually
make an <em>instance</em> of <code>Foo</code> (or <em>get</em> one for that matter, as we havent exported
anything that can return instances of <code>Foo</code>).</p>
</div></details><details class="toggle method-toggle" open><summary><section id="tymethod.add_wrapped" class="method"><a class="src rightside" href="../../src/pyo3/types/module.rs.html#311-313">Source</a><h4 class="code-header">fn <a href="#tymethod.add_wrapped" class="fn">add_wrapped</a>&lt;T&gt;(&amp;self, wrapper: &amp;impl <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/ops/function/trait.Fn.html" title="trait core::ops::function::Fn">Fn</a>(<a class="struct" href="../marker/struct.Python.html" title="struct pyo3::marker::Python">Python</a>&lt;'py&gt;) -&gt; T) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.unit.html">()</a>&gt;<div class="where">where
T: IntoPyCallbackOutput&lt;'py, <a class="type" href="../type.PyObject.html" title="type pyo3::PyObject">PyObject</a>&gt;,</div></h4></section></summary><div class="docblock"><p>Adds a function or a (sub)module to a module, using the functions name as name.</p>
<p>Prefer to use <a href="../types/trait.PyModuleMethods.html#tymethod.add_function" title="method pyo3::types::PyModuleMethods::add_function"><code>PyModuleMethods::add_function</code></a> and/or <a href="../types/trait.PyModuleMethods.html#tymethod.add_submodule" title="method pyo3::types::PyModuleMethods::add_submodule"><code>PyModuleMethods::add_submodule</code></a>
instead.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="tymethod.add_submodule" class="method"><a class="src rightside" href="../../src/pyo3/types/module.rs.html#355">Source</a><h4 class="code-header">fn <a href="#tymethod.add_submodule" class="fn">add_submodule</a>(&amp;self, module: &amp;<a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'_, <a class="struct" href="../types/struct.PyModule.html" title="struct pyo3::types::PyModule">PyModule</a>&gt;) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.unit.html">()</a>&gt;</h4></section></summary><div class="docblock"><p>Adds a submodule to a module.</p>
<p>This is especially useful for creating module hierarchies.</p>
<p>Note that this doesnt define a <em>package</em>, so this wont allow Python code
to directly import submodules by using
<span style="white-space: pre"><code>from my_module import submodule</code></span>.
For more information, see <a href="https://github.com/PyO3/pyo3/issues/759">#759</a> and <a href="https://github.com/PyO3/pyo3/issues/1517#issuecomment-808664021">#1517</a>.</p>
<h5 id="examples-2"><a class="doc-anchor" href="#examples-2">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="attr">#[pymodule]
</span><span class="kw">fn </span>my_module(py: Python&lt;<span class="lifetime">'_</span>&gt;, module: <span class="kw-2">&amp;</span>Bound&lt;<span class="lifetime">'_</span>, PyModule&gt;) -&gt; PyResult&lt;()&gt; {
<span class="kw">let </span>submodule = PyModule::new(py, <span class="string">"submodule"</span>)<span class="question-mark">?</span>;
submodule.add(<span class="string">"super_useful_constant"</span>, <span class="string">"important"</span>)<span class="question-mark">?</span>;
module.add_submodule(<span class="kw-2">&amp;</span>submodule)<span class="question-mark">?</span>;
<span class="prelude-val">Ok</span>(())
}</code></pre></div>
<p>Python code can then do the following:</p>
<div class="example-wrap"><pre class="language-python"><code>import my_module
print(&quot;super_useful_constant is&quot;, my_module.submodule.super_useful_constant)</code></pre></div>
<p>This will result in the following output:</p>
<div class="example-wrap"><pre class="language-text"><code>super_useful_constant is important</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="tymethod.add_function" class="method"><a class="src rightside" href="../../src/pyo3/types/module.rs.html#391">Source</a><h4 class="code-header">fn <a href="#tymethod.add_function" class="fn">add_function</a>(&amp;self, fun: <a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'_, <a class="struct" href="../types/struct.PyCFunction.html" title="struct pyo3::types::PyCFunction">PyCFunction</a>&gt;) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.unit.html">()</a>&gt;</h4></section></summary><div class="docblock"><p>Add a function to a module.</p>
<p>Note that this also requires the <a href="../macro.wrap_pyfunction.html" title="macro pyo3::wrap_pyfunction"><code>wrap_pyfunction!</code></a> macro
to wrap a function annotated with <a href="../attr.pyfunction.html" title="attr pyo3::pyfunction"><code>#[pyfunction]</code></a>.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="attr">#[pyfunction]
</span><span class="kw">fn </span>say_hello() {
<span class="macro">println!</span>(<span class="string">"Hello world!"</span>)
}
<span class="attr">#[pymodule]
</span><span class="kw">fn </span>my_module(module: <span class="kw-2">&amp;</span>Bound&lt;<span class="lifetime">'_</span>, PyModule&gt;) -&gt; PyResult&lt;()&gt; {
module.add_function(<span class="macro">wrap_pyfunction!</span>(say_hello, module)<span class="question-mark">?</span>)
}</code></pre></div>
<p>Python code can then do the following:</p>
<div class="example-wrap"><pre class="language-python"><code>from my_module import say_hello
say_hello()</code></pre></div>
<p>This will result in the following output:</p>
<div class="example-wrap"><pre class="language-text"><code>Hello world!</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="tymethod.gil_used" class="method"><a class="src rightside" href="../../src/pyo3/types/module.rs.html#425">Source</a><h4 class="code-header">fn <a href="#tymethod.gil_used" class="fn">gil_used</a>(&amp;self, gil_used: <a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.bool.html">bool</a>) -&gt; <a class="type" href="../type.PyResult.html" title="type pyo3::PyResult">PyResult</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.unit.html">()</a>&gt;</h4></section></summary><div class="docblock"><p>Declare whether or not this module supports running with the GIL disabled</p>
<p>If the module does not rely on the GIL for thread safety, you can pass
<code>false</code> to this function to indicate the module does not rely on the GIL
for thread-safety.</p>
<p>This function sets the <a href="https://docs.python.org/3/c-api/module.html#c.Py_mod_gil"><code>Py_MOD_GIL</code>
slot</a> on the
module object. The default is <code>Py_MOD_GIL_USED</code>, so passing <code>true</code> to
this function is a no-op unless you have already set <code>Py_MOD_GIL</code> to
<code>Py_MOD_GIL_NOT_USED</code> elsewhere.</p>
<h5 id="examples-3"><a class="doc-anchor" href="#examples-3">§</a>Examples</h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
<span class="attr">#[pymodule(gil_used = <span class="bool-val">false</span>)]
</span><span class="kw">fn </span>my_module(py: Python&lt;<span class="lifetime">'_</span>&gt;, module: <span class="kw-2">&amp;</span>Bound&lt;<span class="lifetime">'_</span>, PyModule&gt;) -&gt; PyResult&lt;()&gt; {
<span class="kw">let </span>submodule = PyModule::new(py, <span class="string">"submodule"</span>)<span class="question-mark">?</span>;
submodule.gil_used(<span class="bool-val">false</span>)<span class="question-mark">?</span>;
module.add_submodule(<span class="kw-2">&amp;</span>submodule)<span class="question-mark">?</span>;
<span class="prelude-val">Ok</span>(())
}</code></pre></div>
<p>The resulting module will not print a <code>RuntimeWarning</code> and re-enable the
GIL when Python imports it on the free-threaded build, since all module
objects defined in the extension have <code>Py_MOD_GIL</code> set to
<code>Py_MOD_GIL_NOT_USED</code>.</p>
<p>This is a no-op on the GIL-enabled build.</p>
</div></details></div><h2 id="dyn-compatibility" class="section-header">Dyn Compatibility<a href="#dyn-compatibility" class="anchor">§</a></h2><div class="dyn-compatibility-info"><p>This trait is <b>not</b> <a href="https://doc.rust-lang.org/1.85.1/reference/items/traits.html#object-safety">dyn compatible</a>.</p><p><i>In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.</i></p></div><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"><section id="impl-PyModuleMethods%3C'py%3E-for-Bound%3C'py,+PyModule%3E" class="impl"><a class="src rightside" href="../../src/pyo3/types/module.rs.html#428-565">Source</a><a href="#impl-PyModuleMethods%3C'py%3E-for-Bound%3C'py,+PyModule%3E" class="anchor">§</a><h3 class="code-header">impl&lt;'py&gt; <a class="trait" href="../types/trait.PyModuleMethods.html" title="trait pyo3::types::PyModuleMethods">PyModuleMethods</a>&lt;'py&gt; for <a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a>&lt;'py, <a class="struct" href="../types/struct.PyModule.html" title="struct pyo3::types::PyModule">PyModule</a>&gt;</h3></section></div><script src="../../trait.impl/pyo3/types/module/trait.PyModuleMethods.js" async></script></section></div></main></body></html>