LWN.net Logo

PyCon: Evangelizing Python

PyCon: Evangelizing Python

Posted Apr 2, 2013 16:15 UTC (Tue) by lab (subscriber, #51153)
In reply to: PyCon: Evangelizing Python by kleptog
Parent article: PyCon: Evangelizing Python

> Python is the only contemporary language I know that includes something like the with-statement, which does say something

The C# 'using' statement?
http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.110).aspx


(Log in to post comments)

PyCon: Evangelizing Python

Posted Apr 2, 2013 17:06 UTC (Tue) by hummassa (subscriber, #307) [Link]

Every single C++ variable initialization is the equivalent to python's with and C#'s using...

PyCon: Evangelizing Python

Posted Apr 2, 2013 17:23 UTC (Tue) by intgr (subscriber, #39733) [Link]

Similar, yes. Equivalent, no.

1. Python's "with" statement is more flexible, it has access to the exception when it occurs. A "with db_transaction():" block can automatically decide to commit or roll back, unlike C++/C#. You can also implement something like "with ignore_exception(OSError):"

2. Python doesn't require you to create a local variable to hold the state; this always annoys me when using the RAII pattern to hold locks in C++.

3. Prettier syntax ;)

PyCon: Evangelizing Python

Posted Apr 2, 2013 19:13 UTC (Tue) by hummassa (subscriber, #307) [Link]

1. std::uncaught_exception() allows for your commit/rollback scenario, but not your ignore_this_exception scenario. The latter does not seem a good idea to me, but...
2. the lock thing can be implicit... it's a matter of how you do it. Anyway,
{
some_lock_t lock(x);
x.do_something();
}
does not seem sooo wrong to me... although I usually put it on X and do
x.do_something_locked<some_lock_t>();
only, or even
some_locking_scaffold(x).do_something();
and this last one is really easy.
3. it's not! :-D

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds