functional programming

Denys Duchier Denys.Duchier at ps.uni-sb.de
Fri Feb 25 22:33:10 CET 2000


Moshe Zadka <moshez at math.huji.ac.il> writes:

> On 25 Feb 2000, Denys Duchier wrote:
> 
> > The point, however, is moot since, as I suggested previously, the
> > finalization semantics of Python (much like that of C++) are not
> > easily reconciled with tail call optimization.
> 
> I won't say anything about C++ (what I'd like to say about it isn't
> fit for prime time), but about Python, this is untrue: either the called
> function has a reference to the object (in which case it won't be
> finalized) or it can be deleted when the calling frame is deleted, in
> which case the call to the destructor will *precede* the tail-call,
> and still leave it in tail position.

As I said in my original message, you can indeed do this but it
violates an assumption that many Python programmers seem to care
about: namely that the destructor of an object is invoked when exiting
the scope rather than prior to the tail call, or, as the logical
generalization would have it, as soon as possible (i.e. as soon as it
is no longer referenced in subsequent code).

Consider the following (I am not saying that this is necessarily a
sensible programming style, but current Python semantics give it a
straightforward reading and I have actually occasionally done similar
things in C++):

class Lock:
	def __init__(self):
                acquire_some_lock()
        def __del__(self):
                release_some_lock()

def foo()
        lock = Lock()
        baz()

It makes a difference whether the lock is released before the call to
baz() or after.  I am by no means sanguine about keeping Python's
finalization semantics, but it _is_ an issue that makes a difference.

Sincerely,

-- 
Dr. Denys Duchier			Denys.Duchier at ps.uni-sb.de
Forschungsbereich Programmiersysteme	(Programming Systems Lab)
Universitaet des Saarlandes, Geb. 45	http://www.ps.uni-sb.de/~duchier
Postfach 15 11 50			Phone: +49 681 302 5618
66041 Saarbruecken, Germany		Fax:   +49 681 302 5615



More information about the Python-list mailing list