import reloading

Dave Angel davea at davea.name
Wed Sep 24 09:58:13 EDT 2014


Robin Becker <robin at reportlab.com> Wrote in message:
> 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.
> 
> 


Please don't misquote me. I have no idea why testmod.A gave you a
 number at all.  Must be a bug in reload. Anyway I talked only
 about import. imp.reload is an abomination useful only to
 occasionally save time in an interactive session.
 

import, on the other hand, had better continue to be consistent. 
 Many modules are imported dozens of times in a typical
 application,  and you wouldn't want multiple instances of
 them.

-- 
DaveA




More information about the Python-list mailing list