Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

What is weak reference in Python?

Educative Answers Team

Python contains the weakref module that creates a weak reference to an object. If there are no strong references to an object, the garbage collector is free to use the memory for other purposes.

Weak references are used to implement caches and mappings that contain massive data.

It makes use of

  • WeakKeyDictionary and WeakValueDictionary:setup callback functions to notify the weak dictionaries when the garbage collector has collected a memory.
  • WeakSet: implement the functionalities of Set.
  • finalize: register a function that is called when the garbage collector collects the memory.

Note: Only classes, functions written in python, instance methods, sets, frozensets, file objects, generators, type objects, sockets, arrays, deques, regex pattern objects, and code objects can be weakly referenced.

Functions

The functions within the weakref class are defined below:

class weakref.ref(object[,callback])

Returns weak reference to the object. The original can be called by calling the referent object if it exists; else, None is returned.

Many weak references can be created for the same object. Callbacks, however, will be called for all references from the most recently registered callback.

Exceptions will be noted on the standard error output like the object’s _del_() method.

Weak references are hashable, but only if the hash() is called before the object was deleted; else, a TypeError is raised.

  • _callback_

    Returns callback that is currently associated with the weakref if it exists; otherwise, it returns None.

  • weakref.proxy(object[,callback])

    Returns a proxy to the object that uses a weak reference. The returned object has type ProxyType or CallableProxyType, depending on if the object is callable.

    These objects are not hashable.

  • weakref.getweakrefcount(object)

    Returns the number of weak references and proxies that an object refers to.

  • weakref.getweakrefs(object)

    Returns list of all weak references and proxy obj that refer to the object.

class weakref.WeakKeyDictionary([dict])

This class is the mapping class that references keys using weak references. It is used to associate additional data with an object without associating attributes to the object. This class is especially useful with objects that override attribute accesses.

  • WeakKeyDictionary([dict])

    Returns an iterable of the weak references to the keys.

class weakref.WeakValueDictionary([dict])

It is a mapping class that weakly references values. It has an additional method like the keyrefs()method.

  • WeakValueDictionary.valuerefs()

    Returns an iterable of the weak references to the value.

class weakref.WeakSet([elements])

This class is the set class that assigns weak references to its elements.

class weakref.WeakMethod(method)

A reference subclass that weakly references to a bound methoda method defined on a class and looked up on an instance.

class weakref.finalize(obj, func, *args, **kwarks class weakref.finalize(obj, func, *args, **kwargs)

Returns a function that is called when the garbage collector collects the object. The object survives until the reference object is called. Calling a live finalizer will call the function func( * args, **kwargs); whereas, a dead finalizer returns None.

Exceptions will be noted on the standard error output like the object’s _del_() method.

  • _call_

    Returns function if the object is alive; else, it returns None.

  • detach()

    Returns tuple(obj, func, args, kwargs) if the object is alive; else, it returns None.

  • peak()

    Returns tuple(obj, func, args, kwargs) if the object is alive; else, it returns None.

  • alive

    A property that is true if the finalizer is alive.

  • atexit

    This property is true by default. When a program exists, all live finalizers are called in reverse order of creation, which can be disabled by setting the atexit property to false.

RELATED TAGS

python
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring