Deprecating reload() ???

Skip Montanaro skip at pobox.com
Fri Mar 12 12:42:06 EST 2004


    >> Reload is not broken, and certainly shouldn't be deprecated at least
    >> until there's a better solution that won't suffer from reload's one
    >> problem, IMHO, which is that it surprises some people by its
    >> behaviour.

    David> It's worse than just a surprise.  It's a serious problem when
    David> what you need to do is what most people are expecting -- replace
    David> every reference to objects in the old module with references to
    David> the new objects.  The problem becomes a near impossibility when
    David> those references are scattered throughout a multi-module program.

This is where I think your model of how Python works has broken down.
Objects don't live within modules.  References to objects do.  All objects
inhabit a space not directly associated with any particular Python
namespace.  If I execute

    import urllib
    quote = urllib.quote
    reload(urllib)

quote and urllib.quote refer to different objects and compare False.  (I
suppose the cmp() routine for functions could compare code objects and other
attributes which might change.)  The object referred to by urllib.quote is
not bound in any other way to the urllib module other than the reference
named "quote" in the urllib module's dict.  reload() simply decrements the
reference count to urllib, which decrements the reference count to its dict,
which causes it to clean up and decrement the reference count for all its
key/value pairs.

    David> For the *exceptional* case where we want to pick and chose which
    David> objects to update, seems like the best solution would be to add
    David> some options to the reload function.

    David> def reload(<module>, objects = '*'):

    David> The default second argument '*' updates all references.  '?'
    David> prompts for each object.  A list of objects updates references to
    David> just those objects automatically.  'None' would replicate the
    David> current behavior, updating only the name of the module itself.

If you want to implement this and all you're interested in are class,
function and method definitions and don't care about local variables I think
you can make a reasonable stab at this.  I wouldn't recommend you overload
reload() with this code, at least not initially.  Write a super_reload() in
Python.  If/when you get that working, then rewrite it in C, then get that
working, then you're free to recommend that the builtin reload() function be
modified.

    David> I don't understand.  My assumption is you would normally update
    David> all references to the selected objects in all namespaces.

As Denis Otkidach pointed out in response to an earlier post of mine,
objects on shared free lists can't be rebound using this mechanism.  I think
it's safe initially to restrict super_reload() to classes, functions and
methods.

Skip





More information about the Python-list mailing list