Python is readable

Mark Lawrence breamoreboy at yahoo.co.uk
Thu Mar 15 15:06:01 CET 2012


On 15/03/2012 11:48, Kiuhnm wrote:
> 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

Nope.

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> with open("filename", "w") as f
   File "<stdin>", line 1
     with open("filename", "w") as f
                                   ^
SyntaxError: invalid syntax

-- 
Cheers.

Mark Lawrence.




More information about the Python-list mailing list