Following up on this question regarding reloading a module, how do I reload a specific function from a changed module?
pseudo-code:
from foo import bar
if foo.py has changed:
reload bar
Following up on this question regarding reloading a module, how do I reload a specific function from a changed module?
pseudo-code:
from foo import bar
if foo.py has changed:
reload bar
What you want is possible, but requires reloading two things... first
reload(foo)
, but then you also have toreload(baz)
(assumingbaz
is the name of the module containing thefrom foo import bar
statement).As to why... When
foo
is first loaded, afoo
object is created, containing abar
object. When you importbar
into thebaz
module, it stores a reference tobar
. Whenreload(foo)
is called, thefoo
object is blanked, and the module re-executed. This means allfoo
references are still valid, but a newbar
object has been created... so all references that have been imported somewhere are still references to the oldbar
object. By reloadingbaz
, you cause it to reimport the newbar
.Alternately, you can just do
import foo
in your module, and always callfoo.bar()
. That way whenever youreload(foo)
, you'll get the newestbar
reference.