Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'interpreter': 0.05; 'output': 0.05; '"""': 0.07; 'string': 0.09; 'exec': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:module': 0.09; 'way:': 0.09; 'def': 0.12; '.py': 0.16; '__del__': 0.16; 'a()': 0.16; 'dict': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:created': 0.16; 'wrote:': 0.18; 'module': 0.19; 'patrick': 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'received:comcast.net': 0.24; 'file.': 0.24; 'source': 0.25; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'code': 0.31; 'prints': 0.31; 'subject:skip:i 10': 0.31; 'class': 0.32; 'run': 0.32; 'another': 0.32; 'running': 0.33; 'skip:_ 10': 0.34; 'subject:with': 0.35; 'done': 0.36; 'method': 0.36; 'being': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'enough': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'new': 0.61; 'charset:windows-1252': 0.65; 'between': 0.67 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ned Batchelder Subject: Re: unloading a module created with imp.new_module Date: Mon, 24 Nov 2014 06:44:10 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: c-50-133-228-126.hsd1.ma.comcast.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1416829460 news.xs4all.nl 2929 [2001:888:2000:d::a6]:52476 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81452 On 11/23/14 5:10 AM, Patrick Stinson wrote: > I am defining a single class with a destructor method that prints > ‘__del__’, and running that source code string using exec with the > module’s dict like so: > > import rtmidi > importsys > import types > importtime > importgc > > s= """ > class A: > def __del__(self): > print('__del__') > a = A() > """ > > m = types.ModuleType('mine') > exec(s, m.__dict__) > print('deleting...') > m= None > print('done') > > and the output is: > > deleting... > done > __del__ > > I the “__del__" to come between “deleting…” and “done”. This is not > being run from the interactive interpreter by via a .py file. > Let's look at this another way: Why do you need the module to be unloaded? Isn't it enough to have the new code loaded? -- Ned Batchelder, http://nedbatchelder.com