Memory Leak ['LBBW': checked]
jepler at unpythonic.net
Wed Feb 25 15:59:28 CET 2004
On Wed, Feb 25, 2004 at 03:18:33PM +0100, Holger Joukl wrote:
> 2) I am quite stunned by the del alist[:] thing:
> >>> class foo:
> ... def __del__(self):
> ... print "foo.__del__"
> >>> alist=[foo(), foo(), foo()]
> >>> del alist[:]
> >>> alist
> alist[:] creates a shallow copy of alist, which is a new object. del then
> decreases the refcount of this
> temporary object, thus it is destroyed. But why is the original alist empty
> after that, why are the
> list elements destroyed?
> Does that mean if a shallow copy of a list is created, the refcounts of the
> elements in the list are not increased?
"del alist[:]" does not get a slice of alist and then delete the resulting
copy. del can work on names ("del a"), attributes ("del o.x") and slices
("del l[:]"). The following part of an interactive session shows that
__delslice__, not __getslice__ is called for "del c[:]".
>>> class C:
... def __getslice__(self, *args): print "getslice", args
... def __delslice__(self, *args): print "delslice", args
>>> c = C()
getslice (0, 2147483647)
>>> del c[:]
delslice (0, 2147483647)
More information about the Python-list