ownership problem?

Jeffrey Schwab jeff at schwabcenter.com
Sun Nov 20 19:30:01 EST 2005


Fredrik Lundh wrote:
> Jeffrey Schwab wrote:
> 
> 
>>>Is it correct to say that the typical ownership problem, which
>>>frequently arises in C++, does not occur normally in Python?
>>
>>What "typical ownership problem" do you feel frequently arises in C++?
>>If you are referring to the sometimes difficult task of determining
>>which object owns a particular resource, why would it occur less in
>>Python than in C++?
> 
> 
> the problem isn't determining who owns it, the problem is determining
> who's supposed to release it.  that's not a very common problem in a
> garbage-collected language...

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.



More information about the Python-list mailing list