From: Bill Soudan Date: Wed Nov 26, 2003 16:24:37 Europe/London To: Michael Hudson Subject: Re: PEP 310/object lifetimes? On 26 Nov 2003, Michael Hudson wrote: > Is this the same Bill Soudan as in libicq? That stirs some old > memories... Whoa... that's right, you were the Python guy that wrote the bindings! Ah, interesting how our paths cross again. Great memory. Since the libicq days, I've mainly been C++ but recently doing more and more in Python. My lastest task was a performance testing system for our software here at work coded entirely in Python. The main loop was a bit too much for Python to handle, so I rewrote it in C++ and used sip to make some bindings. All in all, it worked out superbly - complicated setup/teardown bits still in Python, simple tight main loop ported almost line-by-line (!) from Python to C++. I'd like to see us do this will all our software, personally, Python is a dream to develop with and maps so well to C++. My three nits with the current system: sip won't propagate exceptions from from Python->C++ (though it will the other way around), you have to write special sip versions of your header files, and I wish I could control object lifetimes explicitly in Python. I don't like doing a 'del last_reference_i_think' and hoping the implementation will delete the underlying object. > I think it's a *different* solution to a different problem. > > While the "Resource Acquistion is Initialization" pattern works > pretty well in C++, I'm not sure it would work so well in Python. > Maybe it's just my distrust of __del__ methods. It's definitely a different solution, but your __enter__/__exit__ mechanism is served just as well (in my mind) by __init__/__del__. And since I've always been of the opinion Python's a little crazy already with the large number of __special__ methods, re-using existing is a plus. Finally, the concept is well known by C++ programmers, I use it all the time. It'd be nice if Python supported the same, especially for cases like me where you're working with C++ bindings and you _need_ the underlying C++ object to go away. I wanted to discuss it with you since I see it as a two-birds-one-stone type of thing. Actually, maybe more, another use case I was thinking about: with o = ReallyExpensiveObject(): # ... # no more ReallyExpensiveObject To me, this feature is very 'Python': simple, powerful, elegant. >> I did a bit of digging, but I couldn't come up with much. Has it >> been discussed before, and if so, do you remember where? And does >> your comment mean it isn't possible given the current >> implementation? > > Maybe you should ask Paul? I haven't done a lot of thinking about > this. I'll get in touch with him, as well. Thanks, Bill