How to "reimport" an object from a Python module?

1

I wrote a module called mymodule.py which has the function

def funcion(x):
     return x

So I imported this function into a code like

from mymodule import function

and worked normally.

Then I wanted to change the function to

def function(x):
     return 2*x

but the code continues to work as in the previous version of function , with return x . How do I reimport the function object without restarting the kernel? Do you know of any function similar to importlib.reload() for objects in Python 3?

    
asked by anonymous 27.11.2018 / 17:57

2 answers

2

Before the possible solution, it is good to know: The reload process is not free from flaws because if it is true that the variables are updated, it is not guaranteed that the old ones are removed, for example if you rename some, the old name may continue to exist, if you change types of objects in class definitions, existing objects can continue with their old types. * So beware of reload, although sometimes it's convenient.

Version 3.0 to 3.3:

imp.reload(module)

Version 3.4+

importlib.reload(module)

* How did I find this? using a lot of the jupyter notebook and checking for several flaws in the reload process, after some research I saw that I was not alone. (i.e.     

27.11.2018 / 18:31
0

If you are using jupyter , simply press "Shift" + "Enter" in the cell, you will see that the number of brackets will increase, this means that the function has been "relied" and that is now available with the new syntax (the same goes for variable definitions or any code snippet inside a cell).

    
01.12.2018 / 13:00