Re: Searching equivalent to C++ RAII or deterministic destructors

2009-07-04 Thread Carl Banks
On Jul 3, 5:34 am, Jack Diederich wrote: > On Thu, Jul 2, 2009 at 2:36 PM, Carl Banks wrote: > > Warning: objects with a __del__ attribute prevent reference cycle > > detection, which can potentially lead to memory (and resource) leaks. > > So you must be careful to avoid creating reference loops

Re: Searching equivalent to C++ RAII or deterministic destructors

2009-07-03 Thread Aahz
In article , Tim Roberts wrote: >Dave Angel wrote: >> >>You're right of course. What I was trying to say was it deletes the >>reference to the object. Unlike obj = None, del obj removes the >>reference (attribute) entirely. Although I don't know what it should be >>called if it's a local

Re: Searching equivalent to C++ RAII or deterministic destructors

2009-07-03 Thread Tim Roberts
Dave Angel wrote: > >You're right of course. What I was trying to say was it deletes the >reference to the object. Unlike obj = None, del obj removes the >reference (attribute) entirely. Although I don't know what it should be >called if it's a local variable. Perhaps it "unbinds" the nam

Re: Searching equivalent to C++ RAII or deterministic destructors

2009-07-03 Thread Jack Diederich
On Thu, Jul 2, 2009 at 2:36 PM, Carl Banks wrote: > On Jul 2, 3:12 am, Ulrich Eckhardt wrote: >> Bearophile wrote: >> > Ulrich Eckhardt: >> >> a way to automatically release the resource, something >> >> which I would do in the destructor in C++. >> >> > Is this helpful? >> >http://effbot.org/pyre

Re: Searching equivalent to C++ RAII or deterministic destructors

2009-07-03 Thread Ulrich Eckhardt
Thanks to all that answered, in particular I wasn't aware of the existence of the __del__ function. For completeness' sake, I think I have found another way to not really solve but at least circumvent the problem: weak references. If I understand correctly, those would allow me to pass out handles

Re: Searching equivalent to C++ RAII or deterministic destructors

2009-07-02 Thread ryles
> You can go ahead and implement a __del__() method. It will often work in > CPython, but you get no guarantees, especially when you have reference > cycles and with other Python implementations that don't use refcounting. And for resources whose lifetime is greater than your process (e.g. a file)

Re: Searching equivalent to C++ RAII or deterministic destructors

2009-07-02 Thread Roel Schroeven
Peter Otten schreef: > Ulrich Eckhardt wrote: > >> Bearophile wrote: >>> Ulrich Eckhardt: a way to automatically release the resource, something which I would do in the destructor in C++. >>> Is this helpful? >>> http://effbot.org/pyref/with.htm >> Yes, it aims in the same direction. How

Re: Searching equivalent to C++ RAII or deterministic destructors

2009-07-02 Thread Carl Banks
On Jul 2, 3:12 am, Ulrich Eckhardt wrote: > Bearophile wrote: > > Ulrich Eckhardt: > >> a way to automatically release the resource, something > >> which I would do in the destructor in C++. > > > Is this helpful? > >http://effbot.org/pyref/with.htm > > Yes, it aims in the same direction. However,

Re: Re: Searching equivalent to C++ RAII or deterministic destructors

2009-07-02 Thread Dave Angel
Christian Heimes wrote: Dave Angel wrote: Look also at 'del' a command in the language which explicitly deletes an object. No, you are either explaining it the wrong way or you have been fallen for a common misinterpretation of the del statement. The del statement only removes the obje

Re: Searching equivalent to C++ RAII or deterministic destructors

2009-07-02 Thread Peter Otten
Dave Angel wrote: > But I'm guessing you want something that automatically deletes objects > whenever the last reference disappears. That's an implementation > detail, not a language guarantee. In particular CPython does what you > want, by using reference counting. That's the only Python I've

Re: Searching equivalent to C++ RAII or deterministic destructors

2009-07-02 Thread Christian Heimes
Dave Angel wrote: > Look also at 'del' a command in the language which explicitly deletes an > object. No, you are either explaining it the wrong way or you have been fallen for a common misinterpretation of the del statement. The del statement only removes the object from the current scope. This

Re: Searching equivalent to C++ RAII or deterministic destructors

2009-07-02 Thread Dave Angel
Ulrich Eckhardt wrote: Hi! I'm currently converting my bioware to handle Python code and I have stumbled across a problem... Simple scenario: I have a handle to a resource. This handle allows me to manipulate the resource in various ways and it also represents ownership. Now, when I put this in

Re: Searching equivalent to C++ RAII or deterministic destructors

2009-07-02 Thread Peter Otten
Ulrich Eckhardt wrote: > Bearophile wrote: >> Ulrich Eckhardt: >>> a way to automatically release the resource, something >>> which I would do in the destructor in C++. >> >> Is this helpful? >> http://effbot.org/pyref/with.htm > > Yes, it aims in the same direction. However, I'm not sure this a

Re: Searching equivalent to C++ RAII or deterministic destructors

2009-07-02 Thread Ulrich Eckhardt
Bearophile wrote: > Ulrich Eckhardt: >> a way to automatically release the resource, something >> which I would do in the destructor in C++. > > Is this helpful? > http://effbot.org/pyref/with.htm Yes, it aims in the same direction. However, I'm not sure this applies to my case. The point is that

Re: Searching equivalent to C++ RAII or deterministic destructors

2009-07-02 Thread Bearophile
Ulrich Eckhardt: > a way to automatically release the resource, something > which I would do in the destructor in C++. Is this helpful? http://effbot.org/pyref/with.htm Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Searching equivalent to C++ RAII or deterministic destructors

2009-07-02 Thread Ulrich Eckhardt
Hi! I'm currently converting my bioware to handle Python code and I have stumbled across a problem... Simple scenario: I have a handle to a resource. This handle allows me to manipulate the resource in various ways and it also represents ownership. Now, when I put this into a class, instances to