summaryrefslogtreecommitdiffstats
path: root/Doc/library/weakref.rst
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2012-11-17 17:57:38 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2012-11-17 17:57:38 (GMT)
commitc3afba104aef5032e114b4f5cac0a3bbdfef2bba (patch)
treee4a174e067a9af550dba2b851b647a2f354ce54c /Doc/library/weakref.rst
parent25bbe5e0bce2d8b6c96a5e632bdb9007a0bf0954 (diff)
downloadcpython-c3afba104aef5032e114b4f5cac0a3bbdfef2bba.zip
cpython-c3afba104aef5032e114b4f5cac0a3bbdfef2bba.tar.gz
cpython-c3afba104aef5032e114b4f5cac0a3bbdfef2bba.tar.bz2
Issue #14631: Add a new :class:`weakref.WeakMethod` to simulate weak references to bound methods.
Diffstat (limited to 'Doc/library/weakref.rst')
-rw-r--r--Doc/library/weakref.rst29
1 files changed, 29 insertions, 0 deletions
diff --git a/Doc/library/weakref.rst b/Doc/library/weakref.rst
index 224f442..1bf6b58 100644
--- a/Doc/library/weakref.rst
+++ b/Doc/library/weakref.rst
@@ -192,6 +192,35 @@ These method have the same issues as the and :meth:`keyrefs` method of
discarded when no strong reference to it exists any more.
+.. class:: WeakMethod(method)
+
+ A custom :class:`ref` subclass which simulates a weak reference to a bound
+ method (i.e., a method defined on a class and looked up on an instance).
+ Since a bound method is ephemeral, a standard weak reference cannot keep
+ hold of it. :class:`WeakMethod` has special code to recreate the bound
+ method until either the object or the original function dies::
+
+ >>> class C:
+ ... def method(self):
+ ... print("method called!")
+ ...
+ >>> c = C()
+ >>> r = weakref.ref(c.method)
+ >>> r()
+ >>> r = weakref.WeakMethod(c.method)
+ >>> r()
+ <bound method C.method of <__main__.C object at 0x7fc859830220>>
+ >>> r()()
+ method called!
+ >>> del c
+ >>> gc.collect()
+ 0
+ >>> r()
+ >>>
+
+ .. versionadded:: 3.4
+
+
.. data:: ReferenceType
The type object for weak references objects.