Weak References in Python
Last Updated :
27 Apr, 2022
Python's memory management algorithm uses a reference counting mechanism for garbage collection, which tracks all reachable objects. All Python objects include a reference count field, which counts how many objects are referencing it. If an object is referenced by another object, then its counter is set to a non-zero number and the object can't be garbage collected. If the counter reaches zero, the garbage collector will free that object.
Below is a short example of how can we find the reference count of any object in python.
Python3
import ctypes
# list object which is referenced by
# my_list
my_list = [1, 2, 3]
# finding the id of list object
my_list_address = id(my_list)
# finds reference count of my_list
ref_count = ctypes.c_long.from_address(my_list_address).value
print(f"Reference count for my_list is: {ref_count}")
Output:
Reference count for my_list is: 1
So, now as we are clear with the basics of references and garbage collections let's move to what are weak references and why do we need them?
What is Weak Reference?
Unlike the references we discussed above, a weak reference is a reference that does not protect the object from getting garbage collected. Why do we want such a thing in the first place?
There are two main applications of weak references:
- Implement caches for large objects (weak dictionaries).
- Reduction of Pain from circular references.
To create weak references, Python has provided us with a module named weakref. A point to keep in mind before using weakref is that some builtins such as tuple or int do not support this. list and dict support is either but we can add support through subclassing. Let's discuss the applications in detail.
Weakref module
Sometimes a large object is stored in the cache so there is no need to keep it alive. Let's you have a large number of image objects and they are being used as keys to map to images due to which these objects will be kept alive.
Fortunately, weakref module provides something called as WeakKeyDictionary and WeakValueDictionary doesn't keep the objects alive as they appear in the mapping objects.
The following classes and methods are provided by weakref module:
- class weakref.ref(object[, callback]) - This returns a weak reference to the object.
- weakref.proxy(object[, callback]) - This returns a proxy to object which uses a weak reference.
- weakref.getweakrefcount(object) - Return the number of weak references and proxies which refer to object.
- weakref.getweakrefs(object) - Return a list of all weak reference and proxy objects which refer to object.
Let's understand the work with some examples:
Example 1:
In the below-given example, we create a normal list object, a weak reference list object, and a proxy list object and print the weak reference count for all of them.
Python3
# importing weakref module
import weakref
# creating a class
class GFG(list):
pass
# creating object of a class
obj = GFG("Geeks")
# creating a normal list object
normal_list = obj
print(f"This is a normal object: {normal_list}")
# this returns a weak reference to obj
weak_list = weakref.ref(obj)
weak_list_obj = weak_list()
print(f"This is a object created using weak reference: {weak_list_obj}")
# creating a proxy of original object
proxy_list = weakref.proxy(obj)
print(f"This is a proxy object: {proxy_list}")
# printing the count of weak references
for objects in [normal_list, weak_list_obj, proxy_list]:
print(f"Number of weak references: {weakref.getweakrefcount(objects)}")
Output:
This is a normal object: ['G', 'e', 'e', 'k', 's']
This is a object created using weak reference: ['G', 'e', 'e', 'k', 's']
This is a proxy object: ['G', 'e', 'e', 'k', 's']
Number of weak references: 2
Number of weak references: 2
Number of weak references: 0
As the normal object has references to the weak reference object hence both of them have a weak reference count of 2 but as proxy_object is a proxy created from the weak reference thus it does not have a reference count.
Example 2:
In this example, we will see how to use WeakValueDictionary which we discussed earlier in the article.
Python3
# import weakref module
import weakref
# creates a class
class GFG:
def __init__(self,data):
self.data = data
def __repr__(self):
return str(self.data)
# creates an object of class GFG
# (consider that this object is very
# large and has been stored in the cache)
value = GFG(5)
# creates a Weak Value Dictionary
weak_dict = weakref.WeakValueDictionary()
# inserting value into the dictionary
weak_dict["num"] = value
# getting the weak ref count
print(f'Weak reference count is: {weakref.getweakrefcount(weak_dict)}')
# deleting the weak dictionary
del weak_dict
# running this will generate error
# print(weak_dict)
Output:
Weak reference count is: 1
In the above example, we assumed that there is a really large object which has been placed in the cache so we have created a weak ref dictionary to store that key and value pair so it will be garbage collected.
Similar Reads
Retrieve elements from Python Set
In this article, we will discuss the different examples of how to retrieve an element from a Python set. Sets are non-linear and unordered data structures so we can't directly access its elements using indices like we do in lists. However there are several ways to do it, here are some examples.Using
3 min read
Unpacking a Tuple in Python
Tuple unpacking is a powerful feature in Python that allows you to assign the values of a tuple to multiple variables in a single line. This technique makes your code more readable and efficient. In other words, It is a process where we extract values from a tuple and assign them to variables in a s
2 min read
Python Set - remove() method
Python remove() Function is a built-in method to remove elements from the set. remove() method takes exactly one argument. Syntax set.remove(element) If the element passed to the remove() is present in the set then the element will be removed from the set. If the element passed to the remove() is no
1 min read
Scientific Computing with Python
Scientific computing refers to the use of computational techniques and tools to solve scientific and engineering problems. Python has become one of the most popular languages for scientific computing due to its simplicity, readability and the libraries used for various scientific tasks. From data an
5 min read
Python Version History
Python, one of the most popular programming languages today, has a rich history of development and evolution. From its inception in the late 1980s to its current status as a versatile and powerful language, Python's version history reflects the language's adaptability and the community's dedication
5 min read
Python Data Structures
Data Structures are a way of organizing data so that it can be accessed more efficiently depending upon the situation. Data Structures are fundamentals of any programming language around which a program is built. Python helps to learn the fundamental of these data structures in a simpler way as comp
15+ min read
Undefined Variable Nameerror In Python
Encountering the "NameError: name 'var' is not defined" is a common experience for Python users. In Python, variable declaration is a straightforward process of assigning a value. This error arises when attempting to access a non-existent variable or one not defined in the program. Surprisingly, it
5 min read
set() Function in python
set() function in Python is used to create a set, which is an unordered collection of unique elements. Sets are mutable, meaning elements can be added or removed after creation. However, all elements inside a set must be immutable, such as numbers, strings or tuples. The set() function can take an i
3 min read
Single and Double Underscores in Python
In Python, naming conventions play a crucial role in code readability and maintainability. Single and double underscores, when used in names, convey specific meanings and conventions. These naming conventions are widely adopted in the Python community and are often utilized in various contexts, incl
3 min read
Best way to learn python
Python is a versatile and beginner-friendly programming language that has become immensely popular for its readability and wide range of applications. Whether you're aiming to start a career in programming or just want to expand your skill set, learning Python is a valuable investment of your time.
11 min read