Class doesn't update on Shift+Enter

Hi Everyone
When I define a class in a notebook cell, a method’s code is not updated, I have to restart the kernel. Without a class, a function is updated just fine

Can you share more details about this? (A minimal reproducible example where you clearly detail separate cells and how you run them would help a lot.)

Assuming we’re talking about an IPython/ipykernel, it’s generally the case that instances of a class defined in the current namespace won’t magically become a new class (with the same name) after editing/overloading the class definition, but all new instances will have the new implementation.

An (awkward) approach is to split the interface and implementation, as the global namespace, used inside a method body will always be used:

class A:
    def do_a_thing(self, x):
        return _do_a_thing(self, x)

def _do_a_thing(self: A, x):
    return x.upper()

a = A()
a.do_a_thing("x") # see X

def _do_a_thing(self: A, x):
    return x.upper() * 2

a.do_a_thing("x") # see XX

That little bit of type hinting will go a long way to making interactive development bearable.

3 Likes