human
I am working on the file "Section 2 - Advance Python/Chapter S2.12 - Weak Reference, Copy/Weak Reference.ipynb".
The first task is:
Reference Callbacks
The ref constructor accepts an optional callback function that is invoked when the referenced object is deleted.
Can you write Python code for it?
assistant
#Here is the Python code you requested.

import weakref

class ExpensiveObject:

    def __del__(self):
        print('(Deleting {})'.format(self))

def callback(reference):
    """Invoked when referenced object is deleted"""
    print('callback({!r})'.format(reference))

obj = ExpensiveObject()
r = weakref.ref(obj, callback)

print('obj:', obj)
print('ref:', r)
print('r():', r())

print('deleting obj')
del obj
print('r():', r())