pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <i id='5p8us'><tr id='5p8us'><dt id='5p8us'><q id='5p8us'><span id='5p8us'><b id='5p8us'><form id='5p8us'><ins id='5p8us'></ins><ul id='5p8us'></ul><sub id='5p8us'></sub></form><legend id='5p8us'></legend><bdo id='5p8us'><pre id='5p8us'><center id='5p8us'></center></pre></bdo></b><th id='5p8us'></th></span></q></dt></tr></i><div class="dnd7p79" id='5p8us'><tfoot id='5p8us'></tfoot><dl id='5p8us'><fieldset id='5p8us'></fieldset></dl></div>

    <small id='5p8us'></small><noframes id='5p8us'>

    <legend id='5p8us'><style id='5p8us'><dir id='5p8us'><q id='5p8us'></q></dir></style></legend>
    1. <tfoot id='5p8us'></tfoot>

          <bdo id='5p8us'></bdo><ul id='5p8us'></ul>

        `except` 子句中的名稱綁定在子句后刪除

        Name binding in `except` clause deleted after the clause(`except` 子句中的名稱綁定在子句后刪除)
        • <small id='mTvOp'></small><noframes id='mTvOp'>

            <bdo id='mTvOp'></bdo><ul id='mTvOp'></ul>

              <tfoot id='mTvOp'></tfoot>
                  <legend id='mTvOp'><style id='mTvOp'><dir id='mTvOp'><q id='mTvOp'></q></dir></style></legend>

                • <i id='mTvOp'><tr id='mTvOp'><dt id='mTvOp'><q id='mTvOp'><span id='mTvOp'><b id='mTvOp'><form id='mTvOp'><ins id='mTvOp'></ins><ul id='mTvOp'></ul><sub id='mTvOp'></sub></form><legend id='mTvOp'></legend><bdo id='mTvOp'><pre id='mTvOp'><center id='mTvOp'></center></pre></bdo></b><th id='mTvOp'></th></span></q></dt></tr></i><div class="5hxhnvb" id='mTvOp'><tfoot id='mTvOp'></tfoot><dl id='mTvOp'><fieldset id='mTvOp'></fieldset></dl></div>

                    <tbody id='mTvOp'></tbody>
                  本文介紹了`except` 子句中的名稱綁定在子句后刪除的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  如何阻止 Python 刪除名稱綁定,當該名稱是用于綁定捕獲的異常?這是什么時候發生的變化行為進入 Python?

                  How can I stop Python from deleting a name binding, when that name is used for binding the exception that is caught? When did this change in behaviour come into Python?

                  我正在編寫在 Python 2 和 Python 3 上運行的代碼:

                  I am writing code to run on both Python 2 and Python 3:

                  exc = None
                  try:
                      1/0
                      text_template = "All fine!"
                  except ZeroDivisionError as exc:
                      text_template = "Got exception: {exc.__class__.__name__}"
                  
                  print(text_template.format(exc=exc))
                  

                  請注意,exc 在異常處理之前被顯式綁定,因此 Python 知道它是外部范圍內的名稱.

                  Notice that exc is explicitly bound before the exception handling, so Python knows it is a name in the outer scope.

                  在 Python 2.7 上,它運行良好,并且 exc 名稱仍然可以用于format 調用::

                  On Python 2.7, this runs fine and the exc name survives to be used in the format call::

                  Got exception: ZeroDivisionError
                  

                  太好了,這正是我想要的:except 子句綁定了名稱我可以在函數的其余部分使用該名稱來引用異常對象.

                  Great, this is exactly what I want: The except clause binds the name and I can use that name in the rest of the function to refer to the exception object.

                  在 Python 3.5 上,format 調用失敗,因為顯然 exc綁定被刪除::

                  On Python 3.5, the format call fails because apparently the exc binding is deleted::

                  Traceback (most recent call last):
                    File "<stdin>", line 8, in <module>
                  NameError: name 'exc' is not defined
                  

                  為什么 exc 綁定會從外部作用域中刪除?我們是什么意思可靠地保留名稱綁定以在 except 之后使用它子句?

                  Why is the exc binding deleted from the outer scope? How are we meant to reliably preserve the name binding to use it after the except clause?

                  此更改何時進入 Python,記錄在哪里?

                  When did this change come into Python, where is it documented?

                  將其報告為 Python 3 中的錯誤是否正確?

                  Would I be right to report this as a bug in Python 3?

                  推薦答案

                  不,這不是錯誤.您遇到的行為在 Python 3 中明確定義try/except 語句 的文檔.也給出了這種行為的原因:

                  No this is not a bug. The behavior you are experiencing is clearly and explicitly defined in the Python 3 documentation for the try/except statement. The reason for this behavior is also given:

                  當使用 as target 分配異常時,它會在 except 子句的末尾被清除.這好像

                  When an exception has been assigned using as target, it is cleared at the end of the except clause. This is as if

                  except E as N:
                     foo
                  

                  被翻譯成

                  except E as N:
                      try:
                          foo
                      finally:
                          del N
                  

                  這意味著必須將異常分配給不同的名稱,以便能夠在 except 子句之后引用它.異常被清除,因為附加了回溯,它們與堆棧幀形成一個引用循環,使該幀中的所有本地人保持活動狀態,直到下一次垃圾回收發生.

                  This means the exception must be assigned to a different name to be able to refer to it after the except clause. Exceptions are cleared because with the traceback attached to them, they form a reference cycle with the stack frame, keeping all locals in that frame alive until the next garbage collection occurs.

                  try/except 塊范圍之外聲明名稱不起作用的原因是您在as 子句.這就是 Python 刪除的名稱.

                  The reason declaring the name outside of the scope of the try/except block didn't work is because you used exc in the as clause. So that was the name Python deleted.

                  解決方法是在 as 子句中使用不同的名稱將異常綁定到,然后將全局變量分配給不同的異常名稱:

                  The fix is to use a different name in the as clause to bind the exception to, and then assign the global variable to the different exception name:

                  >>> exc_global = None
                  >>> try:
                      1 / 0
                      text_template = "All fine!"
                  except ZeroDivisionError as exc:
                      exc_global = exc
                      text_template = "Got exception: {exc.__class__.__name__}"
                  
                  
                  >>> print(text_template.format(exc=exc_global))
                  Got exception: ZeroDivisionError
                  

                  正如 Anthony Sottile 在評論中指出的那樣,try/except 代碼的反匯編也清楚地支持了文檔的上述陳述:

                  As Anthony Sottile noted in the comments, the disassembly for the try/except code also clearly supports the above statements made by the documentation:

                  >>> code = """
                  try:
                      1/0
                      text_template = "All fine!"
                  except ZeroDivisionError as exc:
                      text_template = "Got exception: {exc.__class__.__name__}"
                  """
                  >>> from dis import dis
                  >>> dis(code)
                    2           0 SETUP_EXCEPT            16 (to 18)
                  
                    3           2 LOAD_CONST               0 (1)
                                4 LOAD_CONST               1 (0)
                                6 BINARY_TRUE_DIVIDE
                                8 POP_TOP
                  
                    4          10 LOAD_CONST               2 ('All fine!')
                               12 STORE_NAME               0 (text_template)
                               14 POP_BLOCK
                               16 JUMP_FORWARD            38 (to 56)
                  
                    5     >>   18 DUP_TOP
                               20 LOAD_NAME                1 (ZeroDivisionError)
                               22 COMPARE_OP              10 (exception match)
                               24 POP_JUMP_IF_FALSE       54
                               26 POP_TOP
                               28 STORE_NAME               2 (exc)
                               30 POP_TOP
                               32 SETUP_FINALLY           10 (to 44)
                  
                    6          34 LOAD_CONST               3 ('Got exception: {exc.__class__.__name__}')
                               36 STORE_NAME               0 (text_template)
                               38 POP_BLOCK
                               40 POP_EXCEPT
                               42 LOAD_CONST               4 (None)
                          >>   44 LOAD_CONST               4 (None)
                               46 STORE_NAME               2 (exc)
                               48 DELETE_NAME              2 (exc)
                               50 END_FINALLY
                               52 JUMP_FORWARD             2 (to 56)
                          >>   54 END_FINALLY
                          >>   56 LOAD_CONST               4 (None)
                               58 RETURN_VALUE
                  

                  這篇關于`except` 子句中的名稱綁定在子句后刪除的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  python: Two modules and classes with the same name under different packages(python:不同包下同名的兩個模塊和類)
                  Configuring Python to use additional locations for site-packages(配置 Python 以使用站點包的其他位置)
                  How to structure python packages without repeating top level name for import(如何在不重復導入頂級名稱的情況下構造python包)
                  Install python packages on OpenShift(在 OpenShift 上安裝 python 包)
                  How to refresh sys.path?(如何刷新 sys.path?)
                  Distribute a Python package with a compiled dynamic shared library(分發帶有已編譯動態共享庫的 Python 包)
                • <legend id='oie9S'><style id='oie9S'><dir id='oie9S'><q id='oie9S'></q></dir></style></legend>
                    <tfoot id='oie9S'></tfoot>

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

                        <bdo id='oie9S'></bdo><ul id='oie9S'></ul>

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

                              <tbody id='oie9S'></tbody>
                          1. 主站蜘蛛池模板: 上海网站建设-上海网站制作-上海网站设计-上海做网站公司-咏熠软件 | 工业铝型材生产厂家_铝合金型材配件批发精加工定制厂商 - 上海岐易铝业 | 合肥抖音SEO网站优化-网站建设-网络推广营销公司-百度爱采购-安徽企匠科技 | 山东活动策划|济南活动公司|济南公关活动策划-济南锐嘉广告有限公司 | 集菌仪厂家_全封闭_封闭式_智能智能集菌仪厂家-上海郓曹 | 经济师考试_2025中级经济师报名时间_报名入口_考试时间_华课网校经济师培训网站 | 广东西屋电气有限公司-广东西屋电气有限公司| 水平垂直燃烧试验仪-灼热丝试验仪-漏电起痕试验仪-针焰试验仪-塑料材料燃烧检测设备-IP防水试验机 | 【MBA备考网】-2024年工商管理硕士MBA院校/报考条件/培训/考试科目/提前面试/考试/学费-MBA备考网 | IP检测-检测您的IP质量 | 精密冲床,高速冲床等冲压设备生产商-常州晋志德压力机厂 | 流量卡中心-流量卡套餐查询系统_移动电信联通流量卡套餐大全 | 权威废金属|废塑料|废纸|废铜|废钢价格|再生资源回收行情报价中心-中废网 | 施工围挡-施工PVC围挡-工程围挡-深圳市旭东钢构技术开发有限公司 | 武汉天安盾电子设备有限公司 - 安盾安检,武汉安检门,武汉安检机,武汉金属探测器,武汉测温安检门,武汉X光行李安检机,武汉防爆罐,武汉车底安全检查,武汉液体探测仪,武汉安检防爆设备 | 仓储笼_金属箱租赁_循环包装_铁网箱_蝴蝶笼租赁_酷龙仓储笼租赁 测试治具|过炉治具|过锡炉治具|工装夹具|测试夹具|允睿自动化设备 | 超声波分散机-均质机-萃取仪-超声波涂料分散设备-杭州精浩 | 3d打印服务,3d打印汽车,三维扫描,硅胶复模,手板,快速模具,深圳市精速三维打印科技有限公司 | 色油机-色母机-失重|称重式混料机-称重机-米重机-拌料机-[东莞同锐机械]精密计量科技制造商 | 液氮罐(生物液氮罐)百科-无锡爱思科| 精密机械零件加工_CNC加工_精密加工_数控车床加工_精密机械加工_机械零部件加工厂 | 二维运动混料机,加热型混料机,干粉混料机-南京腾阳干燥设备厂 | 滑板场地施工_极限运动场地设计_滑板公园建造_盐城天人极限运动场地建设有限公司 | 超声波破碎仪-均质乳化机(供应杭州,上海,北京,广州,深圳,成都等地)-上海沪析实业有限公司 | 小型玉石雕刻机_家用玉雕机_小型万能雕刻机_凡刻雕刻机官网 | 单锥双螺旋混合机_双螺旋锥形混合机-无锡新洋设备科技有限公司 | 耐高温硅酸铝板-硅酸铝棉保温施工|亿欧建设工程 | 电池挤压试验机-自行车喷淋-车辆碾压试验装置-深圳德迈盛测控设备有限公司 | 警方提醒:赣州约炮论坛真的安全吗?2025年新手必看的网络交友防坑指南 | C形臂_动态平板DR_动态平板胃肠机生产厂家制造商-普爱医疗 | 掺铥光纤放大器-C/L波段光纤放大器-小信号光纤放大器-合肥脉锐光电技术有限公司 | 安徽净化工程设计_无尘净化车间工程_合肥净化实验室_安徽创世环境科技有限公司 | 电子厂招聘_工厂招聘_普工招聘_小时工招聘信息平台-众立方招工网 | 济南ISO9000认证咨询代理公司,ISO9001认证,CMA实验室认证,ISO/TS16949认证,服务体系认证,资产管理体系认证,SC食品生产许可证- 济南创远企业管理咨询有限公司 郑州电线电缆厂家-防火|低压|低烟无卤电缆-河南明星电缆 | 匀胶机旋涂仪-声扫显微镜-工业水浸超声-安赛斯(北京)科技有限公司 | 海外整合营销-独立站营销-社交媒体运营_广州甲壳虫跨境网络服务 焊管生产线_焊管机组_轧辊模具_焊管设备_焊管设备厂家_石家庄翔昱机械 | 有机废气处理-rto焚烧炉-催化燃烧设备-VOC冷凝回收装置-三梯环境 | 丙烷/液氧/液氮气化器,丙烷/液氧/液氮汽化器-无锡舍勒能源科技有限公司 | 【同风运车官网】一站式汽车托运服务平台,验车满意再付款 | 药品仓库用除湿机-变电站用防爆空调-油漆房用防爆空调-杭州特奥环保科技有限公司 | 英语词典_成语词典_日语词典_法语词典_在线词典网 |