You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implement #[init] method attribute in #[pymethods] (#4951)
* Implement #[init] method attribute in #[pymethods]
This allows to control objects initialization flow in the Rust code
in case of inheritance from native Python types.
* Apply suggestions from code review
Co-authored-by: Bruno Kolenbrander <[email protected]>
* review feedback
* expose `PySuper` on PyPy, GraalPy and ABI3
* fix graalpy issue
---------
Co-authored-by: David Hewitt <[email protected]>
Co-authored-by: Bruno Kolenbrander <[email protected]>
Co-authored-by: Icxolu <[email protected]>
Copy file name to clipboardExpand all lines: guide/src/class.md
+71-1Lines changed: 71 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -145,7 +145,7 @@ There is a [detailed discussion on thread-safety](./class/thread-safety.md) late
145
145
146
146
By default, it is not possible to create an instance of a custom class from Python code.
147
147
To declare a constructor, you need to define a method and annotate it with the `#[new]` attribute.
148
-
Only Python's `__new__` method can be specified, `__init__` is not available.
148
+
A constructor is accessible as Python's `__new__` method.
149
149
150
150
```rust
151
151
# #![allow(dead_code)]
@@ -192,6 +192,76 @@ If no method marked with `#[new]` is declared, object instances can only be crea
192
192
193
193
For arguments, see the [`Method arguments`](#method-arguments) section below.
194
194
195
+
## Initializer
196
+
197
+
An initializer implements Python's `__init__` method.
198
+
199
+
It may be required when it's needed to control an object initalization flow on the Rust code.
200
+
If possible handling this in `__new__` should be preferred, but in some cases, like subclassing native types, overwriting `__init__` might be necessary.
201
+
For example, you define a class that extends `PyDict` and don't want that the original `__init__` method of `PyDict` been called.
202
+
In this case by defining an own `__init__` method it's possible to stop initialization flow.
203
+
204
+
If you declare an `__init__` method you may need to call a super class' `__init__` method explicitly like in Python code.
205
+
206
+
To declare an initializer, you need to define the `__init__` method.
207
+
Like in Python `__init__` must have the `self` receiver as the first argument, followed by the same arguments as the constructor.
Copy file name to clipboardExpand all lines: guide/src/class/protocols.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ Because of the double-underscores surrounding their name, these are also known a
6
6
7
7
PyO3 makes it possible for every magic method to be implemented in `#[pymethods]` just as they would be done in a regular Python class, with a few notable differences:
8
8
9
-
-`__new__`and `__init__` are replaced by the [`#[new]` attribute](../class.md#constructor).
9
+
-`__new__`is replaced by the [`#[new]` attribute](../class.md#constructor).
10
10
-`__del__` is not yet supported, but may be in the future.
11
11
-`__buffer__` and `__release_buffer__` are currently not supported and instead PyO3 supports [`__getbuffer__` and `__releasebuffer__`](#buffer-objects) methods (these predate [PEP 688](https://peps.python.org/pep-0688/#python-level-buffer-protocol)), again this may change in the future.
12
12
- PyO3 adds [`__traverse__` and `__clear__`](#garbage-collector-integration) methods for controlling garbage collection.
0 commit comments