Reason for not allowing import twice but allowing reload()
bc at freeuk.com
Mon Feb 29 10:33:25 EST 2016
On 29/02/2016 07:11, Chris Angelico wrote:
> On Mon, Feb 29, 2016 at 6:02 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>>> - if subsequent imports of same module in a session are not effective, why not simply flag those attempts as an error, rather than letting them go effect-less.
>> Because there are legitimate reasons for importing the same module
>> multiple times. For example, I have a script that imports the sys
>> module. It also imports the os module, which imports the sys module.
>> Both the main module of my script and the os module have need of the
>> sys module, so they both import it. If subsequent imports of the
>> module raised an exception, this wouldn't be possible.
> I think the OP's talking more about the situation of having an active
> session (IDLE was mentioned), importing a local module (a .py file
> from the current directory), then editing the file and re-importing,
> which has no effect. While I am sympathetic to the problem, I don't
> believe the language should be changed here; what might be useful is
> something that notices that an already-loaded module is now out of
> date, but really, the best solution is a change of workflow that no
> longer has long-running modules loading live-updated code.
Not in Python (this was pre-Python actually) but I once used such a
technique all the time.
The main set of modules would be running a GUI application which also
had a command line.
Some commands included editing, compiling [a discrete step] and running
modules of the application that were under development. Then you had a
very fast edit-run cycle for those modules without having to restart the
entire application, and possibly reload the graphical data that was
under test (which could be large), or having to do any other set-ups
required for the test.
(Another reason in those days was that not all modules would fit into
memory at once and a module could be unloaded once it had finished (in
Python terms, running off the end of the module) to make room for another.)
What I'm saying is, you shouldn't dismiss something just because you
can't think of enough uses for it.
More information about the Python-list