beginner's refcount questions

J

Jens Theisen

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.
 
S

Scott David Daniels

Jens said:
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.
2

--Scott David Daniels
(e-mail address removed)
 
T

Terry Reedy

Jens Theisen said:
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
 
F

Fredrik Lundh

Jens said:
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?

http://www.effbot.org/pyfaq/how-does-python-manage-memory.htm
http://www.effbot.org/pyfaq/my-class-defines-del-but-it-is-not-called-when-i-delete-the-object.htm

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top