Python is readable

Duncan Booth duncan.booth at invalid.invalid
Fri Mar 16 05:30:17 EDT 2012


Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915
@spamschutz.glglgl.de> wrote:

>> 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.....
> 
> Or just inspect l - I don't know if a lock's __enter__ methos returns it 
> again for assignment with "as"...
> 

No, if you do `with lock as l:` then l will get the boolean True.

The lock's __enter__ method is the same as lock.acquire(). That method 
takes an optional argument which can be used to conditionally acquire the 
lock and return a boolean indicating success or failure. When used inside a 
`with` statement you can't pass in the optional argument so it acquires it 
unconditionally but still returns the success status which is either True 
or it never returns.

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list