Python is readable

Thu Mar 15 12:48:55 CET 2012

On 3/15/2012 12:14, Thomas Rachel wrote:
> Am 15.03.2012 11:44 schrieb Kiuhnm:
>> Let's try that.
>> Show me an example of "list comprehensions" and "with" (whatever they
>> are).
> with open("filename", "w") as f:
>     f.write(stuff)

Here f is created before executing the block and destroyed right after 
leaving the block. f's destructor will probably close the file handle.

> with lock:
>     do_something_exclusively()

It's clear what it does, but I don't know if that's special syntax. 
Maybe objects can have two special methods that are called respect. on 
entering and leaving the with-block.
Or, more likely, lock creates an object which keeps the lock "acquired". 
The lock is released when we leave the block.
So we could inspect the lock with
   with lock as l:
       inspect l...

BTW, aren't those ':' redundant?


More information about the Python-list mailing list