Skip to content

Commit 44d8f70

Browse files
committed
docs/library/weakref: Add documentation for weakref module.
Signed-off-by: Damien George <damien@micropython.org>
1 parent 6f96d26 commit 44d8f70

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

docs/library/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ library.
8282
struct.rst
8383
sys.rst
8484
time.rst
85+
weakref.rst
8586
zlib.rst
8687
_thread.rst
8788

docs/library/weakref.rst

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
:mod:`weakref` -- Python object lifetime management
2+
===================================================
3+
4+
.. module:: weakref
5+
:synopsis: Create weak references to Python objects
6+
7+
|see_cpython_module| :mod:`python:weakref`.
8+
9+
This module allows creation of weak references to Python objects. A weak reference
10+
is a non-traceable reference to a heap-allocated Python object, so the garbage
11+
collector can still reclaim the object even though the weak reference refers to it.
12+
13+
Python callbacks can be registered to be called when an object is reclaimed by the
14+
garbage collector. This provides a safe way to clean up when objects are no longer
15+
needed.
16+
17+
**Availability:** the weakref module requires ``MICROPY_PY_WEAKREF`` to be enabled
18+
at compile time. It is enabled on the unix coverage variant and the webassembly
19+
pyscript variant.
20+
21+
ref objects
22+
-----------
23+
24+
A ref object is the simplest way to make a weak reference.
25+
26+
.. class:: ref(object [, callback], /)
27+
28+
Return a weak reference to the given *object*.
29+
30+
If *callback* is given and is not ``None`` then, when *object* is reclaimed
31+
by the garbage collector and if the weak reference object is still alive, the
32+
*callback* will be called. The *callback* will be passed the weak reference
33+
object as its single argument.
34+
35+
.. method:: ref.__call__()
36+
37+
Calling the weak reference object will return its referenced object if that
38+
object is still alive. Otherwise ``None`` will be returned.
39+
40+
finalize objects
41+
----------------
42+
43+
A finalize object is an extended version of a ref object that is more convenient to
44+
use, and allows more control over the callback.
45+
46+
.. class:: finalize(object, callback, /, *args, **kwargs)
47+
48+
Return a weak reference to the given *object*. In contrast to *weakref.ref*
49+
objects, finalize objects are held onto internally and will not be collected until
50+
*object* is collected.
51+
52+
A finalize object starts off alive. It transitions to the dead state when the
53+
finalize object is called, either explicitly or when *object* is collected. It also
54+
transitions to dead if the `finalize.detach()` method is called.
55+
56+
When *object* is reclaimed by the garbage collector (or the finalize object is
57+
explicitly called by user code) and the finalize object is still in the alive state,
58+
the *callback* will be called. The *callback* will be passed arguments as:
59+
``callback(*args, **kwargs)``.
60+
61+
.. method:: finalize.__call__()
62+
63+
If the finalize object is alive then it transitions to the dead state and returns
64+
the value of ``callback(*args, **kwargs)``. Otherwise ``None`` will be returned.
65+
66+
.. method:: finalize.alive
67+
68+
Read-only boolean attribute that indicates if the finalizer is in the alive state.
69+
70+
.. method:: finalize.peek()
71+
72+
If the finalize object is alive then return ``(object, callback, args, kwargs)``.
73+
Otherwise return ``None``.
74+
75+
.. method:: finalize.detach()
76+
77+
If the finalize object is alive then it transitions to the dead state and returns
78+
``(object, callback, args, kwargs)``. Otherwise ``None`` will be returned.

0 commit comments

Comments
 (0)