weakref | index

`Weak reference support for Python.
This module is an implementation of PEP 205:
https://peps.python.org/pep-0205/`

modules-decor heading-text">
Modules

modules-decor">

collections.abc | itertools | sys |

classes-decor heading-text">
Classes

classes-decor">

object
  CallableProxyType
  ProxyType
  ReferenceType
    KeyedRef
    WeakMethod
  finalize
MutableMapping(Mapping)
  WeakKeyDictionary
  WeakValueDictionary

class CallableProxyType(object)

Methods defined here:

__abs__(self, /)
  abs(self)

__add__(self, value, /)
  Return self+value.

__and__(self, value, /)
  Return self&value.

__bool__(self, /)
  True if self else False

__call__(self, /, *args, **kwargs)
  Call self as a function.

__contains__(self, key, /)
  Return bool(key in self).

__delattr__(self, name, /)
  Implement delattr(self, name).

__delitem__(self, key, /)
  Delete self[key].

__divmod__(self, value, /)
  Return divmod(self, value).

__eq__(self, value, /)
  Return self==value.

__float__(self, /)
  float(self)

__floordiv__(self, value, /)
  Return self//value.

__ge__(self, value, /)
  Return self>=value.

__getattribute__(self, name, /)
  Return getattr(self, name).

__getitem__(self, key, /)
  Return self[key].

__gt__(self, value, /)
  Return self>value.

__iadd__(self, value, /)
  Return self+=value.

__iand__(self, value, /)
  Return self&=value.

__ifloordiv__(self, value, /)
  Return self//=value.

__ilshift__(self, value, /)
  Return self<<=value.

__imatmul__(self, value, /)
  Return self@=value.

__imod__(self, value, /)
  Return self%=value.

__imul__(self, value, /)
  Return self*=value.

__index__(self, /)
  Return self converted to an integer, if self is suitable for use as an index into a list.

__int__(self, /)
  int(self)

__invert__(self, /)
  ~self

__ior__(self, value, /)
  Return self|=value.

__ipow__(self, value, /)
  Return self**=value.

__irshift__(self, value, /)
  Return self>>=value.

__isub__(self, value, /)
  Return self-=value.

__iter__(self, /)
  Implement iter(self).

__itruediv__(self, value, /)
  Return self/=value.

__ixor__(self, value, /)
  Return self^=value.

__le__(self, value, /)
  Return self<=value.

__len__(self, /)
  Return len(self).

__lshift__(self, value, /)
  Return self<<value.

__lt__(self, value, /)
  Return self<value.

__matmul__(self, value, /)
  Return self@value.

__mod__(self, value, /)
  Return self%value.

__mul__(self, value, /)
  Return self*value.

__ne__(self, value, /)
  Return self!=value.

__neg__(self, /)
  -self

__next__(self, /)
  Implement next(self).

__or__(self, value, /)
  Return self|value.

__pos__(self, /)
  +self

__pow__(self, value, mod=None, /)
  Return pow(self, value, mod).

__radd__(self, value, /)
  Return value+self.

__rand__(self, value, /)
  Return value&self.

__rdivmod__(self, value, /)
  Return divmod(value, self).

__repr__(self, /)
  Return repr(self).

__rfloordiv__(self, value, /)
  Return value//self.

__rlshift__(self, value, /)
  Return value<<self.

__rmatmul__(self, value, /)
  Return value@self.

__rmod__(self, value, /)
  Return value%self.

__rmul__(self, value, /)
  Return value*self.

__ror__(self, value, /)
  Return value|self.

__rpow__(self, value, mod=None, /)
  Return pow(value, self, mod).

__rrshift__(self, value, /)
  Return value>>self.

__rshift__(self, value, /)
  Return self>>value.

__rsub__(self, value, /)
  Return value-self.

__rtruediv__(self, value, /)
  Return value/self.

__rxor__(self, value, /)
  Return value^self.

__setattr__(self, name, value, /)
  Implement setattr(self, name, value).

__setitem__(self, key, value, /)
  Set self[key] to value.

__str__(self, /)
  Return str(self).

__sub__(self, value, /)
  Return self-value.

__truediv__(self, value, /)
  Return self/value.

__xor__(self, value, /)
  Return self^value.

Data and other attributes defined here:

__hash__ = None

class KeyedRef(ReferenceType)
| KeyedRef(ob, callback, key) Specialized reference that includes a key corresponding to the value. This is used in the WeakValueDictionary to avoid having to create a function object for each key stored in the mapping. A shared callback object can use the 'key' attribute of a KeyedRef instead of getting a reference to the key from an enclosing scope.

Method resolution order:
  KeyedRef
  ReferenceType
  object

Methods defined here:

__init__(self, ob, callback, key)
  Initialize self. See help(type(self)) for accurate signature.

Static methods defined here:

__new__(type, ob, callback, key)
  Create and return a new object. See help(type) for accurate signature.

Data descriptors defined here:

key

Methods inherited from ReferenceType:

__call__(self, /, *args, **kwargs)
  Call self as a function.

__eq__(self, value, /)
  Return self==value.

__ge__(self, value, /)
  Return self>=value.

__gt__(self, value, /)
  Return self>value.

__hash__(self, /)
  Return hash(self).

__le__(self, value, /)
  Return self<=value.

__lt__(self, value, /)
  Return self<value.

__ne__(self, value, /)
  Return self!=value.

__repr__(self, /)
  Return repr(self).

Class methods inherited from ReferenceType:

__class_getitem__(object, /)
  Weakrefs are generic over the type of the referenced object.

Data descriptors inherited from ReferenceType:

__callback__

class ProxyType(object)

Methods defined here:

__abs__(self, /)
  abs(self)

__add__(self, value, /)
  Return self+value.

__and__(self, value, /)
  Return self&value.

__bool__(self, /)
  True if self else False

__bytes__(self, /)

__contains__(self, key, /)
  Return bool(key in self).

__delattr__(self, name, /)
  Implement delattr(self, name).

__delitem__(self, key, /)
  Delete self[key].

__divmod__(self, value, /)
  Return divmod(self, value).

__eq__(self, value, /)
  Return self==value.

__float__(self, /)
  float(self)

__floordiv__(self, value, /)
  Return self//value.

__ge__(self, value, /)
  Return self>=value.

__getattribute__(self, name, /)
  Return getattr(self, name).

__getitem__(self, key, /)
  Return self[key].

__gt__(self, value, /)
  Return self>value.

__iadd__(self, value, /)
  Return self+=value.

__iand__(self, value, /)
  Return self&=value.

__ifloordiv__(self, value, /)
  Return self//=value.

__ilshift__(self, value, /)
  Return self<<=value.

__imatmul__(self, value, /)
  Return self@=value.

__imod__(self, value, /)
  Return self%=value.

__imul__(self, value, /)
  Return self*=value.

__index__(self, /)
  Return self converted to an integer, if self is suitable for use as an index into a list.

__int__(self, /)
  int(self)

__invert__(self, /)
  ~self

__ior__(self, value, /)
  Return self|=value.

__ipow__(self, value, /)
  Return self**=value.

__irshift__(self, value, /)
  Return self>>=value.

__isub__(self, value, /)
  Return self-=value.

__iter__(self, /)
  Implement iter(self).

__itruediv__(self, value, /)
  Return self/=value.

__ixor__(self, value, /)
  Return self^=value.

__le__(self, value, /)
  Return self<=value.

__len__(self, /)
  Return len(self).

__lshift__(self, value, /)
  Return self<<value.

__lt__(self, value, /)
  Return self<value.

__matmul__(self, value, /)
  Return self@value.

__mod__(self, value, /)
  Return self%value.

__mul__(self, value, /)
  Return self*value.

__ne__(self, value, /)
  Return self!=value.

__neg__(self, /)
  -self

__next__(self, /)
  Implement next(self).

__or__(self, value, /)
  Return self|value.

__pos__(self, /)
  +self

__pow__(self, value, mod=None, /)
  Return pow(self, value, mod).

__radd__(self, value, /)
  Return value+self.

__rand__(self, value, /)
  Return value&self.

__rdivmod__(self, value, /)
  Return divmod(value, self).

__repr__(self, /)
  Return repr(self).

__reversed__(self, /)

__rfloordiv__(self, value, /)
  Return value//self.

__rlshift__(self, value, /)
  Return value<<self.

__rmatmul__(self, value, /)
  Return value@self.

__rmod__(self, value, /)
  Return value%self.

__rmul__(self, value, /)
  Return value*self.

__ror__(self, value, /)
  Return value|self.

__rpow__(self, value, mod=None, /)
  Return pow(value, self, mod).

__rrshift__(self, value, /)
  Return value>>self.

__rshift__(self, value, /)
  Return self>>value.

__rsub__(self, value, /)
  Return value-self.

__rtruediv__(self, value, /)
  Return value/self.

__rxor__(self, value, /)
  Return value^self.

__setattr__(self, name, value, /)
  Implement setattr(self, name, value).

__setitem__(self, key, value, /)
  Set self[key] to value.

__str__(self, /)
  Return str(self).

__sub__(self, value, /)
  Return self-value.

__truediv__(self, value, /)
  Return self/value.

__xor__(self, value, /)
  Return self^value.

Data and other attributes defined here:

__hash__ = None

class ReferenceType(object)

Methods defined here:

__call__(self, /, *args, **kwargs)
  Call self as a function.

__eq__(self, value, /)
  Return self==value.

__ge__(self, value, /)
  Return self>=value.

__gt__(self, value, /)
  Return self>value.

__hash__(self, /)
  Return hash(self).

__init__(self, /, *args, **kwargs)
  Initialize self. See help(type(self)) for accurate signature.

__le__(self, value, /)
  Return self<=value.

__lt__(self, value, /)
  Return self<value.

__ne__(self, value, /)
  Return self!=value.

__repr__(self, /)
  Return repr(self).

Class methods defined here:

__class_getitem__(object, /)
  Weakrefs are generic over the type of the referenced object.

Static methods defined here:

__new__(*args, **kwargs)
  Create and return a new object. See help(type) for accurate signature.

Data descriptors defined here:

__callback__

class WeakKeyDictionary(MutableMapping)
| WeakKeyDictionary(dict=None) Mapping class that references keys weakly. Entries in the dictionary will be discarded when there is no longer a strong reference to the key. This can be used to associate additional data with an object owned by other parts of an application without adding attributes to those objects. This can be especially useful with objects that override attribute accesses.

Method resolution order:
  WeakKeyDictionary
  MutableMapping
  Mapping
  Collection
  Sized
  Iterable
  Container
  object

Methods defined here:

__contains__(self, key)

__copy__ = copy(self)

__deepcopy__(self, memo)

__delitem__(self, key)

__getitem__(self, key)

__init__(self, dict=None)
  Initialize self. See help(type(self)) for accurate signature.

__ior__(self, other)

__iter__ = keys(self)

__len__(self)

__or__(self, other)
  Return self|value.

__repr__(self)
  Return repr(self).

__ror__(self, other)
  Return value|self.

__setitem__(self, key, value)

copy(self)

get(self, key, default=None)
  D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.

items(self)
  D.items() -> a set-like object providing a view on D's items

keyrefs(self)
  Return a list of weak references to the keys.
  The references are not guaranteed to be 'live' at the time
  they are used, so the result of calling the references needs
  to be checked before being used. This can be used to avoid
  creating references that will cause the garbage collector to
  keep the keys around longer than needed.

keys(self)
  D.keys() -> a set-like object providing a view on D's keys

pop(self, key, *args)
  D.pop(k[,d]) -> v, remove specified key and return the corresponding
  value. If key is not found, d is returned if given, otherwise
  KeyError is raised.

popitem(self)
  D.popitem() -> (k, v), remove and return some (key, value) pair
  as a 2-tuple; but raise KeyError if D is empty.

setdefault(self, key, default=None)
  D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D

update(self, dict=None, /, **kwargs)
  D.update([E, ]**F) -> None. Update D from mapping/iterable E and F.
  If E present and has a .keys() method, does:
  for k in E.keys(): D[k] = E[k]
  If E present and lacks .keys() method, does:
  for (k, v) in E: D[k] = v
  In either case, this is followed by:
  for k, v in F.items(): D[k] = v

values(self)
  D.values() -> an object providing a view on D's values

Data descriptors defined here:

__dict__
  dictionary for instance variables

__weakref__
  list of weak references to the object

Data and other attributes defined here:

__abstractmethods__ = frozenset()

Methods inherited from MutableMapping:

clear(self)
  D.clear() -> None. Remove all items from D.

Methods inherited from Mapping:

__eq__(self, other)
  Return self==value.

Data and other attributes inherited from Mapping:

__hash__ = None

__reversed__ = None

Class methods inherited from Collection:

__subclasshook__(C)
  Abstract classes can override this to customize issubclass().
  This is invoked early on by abc.ABCMeta.__subclasscheck__().
  It should return True, False or NotImplemented. If it returns
  NotImplemented, the normal algorithm is used. Otherwise, it
  overrides the normal algorithm (and the outcome is cached).

Class methods inherited from Iterable:

__class_getitem__ = GenericAlias(args, /)
  Represent a PEP 585 generic type
  E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).

class WeakMethod(ReferenceType)
| WeakMethod(meth, callback=None) A custom `weakref.ref` subclass which simulates a weak reference to a bound method, working around the lifetime problem of bound methods.

Method resolution order:
  WeakMethod
  ReferenceType
  object

Methods defined here:

__call__(self)
  Call self as a function.

__eq__(self, other)
  Return self==value.

__hash__(self, /) from ReferenceType
  Return hash(self).

__ne__(self, other)
  Return self!=value.

Static methods defined here:

__new__(cls, meth, callback=None)
  Create and return a new object. See help(type) for accurate signature.

Data descriptors defined here:

__weakref__
  list of weak references to the object

Methods inherited from ReferenceType:

__ge__(self, value, /)
  Return self>=value.

__gt__(self, value, /)
  Return self>value.

__init__(self, /, *args, **kwargs)
  Initialize self. See help(type(self)) for accurate signature.

__le__(self, value, /)
  Return self<=value.

__lt__(self, value, /)
  Return self<value.

__repr__(self, /)
  Return repr(self).

Class methods inherited from ReferenceType:

__class_getitem__(object, /)
  Weakrefs are generic over the type of the referenced object.

Data descriptors inherited from ReferenceType:

__callback__

class WeakValueDictionary(MutableMapping)
| WeakValueDictionary(other=(), /, **kw) Mapping class that references values weakly. Entries in the dictionary will be discarded when no strong reference to the value exists anymore

Method resolution order:
  WeakValueDictionary
  MutableMapping
  Mapping
  Collection
  Sized
  Iterable
  Container
  object

Methods defined here:

__contains__(self, key)

__copy__ = copy(self)

__deepcopy__(self, memo)

__delitem__(self, key)

__getitem__(self, key)

__init__(self, other=(), /, **kw)
  Initialize self. See help(type(self)) for accurate signature.

__ior__(self, other)

__iter__ = keys(self)

__len__(self)

__or__(self, other)
  Return self|value.

__repr__(self)
  Return repr(self).

__ror__(self, other)
  Return value|self.

__setitem__(self, key, value)

copy(self)

get(self, key, default=None)
  D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.

items(self)
  D.items() -> a set-like object providing a view on D's items

itervaluerefs(self)
  Return an iterator that yields the weak references to the values.
  The references are not guaranteed to be 'live' at the time
  they are used, so the result of calling the references needs
  to be checked before being used. This can be used to avoid
  creating references that will cause the garbage collector to
  keep the values around longer than needed.

keys(self)
  D.keys() -> a set-like object providing a view on D's keys

pop(self, key, *args)
  D.pop(k[,d]) -> v, remove specified key and return the corresponding
  value. If key is not found, d is returned if given, otherwise
  KeyError is raised.

popitem(self)
  D.popitem() -> (k, v), remove and return some (key, value) pair
  as a 2-tuple; but raise KeyError if D is empty.

setdefault(self, key, default=None)
  D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D

update(self, other=None, /, **kwargs)
  D.update([E, ]**F) -> None. Update D from mapping/iterable E and F.
  If E present and has a .keys() method, does:
  for k in E.keys(): D[k] = E[k]
  If E present and lacks .keys() method, does:
  for (k, v) in E: D[k] = v
  In either case, this is followed by:
  for k, v in F.items(): D[k] = v

valuerefs(self)
  Return a list of weak references to the values.
  The references are not guaranteed to be 'live' at the time
  they are used, so the result of calling the references needs
  to be checked before being used. This can be used to avoid
  creating references that will cause the garbage collector to
  keep the values around longer than needed.

values(self)
  D.values() -> an object providing a view on D's values

Data descriptors defined here:

__dict__
  dictionary for instance variables

__weakref__
  list of weak references to the object

Data and other attributes defined here:

__abstractmethods__ = frozenset()

Methods inherited from MutableMapping:

clear(self)
  D.clear() -> None. Remove all items from D.

Methods inherited from Mapping:

__eq__(self, other)
  Return self==value.

Data and other attributes inherited from Mapping:

__hash__ = None

__reversed__ = None

Class methods inherited from Collection:

__subclasshook__(C)
  Abstract classes can override this to customize issubclass().
  This is invoked early on by abc.ABCMeta.__subclasscheck__().
  It should return True, False or NotImplemented. If it returns
  NotImplemented, the normal algorithm is used. Otherwise, it
  overrides the normal algorithm (and the outcome is cached).

Class methods inherited from Iterable:

__class_getitem__ = GenericAlias(args, /)
  Represent a PEP 585 generic type
  E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).

class finalize(object)

finalize(obj, func, /, *args, **kwargs)
Class for finalization of weakrefable objects
finalize(obj, func, *args, **kwargs) returns a callable finalizer
object which will be called when obj is garbage collected. The
first time the finalizer is called it evaluates func(*arg, **kwargs)
and returns the result. After this the finalizer is dead, and
calling it just returns None.
When the program exits any remaining finalizers for which the
atexit attribute is true will be run in reverse order of creation.
By default atexit is true.

Methods defined here:

__call__(self, _=None)
  If alive then mark as dead and return func(*args, **kwargs);
  otherwise return None

__init__(self, obj, func, /, *args, **kwargs)
  Initialize self. See help(type(self)) for accurate signature.

__repr__(self)
  Return repr(self).

detach(self)
  If alive then mark as dead and return (obj, func, args, kwargs);
  otherwise return None

peek(self)
  If alive then return (obj, func, args, kwargs);
  otherwise return None

Readonly properties defined here:

alive
  Whether finalizer is alive

Data descriptors defined here:

atexit
  Whether finalizer should be called at exit

ref = class ReferenceType(object)

Methods defined here:

__call__(self, /, *args, **kwargs)
  Call self as a function.

__eq__(self, value, /)
  Return self==value.

__ge__(self, value, /)
  Return self>=value.

__gt__(self, value, /)
  Return self>value.

__hash__(self, /)
  Return hash(self).

__init__(self, /, *args, **kwargs)
  Initialize self. See help(type(self)) for accurate signature.

__le__(self, value, /)
  Return self<=value.

__lt__(self, value, /)
  Return self<value.

__ne__(self, value, /)
  Return self!=value.

__repr__(self, /)
  Return repr(self).

Class methods defined here:

__class_getitem__(object, /)
  Weakrefs are generic over the type of the referenced object.

Static methods defined here:

__new__(*args, **kwargs)
  Create and return a new object. See help(type) for accurate signature.

Data descriptors defined here:

__callback__

functions-decor heading-text">
Functions

functions-decor">

getweakrefcount(object, /)
  Return the number of weak references to 'object'.

getweakrefs(object, /)
  Return a list of all weak reference objects pointing to 'object'.

proxy(object, callback=None, /)
  Create a proxy object that weakly references 'object'.
  'callback', if given, is called with a reference to the
  proxy when 'object' is about to be finalized.

data-decor heading-text">
Data
data-decor"> |  | ProxyTypes = (<class 'weakref.ProxyType'>, <class 'weakref.CallableProxyType'>) __all__ = ['ref', 'proxy', 'getweakrefcount', 'getweakrefs', 'WeakKeyDictionary', 'ReferenceType', 'ProxyType', 'CallableProxyType', 'ProxyTypes', 'WeakValueDictionary', 'WeakSet', 'WeakMethod', 'finalize']