Default scope of variables

Chris Angelico rosuav at gmail.com
Thu Jul 4 18:58:50 CEST 2013


On Fri, Jul 5, 2013 at 2:38 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Thu, 04 Jul 2013 15:47:57 +1000, Chris Angelico wrote:
>> Here's one example of shadowing that comes from a C++ project at work. I
>> have a class that represents a database transaction (constructing it
>> begins a transaction, it has methods for doing queries, and its
>> destructor rolls back).
>
> When the object finally gets garbage collected, doesn't that mean the
> last transaction will be rolled back?

Oh. Uhm... ahh... it would have helped to mention that it also has a
commit() method! But yes, that's correct; if the object expires (this
is C++, so it's guaranteed to call the destructor at that close brace
- none of the Python vagueness about when __del__ is called) without
commit() being called, then the transaction will be rolled back. And
since this is PostgreSQL we use, the same applies if the process is
SIGKILLed or the power fails. If commit() doesn't happen, neither does
the transaction. (There are a few actions the program can take that
are deliberately non-transactional - log entries of various sorts,
mainly - but everything else is guarded in this way.)

ChrisA



More information about the Python-list mailing list