Delete a function

Steve Holden steve at holdenweb.com
Wed Mar 21 13:43:30 EDT 2007


gtb wrote:
> On Mar 21, 11:37 am, Steve Holden <s... at holdenweb.com> wrote:
>> gtb wrote:
>>> After a function has been imported to a shell how may it be deleted so
>>> that after editing it can reloaded anew?
>> Use the built-in reload() function to reload the module that defines the
>> function.
>>
>> regards
>>   Steve
>> --
>> Steve Holden       +44 150 684 7255  +1 800 494 3119
>> Holden Web LLC/Ltd          http://www.holdenweb.com
>> Skype: holdenweb    http://del.icio.us/steve.holden
>> Recent Ramblings      http://holdenweb.blogspot.com
> 
> Thanks, tried that now and get nameError: with the following.
> 
> import sys
> sys.path.append("c:\maxq\testScripts")
> 
> from CompactTest import CompactTest
> from compactLogin import dvlogin
> 
> reload(compactLogin)
> 
The local compactLogin isn't being bound to the module because of the 
import form you are using.

Here's meta1.py:

$ more meta1.py
class MyMeta(type):
     ...

class MyClass:
     __metaclass__ = MyMeta

Now see what happens when I import the MyMeta class from it:

  >>> from meta1 import MyMeta
  >>> import sys
  >>> sys.modules['meta1']
<module 'meta1' from 'meta1.py'>
  >>> reload(meta1)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
NameError: name 'meta1' is not defined
  >>> reload(sys.modules['meta1'])
<module 'meta1' from 'meta1.pyc'>
  >>>

Note, however, that this reload() call does NOT re-bind the local 
MyMeta, which still references the class defines in the original version 
of the module. You'd be better off using

import compactLogin

and then setting

dvlogin = compactLogin.dvlogin

after the original import and each reload. Eventually you'll develop a 
feel for how namespaces work in Python and you'll be able to take liberties.

Finally, note that there isn't much point doing the reload() unless the 
content of the modue has actually changed!

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb     http://del.icio.us/steve.holden
Recent Ramblings       http://holdenweb.blogspot.com




More information about the Python-list mailing list