import reloading

Robin Becker robin at reportlab.com
Wed Sep 24 09:34:30 EDT 2014


On 24/09/2014 12:54, Dave Angel wrote:
> Robin Becker <robin at reportlab.com> Wrote in message:
>...........
>> Is a loader supposed to reset all the reused module's attributes and reset the
>> __dict__ or does it just run the code from the module in hte __dict__?
>>
>
> Nothing gets reset or run. It simply reuses the existing module object.
>
I find that a bit weird. In fact as a test I created a testmod.py

#######
A=3
print('A=%r' % A)
#######

then in python3.4

Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:24:06) [MSC v.1600 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> import testmod
A=3
 >>> testmod.A
3
 >>> #I externally changed testmod.py to read
 >>> #a=4
 >>> #print('a=%r' % a)
 >>> import imp
 >>> imp.reload(testmod)
a=4
<module 'testmod' from 'C:\\code\\tests\\testmod.py'>
 >>> testmod.A
3
 >>> testmod.a
4
 >>>


so you are right the old module variables are still there. Presumably any 
functions/classes etc etc that don't get overwritten will also continue to exist.


> There are ways to fool the loader, for example by having a module
>   visible in more than one file system path.
> The most common of
>   these bugs  is a module that tries to import the script that's
>   started it all. Because that initial script is named __main__
>   internally,  if you try to import it by filename you end up with
>   two instances.
>
>


-- 
Robin Becker




More information about the Python-list mailing list