Python Weak References

PythonProgrammingServer Side Programming

To create weak references in python, we need to use the weakref module. The weakref is not sufficient to keep the object alive. A basic use of the weak reference is to implement cache or mappings for a large object.

To use this module, we should import it using −

import weakref

Not all objects can be weakly referenced. Some built-in types like tuple or int, do not support weak reference. There are some classes and methods related to weak reference.

Class weakref.ref(object[, callback])

It will return a weak reference to the object. When the referent is still alive, the actual object can be retrieved by calling the reference object, but when the actual object is not present, it will return None.

Method weakref.proxy(object[, callback])

This method is used to return a proxy for the object, which are using weak reference. The returned object may be either ProxyType or CallableProxyType.

Method weakref.getweakrefcount(object)

This method is used to return the number of weak references and proxies of the objects.

Method weakref.getweakrefs(object)

This method is used to return a list of weak references and proxies objects.

Example Code

Live Demo

import weakref
class my_list(list):
new_list = my_list('String') #Use my_list class to define a list
weak_ref = weakref.ref(new_list)
new_weak_list = weak_ref()
new_proxy = weakref.proxy(new_list)
print('The object using proxy: ' + str(new_proxy))
if new_list is new_weak_list:
   print("There is a weak reference")
print('The Number of Weak references: ' + str(weakref.getweakrefcount(new_list)))
del new_list, new_weak_list #Delete both objects
print("The weak reference is: " + str(weak_ref()))


['S', 't', 'r', 'i', 'n', 'g']
['S', 't', 'r', 'i', 'n', 'g']
The object using proxy: ['S', 't', 'r', 'i', 'n', 'g']
There is a weak reference
The Number of Weak references: 2
The weak reference is: None
Published on 31-Oct-2018 11:41:36