Jupyter autoreload fails when changing a class

1. About the problem

Why is autoreload not working in this case?

As the error log stated:

setattr(old, name, getattr(new, name))
ValueError: __init__() requires a code object with 0 free vars, not 1

What autoreload does here:

It tries to replace the old __init__() function’s (the function before code changed) code object with the the new __init__() function code object. When I check the free vars of __init__() function before and after code changed, I got the results as below:

Code to check:

Foo.__init__.__code__.co_freevars

Results:

Before code changed: () , no free var.

After code changed: ('__class__',) , there is one free var here. That’s why the error happen. It cannot replace the old object code with the new one.

2. How to solve the problem

In this case, because autoreload remain the old function object so we cannot do anything but only restart the kernel.

Hope this help.

Leave a Comment