Unexpected side-effects of assigning to sys.modules[__name__]

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Aug 6 18:09:14 EDT 2009


On Thu, 06 Aug 2009 20:01:42 +0200, Jean-Michel Pichavant wrote:


> > I'm completely perplexed by this behaviour. sys.modules() seems to be
> > a regular dict, at least according to type(), and yet assigning to an
> > item of it seems to have unexpected, and rather weird, side-effects.
> >
> > What am I missing?
> >
> >
> >
> >
> Maybe when you assign 123 to sys.modules[__name__], you've removed the
> last reference on <module '__main__' from 'funny.py'> and it is
> garbaged. You are then loosing all your initial namespace.

By Jove, I think you've got it! How obvious in hindsight. Thank you.





-- 
Steven



More information about the Python-list mailing list