What does importlib.reload change for object (Enum) comparisons?

49 views Asked by At

I have stumbled across a weird problem when reloading modules for which I have found a solution on the internet. However, I am missing an explanation of why this problem occurs (especially regarding Enum comparisons). If anyone is wondering why I would use reload(): When I make changes in the modules, they usually seem to require a kernel restart which I want to prevent.

Take the following fruits.py file with the content:

from enum import Enum


class Fruit(Enum):
    Apple = 1
    Banana = 2
    Pear = 3

and try comparing a variable of type Fruit with Fruit.Apple:

(fruits_test.py)

import fruits
from fruits import Fruit
from importlib import reload
reload(fruits)


def check_equality(fruit: Fruit):
    return fruit == Fruit.Apple

If I now try to call fruits_test.check_equality in my Jupyter notebook with the following parameters, it will return False:

import fruits_test
from fruits import Fruit

fruits_test.check_equality(Fruit.Apple)

However, it will work if reload(fruits) is removed. My question is why this is the case.

0

There are 0 answers