I love assert

Chris Angelico rosuav at gmail.com
Sat Nov 15 03:16:50 CET 2014


On Sat, Nov 15, 2014 at 1:14 PM, Tim Chase
<python.list at tim.thechases.com> wrote:
> On 2014-11-15 12:48, Chris Angelico wrote:
>> conn = establish_database_connection()
>> try:
>>     do_stuff()
>> finally:
>>     conn.rollback()
>
> this sounds suspiciously like you'd never actually commit.  Do you
> mean something like
>
>   conn = establisth_database_connection()
>   try:
>     do_stuff(conn)
>   except:
>     conn.rollback()
>     raise

No; I prefer to have the commit be as close as possible to the code
that needs it done. So in this toy example, the commit would be inside
- maybe deep inside - do_stuff(). After committing, rolling back
wouldn't hurt.

ChrisA



More information about the Python-list mailing list