回复:Python-ideas Digest, Vol 146, Issue 13

Thanks for your reply.But the answer is not I except, I will show you some examples to explain what result I except: @contextmanagerdef cm(): print('open file') yield print('close file')with cm(): 1/0 If I use a contextmanager ,I except it can help me to close the file anytime,even raise an error,but if I define a function with @contextmanager like the example which I have showed for you, it will never print('close file') I can only modify it like this:@contextmanagerdef cm(): try: print('open file') yield except Exception as e: print('Error',e) finally: print('close file') It is not friendly for us to use it, so I modify the contextlib to fix it,you can catch it from the e-mail attachment.It's in the line 79 and line 97---------------------------------------------------------------------- 发件人:python-ideas-request@python.org 收件人:python-ideas@python.org 主题:Python-ideas Digest, Vol 146, Issue 13 日期:2019年01月06日 01点05分 Send Python-ideas mailing list submissions to python-ideas@python.org To subscribe or unsubscribe via the World Wide Web, visit https://mail.python.org/mailman/listinfo/python-ideas or, via email, send a message with subject or body 'help' to python-ideas-request@python.org You can reach the person managing the list at python-ideas-owner@python.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Python-ideas digest..." Today's Topics: 1. Re: Make the @contextmanager of contextlib to be a real contextmanager (Serhiy Storchaka) 2. Re: Fixed point format for numbers with locale based separators (?ukasz Stelmach) ---------------------------------------------------------------------- Message: 1 Date: Sat, 5 Jan 2019 16:45:34 +0200 From: Serhiy Storchaka <storchaka@gmail.com> To: python-ideas@python.org Subject: Re: [Python-ideas] Make the @contextmanager of contextlib to be a real contextmanager Message-ID: <q0qfqd$q1q$1@blaine.gmane.org> Content-Type: text/plain; charset=UTF-8; format=flowed 05.01.19 14:52, Moon?sun ????:
Message: 2 Date: Sat, 05 Jan 2019 16:41:20 +0100 From: ?ukasz Stelmach <steelman@post.pl> To: Steven D'Aprano <steve@pearwood.info> Cc: python-ideas@python.org Subject: Re: [Python-ideas] Fixed point format for numbers with locale based separators Message-ID: <87y37z9knz.fsf%steelman@post.pl> Content-Type: text/plain; charset="utf-8" Steven D'Aprano <steve@pearwood.info> writes: the locale database. There is "n" formmatter which behaves like "g" but it does not fit my needs.

On Sat, Jan 5, 2019 at 9:13 PM Moon丶sun <uamr567@sina.com> wrote:
This is intentional, and can't be changed without breaking lots of code. With your version, there's no way for the context manager to catch or modify the exception, which is a common use case. For example, here's a context manager I wrote recently: @contextmanager def catch_and_log(exctype): try: yield except exctype: log.exception(...) This can't be done using your version. Of course you can have your own version of @contextmanager that works however you prefer. -n -- Nathaniel J. Smith -- https://vorpus.org

On Sat, Jan 5, 2019 at 9:13 PM Moon丶sun <uamr567@sina.com> wrote:
This is intentional, and can't be changed without breaking lots of code. With your version, there's no way for the context manager to catch or modify the exception, which is a common use case. For example, here's a context manager I wrote recently: @contextmanager def catch_and_log(exctype): try: yield except exctype: log.exception(...) This can't be done using your version. Of course you can have your own version of @contextmanager that works however you prefer. -n -- Nathaniel J. Smith -- https://vorpus.org
participants (2)
-
Moon丶sun
-
Nathaniel Smith