Junior — Middle
Have you used weak references in your projects?
sobes.tech AI
Answer from AI
In Python projects, I used weak references to manage objects when I needed to avoid creating strong references that prevent garbage collection. For example, in implementing caches or observers, where objects should be deleted if there are no more strong references to them.
An example of using the weakref module:
import weakref
class MyObject:
pass
obj = MyObject()
weak_obj = weakref.ref(obj)
print(weak_obj()) # Will output the object
del obj
print(weak_obj()) # Now None, as the object has been deleted
This helps prevent memory leaks, especially in complex data structures.