This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author cowlinator
Recipients berker.peksag, cowlinator, docs@python, martin.panter, ncoghlan, serhiy.storchaka, terry.reedy
Date 2018-01-30.00:28:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1517272099.56.0.467229070634.issue22374@psf.upfronthosting.co.za>
In-reply-to
Content
I would like to second the improved explanation of contextlib.contextmanager, and additionally point out another problem:  
A very important piece of information is missing from the documentation:  how to return data from the contextmanager-wrapped function.  

I had to go look up the source code, which had a wonderful explanation in the comments: https://gist.github.com/enuomi/1385336#file-contextlib-py-L56 

In particular, note that 

@contextmanager
def some_generator(<arguments>):
  yield <return_data>

can be used to return <return_data> to the caller, via

with some_generator(<arguments>) as <return_data>:
  print(return_data)

This information is wholly and completely missing from the contextlib.contextmanager documentation.
History
Date User Action Args
2018-01-30 00:28:19cowlinatorsetrecipients: + cowlinator, terry.reedy, ncoghlan, docs@python, berker.peksag, martin.panter, serhiy.storchaka
2018-01-30 00:28:19cowlinatorsetmessageid: <1517272099.56.0.467229070634.issue22374@psf.upfronthosting.co.za>
2018-01-30 00:28:19cowlinatorlinkissue22374 messages
2018-01-30 00:28:19cowlinatorcreate