Python is readable

Kiuhnm kiuhnm03.4t.yahoo.it
Thu Mar 15 07:48:55 EDT 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...
       do_some.....

BTW, aren't those ':' redundant?

Kiuhnm



More information about the Python-list mailing list