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

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

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

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 Bankspavlovevide...@gmail.com wrote: On Jul 2, 3:12 am, Ulrich Eckhardt eckha...@satorlaser.com wrote: Bearophile wrote: Ulrich Eckhardt: a way to automatically release the resource, something which I would do in the destructor in C++. Is this

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

2009-07-03 Thread Tim Roberts
Dave Angel da...@dejaviewphoto.com 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

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

2009-07-03 Thread Aahz
In article ftps459nklqio3nghaaraniqo3fs9r8...@4ax.com, Tim Roberts t...@probo.com wrote: Dave Angel da...@dejaviewphoto.com 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)

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

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

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 the

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 applies to my

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

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 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: 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

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

2009-07-02 Thread Carl Banks
On Jul 2, 3:12 am, Ulrich Eckhardt eckha...@satorlaser.com 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.

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. However, I'm not sure

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),