Skip to content

Commit

Permalink
Merge pull request PyO3#391 from Alexander-N/typos
Browse files Browse the repository at this point in the history
Fix some typos and grammar in class.md
  • Loading branch information
konstin authored Mar 7, 2019
2 parents 0866dd8 + ae93e71 commit d63fac8
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions guide/src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Define new class

To define python custom class, rust struct needs to be annotated with `#[pyclass]` attribute.
To define a custom python class, a rust struct needs to be annotated with the
`#[pyclass]` attribute.

```rust
# use pyo3::prelude::*;
Expand All @@ -19,12 +20,12 @@ The above example generates implementations for `PyTypeInfo` and `PyTypeObject`
## Get Python objects from `pyclass`
You can use `pyclass`es like normal rust structs.

However, if instantiate noramlly, you can't treat `pyclass`es as Python objects.
However, if instantiated normally, you can't treat `pyclass`es as Python objects.

To get a Python object which includes `pyclass`, we have to use some special methods.

### `PyRef`
`PyRef` is a special reference, which ensures that the reffrered struct is a part of
`PyRef` is a special reference, which ensures that the referred struct is a part of
a Python object, and you are also holding the GIL.

You can get an instance of `PyRef` by `PyRef::new`, which does 3 things:
Expand Down Expand Up @@ -219,7 +220,7 @@ Getter or setter function's name is used as property name by default. There are
ways how to override name.

If function name starts with `get_` or `set_` for getter or setter respectively.
Descriptor name becomes function name with prefix removed. This is useful in case os
Descriptor name becomes function name with prefix removed. This is useful in case of
rust's special keywords like `type`.

```rust
Expand Down Expand Up @@ -248,7 +249,7 @@ impl MyClass {
In this case property `num` is defined. And it is available from python code as `self.num`.

Also both `#[getter]` and `#[setter]` attributes accepts one parameter.
If parameter is specified, it is used and property name. i.e.
If this parameter is specified, it is used as a property name. i.e.

```rust
# use pyo3::prelude::*;
Expand All @@ -273,7 +274,7 @@ impl MyClass {
}
```

In this case property `number` is defined. And it is available from python code as `self.number`.
In this case the property `number` is defined and is available from python code as `self.number`.

For simple cases you can also define getters and setters in your Rust struct field definition, for example:

Expand All @@ -290,10 +291,9 @@ Then it is available from Python code as `self.num`.

## Instance methods

To define python compatible method, `impl` block for struct has to be annotated
with `#[pymethods]` attribute. `pyo3` library generates python compatible
wrappers for all functions in this block with some variations, like descriptors,
class method static methods, etc.
To define a python compatible method, `impl` block for struct has to be annotated with the
`#[pymethods]` attribute. PyO3 generates python compatible wrappers for all functions in this
block with some variations, like descriptors, class method static methods, etc.

```rust
# use pyo3::prelude::*;
Expand Down Expand Up @@ -342,8 +342,8 @@ From python perspective `method2`, in above example, does not accept any argumen

## Class methods

To specify class method for custom class, method needs to be annotated
with`#[classmethod]` attribute.
To specify a class method for a custom class, the method needs to be annotated
with the `#[classmethod]` attribute.

```rust
# use pyo3::prelude::*;
Expand Down Expand Up @@ -373,9 +373,9 @@ Declares a class method callable from Python.

## Static methods

To specify class method for custom class, method needs to be annotated
with `#[staticmethod]` attribute. The return type must be `PyResult<T>`
for some `T` that implements `IntoPyObject`.
To specify a static method for a custom class, method needs to be annotated with
`#[staticmethod]` attribute. The return type must be `PyResult<T>` for some `T` that implements
`IntoPyObject`.

```rust
# use pyo3::prelude::*;
Expand All @@ -396,8 +396,8 @@ impl MyClass {

## Callable object

To specify custom `__call__` method for custom class, call method needs to be annotated
with `#[call]` attribute. Arguments of the method are specified same as for instance method.
To specify a custom `__call__` method for a custom class, call methods need to be annotated with
the `#[call]` attribute. Arguments of the method are specified same as for instance method.

```rust
# use pyo3::prelude::*;
Expand All @@ -421,14 +421,13 @@ impl MyClass {

## Method arguments

By default pyo3 library uses function signature to determine which arguments are required.
Then it scans incoming `args` parameter and then incoming `kwargs` parameter. If it can not
find all required parameters, it raises `TypeError` exception.
It is possible to override default behavior with `#[args(...)]` attribute. `args` attribute
accept comma separated list of parameters in form `attr_name="default value"`. Each parameter
has to match method parameter by name.
By default PyO3 uses function signatures to determine which arguments are required. Then it scans
incoming `args` parameter and then incoming `kwargs` parameter. If it can not find all required
parameters, it raises a `TypeError` exception. It is possible to override the default behavior
with `#[args(...)]` attribute. `args` attribute accepts a comma separated list of parameters in
form of `attr_name="default value"`. Each parameter has to match the method parameter by name.

Each parameter could one of following type:
Each parameter could be one of following type:

* "\*": var arguments separator, each parameter defined after "*" is keyword only parameters.
corresponds to python's `def meth(*, arg1.., arg2=..)`
Expand All @@ -438,7 +437,7 @@ Each parameter could one of following type:
Type of `kwargs` parameter has to be `Option<&PyDict>`.
* arg="Value": arguments with default value. corresponds to python's `def meth(arg=Value)`.
if `arg` argument is defined after var arguments it is treated as keyword argument.
Note that `Value` has to be valid rust code, pyo3 just inserts it into generated
Note that `Value` has to be valid rust code, PyO3 just inserts it into generated
code unmodified.

Example:
Expand All @@ -464,11 +463,10 @@ impl MyClass {

## Class customizations

Python object model defines several protocols for different object behavior,
like sequence, mapping or number protocols. pyo3 library defines separate trait for each
of them. To provide specific python object behavior you need to implement specific trait
for your struct. Important note, each protocol implementation block has to be annotated
with `#[pyproto]` attribute.
Python's object model defines several protocols for different object behavior, like sequence,
mapping or number protocols. PyO3 defines separate traits for each of them. To provide specific
python object behavior you need to implement the specific trait for your struct. Important note,
each protocol implementation block has to be annotated with `#[pyproto]` attribute.

### Basic object customization

Expand Down Expand Up @@ -527,7 +525,7 @@ Each methods corresponds to python's `self.attr`, `self.attr = value` and `del s

* `fn __bool__(&self) -> PyResult<bool>`

Determines the "truthyness" of the object.
Determines the "truthiness" of the object.
This method works for both python 3 and python 2,
even on Python 2.7 where the Python spelling was `__nonzero__`.

Expand Down Expand Up @@ -574,9 +572,9 @@ impl PyGCProtocol for ClassWithGCSupport {
}
```

Special protocol trait implementation has to be annotated with `#[pyproto]` attribute.
Special protocol trait implementations have to be annotated with the `#[pyproto]` attribute.

It is also possible to enable gc for custom class using `gc` parameter for `class` annotation.
It is also possible to enable GC for custom class using `gc` parameter for `class` annotation.
i.e. `#[pyclass(gc)]`. In that case instances of custom class participate in python garbage
collector, and it is possible to track them with `gc` module methods.

Expand Down Expand Up @@ -618,6 +616,10 @@ TODO: Which traits to implement (basically `PyTypeCreate: PyObjectAlloc + PyType

## How methods are implemented

Users should be able to define a `#[pyclass]` with or without `#[pymethods]`, while pyo3 needs a trait with a function that returns all methods. Since it's impossible make the code generation in pyclass dependent on whether there is an impl block, we'd need to make to implement the trait on `#[pyclass]` and override the implementation in `#[pymethods]`, which is to my best knowledge only possible with the specialization feature, which is can't be used on stable.
Users should be able to define a `#[pyclass]` with or without `#[pymethods]`, while PyO3 needs a
trait with a function that returns all methods. Since it's impossible to make the code generation in
pyclass dependent on whether there is an impl block, we'd need to implement the trait on
`#[pyclass]` and override the implementation in `#[pymethods]`, which is to the best of my knowledge
only possible with the specialization feature, which can't be used on stable.

To escape this we use [inventory](https://github.com/dtolnay/inventory), which allows us to collect `impl`s from arbitrary source code by exploiting some binary trick. See [inventory: how it works](https://github.com/dtolnay/inventory#how-it-works) and `pyo3_derive_backend::py_class::impl_inventory` for more details.

0 comments on commit d63fac8

Please sign in to comment.