[Python-ideas] Enhance reload

Terry Reedy tjreedy at udel.edu
Sat Sep 29 21:16:05 CEST 2007

"Joseph Maurer" <clarkksv at yahoo.com> wrote in 
message news:844399.5421.qm at web58901.mail.re1.yahoo.com...
| I'd like to see the reload feature of Python enhanced so it can replace 
the methods for existing class instances, references to methods, and 
references to functions.

I think would we could get farther by restricting concern to replacing 
class attributes so that existing class instances would use their new 

As I understand, the problem is this.  After somemod is imported, 'import 
somemod' simply binds 'somemod' to the existing module object, while 
'reload somemod' replaces the module object with a new object with all new 
contents, while references to objects within the old module object remain 
as are.

So I propose this.  'Reclass somemod' (by whatever syntax) would execute 
the corresponding code in a new namespace (dict).  But instead of making 
that dict the __dict__ attribute of a new module, reclass would match class 
names with the existing  __dict__, and replace the class.__dict__ 
attributes, so that subsequent access to class attributes, including 
particularly methods, would get the new versions.  In other words  use the 
existing indirection involved in attribute access.  New classes could 
simple be added.  Deleted classes could be disabled, but this really 
requires a restart after editing files that reference such classes, so 
deleting classes should not be done for the restricted reload uses this 
idea is aimed at.

It would probably be possible to modify function objects (replace 
func_code, etc), but this is more difficult.  It is simpler, at least for a 
beginning, to require that functions be put within a class when reclassing 
is anticipated.

Terry Jan Reedy

More information about the Python-ideas mailing list