This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: reload() by symbol name
Type: enhancement Stage:
Components: Interpreter Core, Library (Lib) Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, techtonik, vstinner
Priority: normal Keywords:

Created on 2013-11-27 11:10 by techtonik, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg204573 - (view) Author: anatoly techtonik (techtonik) Date: 2013-11-27 11:10
It would be nice if reload() supported reloading of symbols imported with "from module import ..." syntax. It is quite useful for development sessions, when you patch and test your function on some set of unexpected input.

>>> from astdump import dumpattrs as dat
>>> import imp
>>> imp.reload(dat)
TypeError: reload() argument must be module
>>> imp.reload(dumpattrs)
NameError: name 'dumpattrs' is not defined
>>> imp.reload(astdump)
NameError: name 'astdump' is not defined
>>>
msg204574 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-11-27 11:19
You can retrieve the module using sys.modules[symbol.__module__].

Example:

>>> from os import environ as ENV
>>> import imp, sys
>>> imp.reload(sys.modules[ENV.__module__])
<module 'os' from '/usr/lib64/python2.7/os.pyc'>
>>> _.environ is ENV
False

But if you import symbols instead of modules, symbols will still point to the old version of the module. This explain the final "False" result.

You should import modules if you want to play with reload().

I prefer to exit and restart Python instead of playing with reload, I see it as a hack which has many issues like "_.environ is ENV" returning False.
msg204600 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-11-27 15:54
As Victor pointed out, this is not really feasible at the object level. You can work your way back up to reload a whole module, but it won't update the object you passed in, which is just going to confuse users.
History
Date User Action Args
2022-04-11 14:57:54adminsetgithub: 64011
2013-11-27 15:54:05brett.cannonsetstatus: open -> closed
versions: - Python 2.7
nosy: + brett.cannon

messages: + msg204600

resolution: rejected
2013-11-27 11:19:52vstinnersetnosy: + vstinner
messages: + msg204574
2013-11-27 11:10:56techtonikcreate