<small id='BBn6V'></small><noframes id='BBn6V'>

      <bdo id='BBn6V'></bdo><ul id='BBn6V'></ul>
      1. <tfoot id='BBn6V'></tfoot>

        <i id='BBn6V'><tr id='BBn6V'><dt id='BBn6V'><q id='BBn6V'><span id='BBn6V'><b id='BBn6V'><form id='BBn6V'><ins id='BBn6V'></ins><ul id='BBn6V'></ul><sub id='BBn6V'></sub></form><legend id='BBn6V'></legend><bdo id='BBn6V'><pre id='BBn6V'><center id='BBn6V'></center></pre></bdo></b><th id='BBn6V'></th></span></q></dt></tr></i><div id='BBn6V'><tfoot id='BBn6V'></tfoot><dl id='BBn6V'><fieldset id='BBn6V'></fieldset></dl></div>
        <legend id='BBn6V'><style id='BBn6V'><dir id='BBn6V'><q id='BBn6V'></q></dir></style></legend>

        在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗?

        In Python, if I return inside a quot;withquot; block, will the file still close?(在 Python 中,如果我在一个“with中返回.块,文件还会关闭吗?)
          <tbody id='ddSeB'></tbody>

          <legend id='ddSeB'><style id='ddSeB'><dir id='ddSeB'><q id='ddSeB'></q></dir></style></legend>

          <i id='ddSeB'><tr id='ddSeB'><dt id='ddSeB'><q id='ddSeB'><span id='ddSeB'><b id='ddSeB'><form id='ddSeB'><ins id='ddSeB'></ins><ul id='ddSeB'></ul><sub id='ddSeB'></sub></form><legend id='ddSeB'></legend><bdo id='ddSeB'><pre id='ddSeB'><center id='ddSeB'></center></pre></bdo></b><th id='ddSeB'></th></span></q></dt></tr></i><div id='ddSeB'><tfoot id='ddSeB'></tfoot><dl id='ddSeB'><fieldset id='ddSeB'></fieldset></dl></div>

          • <tfoot id='ddSeB'></tfoot>
            • <small id='ddSeB'></small><noframes id='ddSeB'>

              • <bdo id='ddSeB'></bdo><ul id='ddSeB'></ul>
                  本文介绍了在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  考虑以下几点:

                  with open(path, mode) as f:
                      return [line for line in f if condition]
                  

                  文件会正确关闭,还是使用 return 以某种方式绕过 上下文管理器?

                  Will the file be closed properly, or does using return somehow bypass the context manager?

                  推荐答案

                  是的,它就像 try 块之后的 finally 块,即它总是执行(除非当然,python 进程以一种不寻常的方式终止).

                  Yes, it acts like the finally block after a try block, i.e. it always executes (unless the python process terminates in an unusual way of course).

                  PEP-343 的一个例子中也提到过这是 with 语句的规范:

                  It is also mentioned in one of the examples of PEP-343 which is the specification for the with statement:

                  with locked(myLock):
                      # Code here executes with myLock held.  The lock is
                      # guaranteed to be released when the block is left (even
                      # if via return or by an uncaught exception).
                  

                  但值得一提的是,如果不将整个 with 块放入 try..除了块,这通常不是人们想要的.

                  Something worth mentioning is however, that you cannot easily catch exceptions thrown by the open() call without putting the whole with block inside a try..except block which is usually not what one wants.

                  这篇关于在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)

                    <bdo id='tcaIK'></bdo><ul id='tcaIK'></ul>
                      <tbody id='tcaIK'></tbody>
                    <i id='tcaIK'><tr id='tcaIK'><dt id='tcaIK'><q id='tcaIK'><span id='tcaIK'><b id='tcaIK'><form id='tcaIK'><ins id='tcaIK'></ins><ul id='tcaIK'></ul><sub id='tcaIK'></sub></form><legend id='tcaIK'></legend><bdo id='tcaIK'><pre id='tcaIK'><center id='tcaIK'></center></pre></bdo></b><th id='tcaIK'></th></span></q></dt></tr></i><div id='tcaIK'><tfoot id='tcaIK'></tfoot><dl id='tcaIK'><fieldset id='tcaIK'></fieldset></dl></div>
                    <legend id='tcaIK'><style id='tcaIK'><dir id='tcaIK'><q id='tcaIK'></q></dir></style></legend>
                  • <tfoot id='tcaIK'></tfoot>
                    • <small id='tcaIK'></small><noframes id='tcaIK'>