beginner's refcount questions

Jean-Paul Calderone exarkun at divmod.com
Mon Oct 30 02:43:49 CET 2006


On 30 Oct 2006 00:30:53 +0000, Jens Theisen <jth02 at arcor.de> wrote:
>Hello,
>
>python uses gc only where refcounts alone haven't yet done the
>job. Thus, the following code
>
>class Foo:
>    def __del__(self):
>        print "deled!"
>
>def foo():
>    f = Foo()
>
>foo()
>print "done!"
>
>prints
>
>deled!
>done!
>
>and not the other way round.
>
>In c++, this is a central technique used for all sorts of tasks,
>whereas in garbage collected languages it's usually not available.
>
>Is there a reason not to rely on this in Python? For example, are
>there alternative Python implementations that behave differently?  Or
>some other subtle problems?

Among the numerous other reasons, there's this one:

    Python 2.4.3 (#2, Oct  6 2006, 07:52:30) 
    [GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> class Foo:
    ...     def __del__(self):
    ...             print 'collected'
    ... 
    >>> def foo():
    ...     f = Foo()
    ...     1/0
    ... 
    >>> foo()
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "<stdin>", line 3, in foo
    ZeroDivisionError: integer division or modulo by zero
    >>> print 'done!'
    done!
    >>> raise ValueError
    collected
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError
    >>> 

>
>And some other minor question: Is there a way to query the use count
>of an object? This would be useful for debugging and testing.

Jean-Paul



More information about the Python-list mailing list