pyo3

Attribute Macro pymethods

#[pymethods]
Available on crate feature macros only.
Expand description

A proc macro used to expose methods to Python.

Methods within a #[pymethods] block can be annotated with as well as the following:

AnnotationDescription
#[new]Defines the class constructor, like Python’s __new__ method.
#[getter] and #[setter]These define getters and setters, similar to Python’s @property decorator. This is useful for getters/setters that require computation or side effects; if that is not the case consider using #[pyo3(get, set)] on the struct’s field(s).
#[staticmethod]Defines the method as a staticmethod, like Python’s @staticmethod decorator.
#[classmethod]Defines the method as a classmethod, like Python’s @classmethod decorator.
#[classattr]Defines a class variable.
#[args]Deprecated way to define a method’s default arguments and allows the function to receive *args and **kwargs. Use #[pyo3(signature = (...))] instead.
[#[pyo3(<option> = <value>)][pyo3-method-options]Any of the #[pyo3] options supported on pyfunction.

For more on creating class methods, see the class section of the guide.

If the multiple-pymethods feature is enabled, it is possible to implement multiple #[pymethods] blocks for a single #[pyclass]. This will add a transitive dependency on the inventory crate.

⚠️ Internal Docs ⚠️ Not Public API 👉 Official Docs Here