Is o.__dict__ = {} valid?

Edward K. Ream edreamleo at charter.net
Sat Jun 21 18:54:31 EDT 2003


My app supports multiple windows; it would be tidy to reclaim the storage
associated with a window when the window closes.  The objects in such
windows contain many references to each other, so rather than trying to
track down every last pointer anywhere in this mass of objects, it appears
simple and effective just to break the references between objects "by hand"
when a window closes.

I'd like your comments about the following function:

def clearAllIvars (o):

    """Clear all ivars of o, a member of some class."""

    if 0: # Somewhat faster. Is it valid?
        o.__dict__ = {}
    else:
         for name in o.__dict__.keys():
            setattr(o,name,None)

My questions:

1. Is it proper to do o.__dict__ = {} ?  I haven't found an explicit
prohibition.

2. If it is proper, would such an assignment be sure to get rid of all the
references in __dict__?  It does seem to work.

3. If o.__dict__ = {} is not valid, is there any standard way to get the
same effect quickly?

Any other comments or suggestions?  Thanks for your help.

Edward
--------------------------------------------------------------------
Edward K. Ream   email:  edreamleo at charter.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------






More information about the Python-list mailing list