self destruction :)

Kalle Svensson kalle at lysator.liu.se
Thu Jun 27 16:31:19 EDT 2002


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[Guyon Mor?e]
> > i've tried: 'del self', but somehow it didn't work. afterwards it should
> > also _not_ be in the list anymore.

[Sean 'Shaleh' Perry]
> mylist = [obj1, obj2, ....]
> del obj1 # mylist still has a reference to obj1 so
>          # the del simply decrements the ref count

By the way, this program

- ----- 8< -----
import sys
allobjects = []

class C:
    def __init__(self):
        print "Before append, RC: %s." % (sys.getrefcount(self) - 2)
        allobjects.append(self)
        print "After append, RC: %s." % (sys.getrefcount(self) - 2)

    def __del__(self):
        print "In __del__, RC: %s." % (sys.getrefcount(self) - 2)

    def delete(self):
        print "Before remove, RC: %s." % (sys.getrefcount(self) - 2)
        allobjects.remove(self)
        print "After remove, RC: %s." % (sys.getrefcount(self) - 2)

print "Creating..."
C()
print
print "Deleting..."
allobjects[0].delete()
print
print "Exiting..."
- ----- >8 -----

prints

- ----- 8< -----
Creating...
Before append, RC: 3.
After append, RC: 4.

Deleting...
Before remove, RC: 2.
After remove, RC: 1.
In __del__, RC: 3.

Exiting...
- ----- >8 -----

when run.  Where do the extra references in __init__ and __del__ come
from?

Peace,
  Kalle
- -- 
Kalle Svensson, http://www.juckapan.org/~kalle/
Student, root and saint in the Church of Emacs.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.6 <http://mailcrypt.sourceforge.net/>

iD8DBQE9G3YMdNeA1787sd0RAqHiAJ9Fmja/KUvq08+qls9OZEEHLWrEbACfXWIW
URLMCDEABgB1pkvj1eYxV6Q=
=APuL
-----END PGP SIGNATURE-----





More information about the Python-list mailing list