Skip to content

Commit 1915bf0

Browse files
committed
Doc: expose more prominently const class attributes
They are closer to the actual behavior. State explicitly that function-based class attributes are also computed only once at initialization
1 parent c1f3d34 commit 1915bf0

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

guide/src/class.md

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ impl MyClass {
886886

887887
## Class attributes
888888

889-
To create a class attribute (also called [class variable][classattr]), a method without any arguments can be annotated with the `#[classattr]` attribute.
889+
To create a class attribute (also called [class variable][classattr]), an associated constant can be annotated with the `#[classattr]` attribute.
890890

891891
```rust,no_run
892892
# use pyo3::prelude::*;
@@ -895,25 +895,19 @@ To create a class attribute (also called [class variable][classattr]), a method
895895
#[pymethods]
896896
impl MyClass {
897897
#[classattr]
898-
fn my_attribute() -> String {
899-
"hello".to_string()
900-
}
898+
const MY_ATTRIBUTE: &'static str = "foobar";
901899
}
902-
903-
Python::attach(|py| {
904-
let my_class = py.get_type::<MyClass>();
905-
pyo3::py_run!(py, my_class, "assert my_class.my_attribute == 'hello'")
906-
});
900+
#
901+
#Python::attach(|py| {
902+
# let my_class = py.get_type::<MyClass>();
903+
# pyo3::py_run!(py, my_class, "assert my_class.MY_ATTRIBUTE == 'foobar'")
904+
#});
907905
```
908906

909-
> [!NOTE]
910-
> If the method has a `Result` return type and returns an `Err`, PyO3 will panic during
911-
class creation.
907+
If `const` code is too limiting, a method without any arguments can be annotated with the `#[classattr]` attribute.
912908

913909
> [!NOTE]
914-
> `#[classattr]` does not work with [`#[pyo3(warn(...))]`](./function.md#warn) attribute.
915-
916-
If the class attribute is defined with `const` code only, one can also annotate associated constants:
910+
> Here too, the class attribute value is computed once during the class creation and not each time the attribute is accessed.
917911
918912
```rust,no_run
919913
# use pyo3::prelude::*;
@@ -922,10 +916,24 @@ If the class attribute is defined with `const` code only, one can also annotate
922916
#[pymethods]
923917
impl MyClass {
924918
#[classattr]
925-
const MY_CONST_ATTRIBUTE: &'static str = "foobar";
919+
fn my_attribute() -> String {
920+
"hello".to_string()
921+
}
926922
}
923+
#
924+
#Python::attach(|py| {
925+
# let my_class = py.get_type::<MyClass>();
926+
# pyo3::py_run!(py, my_class, "assert my_class.my_attribute == 'hello'")
927+
#});
927928
```
928929

930+
> [!NOTE]
931+
> If the method has a `Result` return type and returns an `Err`, PyO3 will panic during
932+
class creation.
933+
934+
> [!NOTE]
935+
> `#[classattr]` does not work with [`#[pyo3(warn(...))]`](./function.md#warn) attribute.
936+
929937
## Classes as function arguments
930938

931939
Class objects can be used as arguments to `#[pyfunction]`s and `#[pymethods]` in the same way as the self parameters of instance methods, i.e. they can be passed as:

0 commit comments

Comments
 (0)