Best practice: Sharing object between different objects

Ian Kelly ian.g.kelly at gmail.com
Mon Feb 23 15:39:52 EST 2015


On Mon, Feb 23, 2015 at 1:02 PM,  <sohcahtoa82 at gmail.com> wrote:
> What's REALLY interesting is that this happens:
>
>>>> import myModule
>>>> myModule.myInt
> 1
>>>> myModule.myInt = 2
>>>> myModule.myInt
> 2
>>>> del myModule
>>>> import myModule
>>>> myModule.myInt
> 2
>
> I would REALLY expect that deleting the module object and then re-importing would reset that variable.

Even though you deleted the module locally, it's still referenced in
the sys.modules cache (as well as in any other place where it might
have been imported). That's the place you need to delete it from if
you really want to re-execute it.

>>> import myModule
>>> myModule.myInt
1
>>> myModule.myInt = 2
>>> myModule.myInt
2
>>> import sys
>>> del sys.modules['myModule']
>>> import myModule
>>> myModule.myInt
1



More information about the Python-list mailing list