ownership problem?

Jeffrey Schwab jeff at schwabcenter.com
Mon Nov 21 16:46:49 EST 2005


Donn Cave wrote:
> In article <dy8gf.3763$3o6.904548 at twister.southeast.rr.com>,
>  Jeffrey Schwab <jeff at schwabcenter.com> wrote:
> ....
> 
>>Yes it is.  Memory is only one type of resource.  There are still files 
>>and sockets to close, pipes to flush, log messages to be printed, GDI 
>>contexts to free, locks to release, etc.  In C++, these things are 
>>generally done by destructors, which are called automatically and 
>>deterministically.  I am not a Python Guru, but in Perl, Java, and other 
>>languages that have built-in garbage collectors, these tasks have to be 
>>done explicitly.  I find that this forces a procedural approach, even in 
>>an otherwise object-oriented program.
>>
>>If you want something like automatic garbage collection in C++, I 
>>recommend the use of Factories with destructors that release the 
>>Factories' products.  The zeitgeist in c.l.c++.moderated seems to prefer 
>>the use of smart (reference-counted) pointers, which also rely on 
>>destructors to release resources automatically.  Plentry of free, 
>>open-source implementations are available.
> 
> 
> You may be gratified to learn that Python's main storage model
> is reference counted objects, and when an object falls out of
> all referenced scopes its finalizers run immediately.

Thanks, that's good to know!  For some reason I had it in my head that 
Python always used mark & sweep.  I'm used to ref-counted collection in 
Perl, but I never relied on it because of a nagging (probably 
ill-founded) worry about cyclic references.  Does Python have any way 
around this?  Is there a Python equivalent to C++ destructors?

> This is however true only of the C implementation.  The Java
> implementation naturally has Java's limitations in this matter,
> so documentation generally avoids the issue.  The C implementation
> has been around for over a decade, wonder if it had any influence
> on your C++ zeitgeist?

Possibly.  Python has really caught on with the C++ crowd in a way that 
other languages never did.  I'm not sure why; not that I don't love 
Python, but there are other good, dynamic languages that haven't made 
the same in-roads.

I think Java has had a big influence, too.  People just don't seem to 
want to be bothered with thinking about object life cycles at all.  This 
seems unfortunate to me, because cleanup still has to be done, so it 
just ends up getting moved outside the objects where it belongs.  I 
think this hurts abstraction.



More information about the Python-list mailing list