beginner's refcount questions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jens Theisen

    #1

    beginner's refcount questions

    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?

    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.

    --
    Cheers, Jens
  • Terry Reedy

    #2
    Re: beginner's refcount questions


    "Jens Theisen" <jth02@arcor.de wrote in message
    news:87odru4pc2 .fsf@arcor.de.. .
    python uses gc only where refcounts alone haven't yet done the
    job.
    /python/The CPython implementation/
    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.
    sys.getrefcount I believe

    thr



    Comment

    • Fredrik Lundh

      #3
      Re: beginner's refcount questions

      Jens Theisen wrote:
      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.
      the behaviour you see in this simple program is not guaranteed by the
      language specification, and even trivial modifications to your program
      may cause trouble even under a reference-counting implementation of
      Python. for example,

      class Foo:
      def __del__(self):
      print "deled!"

      def foo():
      f = Foo()
      i = open("file.txt" )
      return i.readline()

      try:
      foo()
      except IOError:
      pass
      print "done!"

      prints

      done!
      deled!
      In c++, this is a central technique used for all sorts of tasks,
      whereas in garbage collected languages it's usually not available.
      Python is not C++. if you want to do controlled resource management,
      you need to use "try/finally" or "with".
      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?



      </F>

      Comment

      • Scott David Daniels

        #4
        Re: beginner's refcount questions

        Jens Theisen wrote:
        python uses gc only where refcounts alone haven't yet done the
        job. Thus, the following code
        class Foo:
        def __del__(self):
        print "deled!"
        .... 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?
        Python (the language) does not guarantee when (or even if) __del__
        methods will be invoked, while C++ does (for the corresponding method).
        Jython relies on Java's GC, IronPython?.... CPython's garbage collector
        (which finds loops of references and drops them as a group) avoids
        any loops containing more than a single __del__ method. In the spirit
        of "explicit is better than implicit," ignore this problem and stop
        using __del__. In some cases you might want to check into the semantics
        of the "with" construct introduced in 2.5, it may allow you to do what
        you really mean.
        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.
        >>import sys
        >>sys.getrefcou nt(42 * 7)
        2

        --Scott David Daniels
        scott.daniels@a cm.org

        Comment

        Working...