Skip to content

Commit

Permalink
wip bound docs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Feb 26, 2024
1 parent 404161c commit fa45912
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions guide/src/types.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
# GIL lifetimes, mutability and Python object types

On first glance, PyO3 provides a huge number of different types that can be used
At first glance, PyO3 provides a huge number of different types that can be used
to wrap or refer to Python objects. This page delves into the details and gives
an overview of their intended meaning, with examples when each type is best
used.


## The Python GIL, mutability, and Rust types

Since Python has no concept of ownership, and works solely with boxed objects,
any Python object can be referenced any number of times, and mutation is allowed
from any reference.
Python code differs from Rust in two key ways:
- There is no concept of ownership; all Python objects are reference counted
- There is no concept of exclusive (`&mut`) references; any reference can mutate a Python object

PyO3's API for interacting with Python objects is built with this in mind. There are two generic "smart pointers" to Python objects, `Py<T>` and `Bound<'py, T>`, which both use Python reference counting as their memory management. Almost all methods on these smart pointers use `&self` receivers, that is, a `&mut Py<T>` or `&mut Bound<'py, T>` are almost never used in PyO3 code.

The situation is helped a little by the Global Interpreter Lock (GIL), which
ensures that only one thread can use the Python interpreter and its API at the
same time, while non-Python operations (system calls and extension code) can
unlock the GIL. (See [the section on parallelism](parallelism.md) for how to do
that in PyO3.)
The situation is helped by the Global Interpreter Lock (GIL), which ensures that only one thread can use the Python interpreter and its API at the same time, while non-Python operations (system calls and extension code) can unlock the GIL. (See [the section on parallelism](parallelism.md) for how to do that in PyO3.)

In PyO3, holding the GIL is modeled by acquiring a token of the type
`Python<'py>`, which serves three purposes:
Expand Down

0 comments on commit fa45912

Please sign in to comment.