Files
google_speech_rs/docs/pyo3/attr.pyclass.html
T
2025-04-02 13:41:38 +00:00

40 lines
10 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="A proc macro used to expose Rust structs and fieldless enums as Python objects."><title>pyclass in pyo3 - 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 attr"><!--[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"><div id="rustdoc-modnav"></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></span><h1>Attribute Macro <span class="attr">pyclass</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_macros/lib.rs.html#68">Source</a> </span></div><pre class="rust item-decl"><code>#[pyclass]</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A proc macro used to expose Rust structs and fieldless enums as Python objects.</p>
<p><code>#[pyclass]</code> can be used with the following parameters:</p>
<div><table><thead><tr><th style="text-align: left">Parameter</th><th style="text-align: left">Description</th></tr></thead><tbody>
<tr><td style="text-align: left"><code>constructor</code></td><td style="text-align: left">This is currently only allowed on <a href="https://pyo3.rs/latest/class.html#complex-enums">variants of complex enums</a>. It allows customization of the generated class constructor for each variant. It uses the same syntax and supports the same options as the <code>signature</code> attribute of functions and methods.</td></tr>
<tr><td style="text-align: left"><span style="white-space: pre"><code>crate = "some::path"</code></span></td><td style="text-align: left">Path to import the <code>pyo3</code> crate, if its not accessible at <code>::pyo3</code>.</td></tr>
<tr><td style="text-align: left"><code>dict</code></td><td style="text-align: left">Gives instances of this class an empty <code>__dict__</code> to store custom attributes.</td></tr>
<tr><td style="text-align: left"><code>eq</code></td><td style="text-align: left">Implements <code>__eq__</code> using the <code>PartialEq</code> implementation of the underlying Rust datatype.</td></tr>
<tr><td style="text-align: left"><code>eq_int</code></td><td style="text-align: left">Implements <code>__eq__</code> using <code>__int__</code> for simple enums.</td></tr>
<tr><td style="text-align: left"><span style="white-space: pre"><code>extends = BaseType</code></span></td><td style="text-align: left">Use a custom baseclass. Defaults to <a href="https://docs.rs/pyo3/latest/pyo3/types/struct.PyAny.html"><code>PyAny</code></a></td></tr>
<tr><td style="text-align: left"><span style="white-space: pre"><code>freelist = N</code></span></td><td style="text-align: left">Implements a <a href="https://en.wikipedia.org/wiki/Free_list">free list</a> of size N. This can improve performance for types that are often created and deleted in quick succession. Profile your code to see whether <code>freelist</code> is right for you.</td></tr>
<tr><td style="text-align: left"><span style="white-space: pre"><code>frozen</code></span></td><td style="text-align: left">Declares that your pyclass is immutable. It removes the borrow checker overhead when retrieving a shared reference to the Rust struct, but disables the ability to get a mutable reference.</td></tr>
<tr><td style="text-align: left"><code>get_all</code></td><td style="text-align: left">Generates getters for all fields of the pyclass.</td></tr>
<tr><td style="text-align: left"><code>hash</code></td><td style="text-align: left">Implements <code>__hash__</code> using the <code>Hash</code> implementation of the underlying Rust datatype.</td></tr>
<tr><td style="text-align: left"><code>mapping</code></td><td style="text-align: left">Inform PyO3 that this class is a <a href="https://pyo3.rs/latest/class/protocols.html#mapping--sequence-types"><code>Mapping</code></a>, and so leave its implementation of sequence C-API slots empty.</td></tr>
<tr><td style="text-align: left"><span style="white-space: pre"><code>module = "module_name"</code></span></td><td style="text-align: left">Python code will see the class as being defined in this module. Defaults to <code>builtins</code>.</td></tr>
<tr><td style="text-align: left"><span style="white-space: pre"><code>name = "python_name"</code></span></td><td style="text-align: left">Sets the name that Python sees this class as. Defaults to the name of the Rust struct.</td></tr>
<tr><td style="text-align: left"><code>ord</code></td><td style="text-align: left">Implements <code>__lt__</code>, <code>__gt__</code>, <code>__le__</code>, &amp; <code>__ge__</code> using the <code>PartialOrd</code> implementation of the underlying Rust datatype. <em>Requires <code>eq</code></em></td></tr>
<tr><td style="text-align: left"><code>rename_all = "renaming_rule"</code></td><td style="text-align: left">Applies renaming rules to every getters and setters of a struct, or every variants of an enum. Possible values are: “camelCase”, “kebab-case”, “lowercase”, “PascalCase”, “SCREAMING-KEBAB-CASE”, “SCREAMING_SNAKE_CASE”, “snake_case”, “UPPERCASE”.</td></tr>
<tr><td style="text-align: left"><code>sequence</code></td><td style="text-align: left">Inform PyO3 that this class is a <a href="https://pyo3.rs/latest/class/protocols.html#mapping--sequence-types"><code>Sequence</code></a>, and so leave its C-API mapping length slot empty.</td></tr>
<tr><td style="text-align: left"><code>set_all</code></td><td style="text-align: left">Generates setters for all fields of the pyclass.</td></tr>
<tr><td style="text-align: left"><code>str</code></td><td style="text-align: left">Implements <code>__str__</code> using the <code>Display</code> implementation of the underlying Rust datatype or by passing an optional format string <code>str="&lt;format string&gt;"</code>. <em>Note: The optional format string is only allowed for structs. <code>name</code> and <code>rename_all</code> are incompatible with the optional format string. Additional details can be found in the discussion on this <a href="https://github.com/PyO3/pyo3/pull/4233">PR</a>.</em></td></tr>
<tr><td style="text-align: left"><code>subclass</code></td><td style="text-align: left">Allows other Python classes and <code>#[pyclass]</code> to inherit from this class. Enums cannot be subclassed.</td></tr>
<tr><td style="text-align: left"><code>unsendable</code></td><td style="text-align: left">Required if your struct is not <a href="https://doc.rust-lang.org/std/marker/trait.Send.html"><code>Send</code></a>. Rather than using <code>unsendable</code>, consider implementing your struct in a thread-safe way by e.g. substituting <a href="https://doc.rust-lang.org/std/rc/struct.Rc.html"><code>Rc</code></a> with <a href="https://doc.rust-lang.org/std/sync/struct.Arc.html"><code>Arc</code></a>. By using <code>unsendable</code>, your class will panic when accessed by another thread. Also note the Pythons GC is multi-threaded and while unsendable classes will not be traversed on foreign threads to avoid UB, this can lead to memory leaks.</td></tr>
<tr><td style="text-align: left"><code>weakref</code></td><td style="text-align: left">Allows this class to be <a href="https://docs.python.org/3/library/weakref.html">weakly referenceable</a>.</td></tr>
</tbody></table>
</div>
<p>All of these parameters can either be passed directly on the <code>#[pyclass(...)]</code> annotation, or as one or
more accompanying <code>#[pyo3(...)]</code> annotations, e.g.:</p>
<div class="example-wrap ignore"><a href="#" class="tooltip" title="This example is not tested"></a><pre class="rust rust-example-rendered"><code><span class="comment">// Argument supplied directly to the `#[pyclass]` annotation.
</span><span class="attr">#[pyclass(name = <span class="string">"SomeName"</span>, subclass)]
</span><span class="kw">struct </span>MyClass {}
<span class="comment">// Argument supplied as a separate annotation.
</span><span class="attr">#[pyclass]
#[pyo3(name = <span class="string">"SomeName"</span>, subclass)]
</span><span class="kw">struct </span>MyClass {}</code></pre></div>
<p>For more on creating Python classes,
see the <a href="https://pyo3.rs/v0.24.1/class.html">class section of the guide</a>.</p>
</div></details></section></div></main></body></html>