久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    • <bdo id='i3PtZ'></bdo><ul id='i3PtZ'></ul>

      <tfoot id='i3PtZ'></tfoot>

      <i id='i3PtZ'><tr id='i3PtZ'><dt id='i3PtZ'><q id='i3PtZ'><span id='i3PtZ'><b id='i3PtZ'><form id='i3PtZ'><ins id='i3PtZ'></ins><ul id='i3PtZ'></ul><sub id='i3PtZ'></sub></form><legend id='i3PtZ'></legend><bdo id='i3PtZ'><pre id='i3PtZ'><center id='i3PtZ'></center></pre></bdo></b><th id='i3PtZ'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='i3PtZ'><tfoot id='i3PtZ'></tfoot><dl id='i3PtZ'><fieldset id='i3PtZ'></fieldset></dl></div>
    1. <small id='i3PtZ'></small><noframes id='i3PtZ'>

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

      1. exec() 方法中自定義命名空間的子類化 python 字典

        subclassed python dictionary for custom namespace in exec() method(exec() 方法中自定義命名空間的子類化 python 字典)
            <bdo id='VoVGr'></bdo><ul id='VoVGr'></ul>
            <tfoot id='VoVGr'></tfoot>
            • <small id='VoVGr'></small><noframes id='VoVGr'>

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

              1. <legend id='VoVGr'><style id='VoVGr'><dir id='VoVGr'><q id='VoVGr'></q></dir></style></legend>

                  本文介紹了exec() 方法中自定義命名空間的子類化 python 字典的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在嘗試對字典進行子類化以在 exec 方法中使用.最終,我希望本地函數具有自定義名稱范圍行為.

                  I am trying to subclass a dictionary for use in an exec method. Ultimately, I would like the local function to have a custom name scoping behaviour.

                  在下面的代碼中,函數 b() 實際上確實有正確的 globals() 字典可供它使用,但是在解析 z.

                  In the code below, function b() does in fact have the correct globals() dictionary available to it, however it fails to search it when resolving z.

                  函數b()是否先不搜索locals()然后globals()?

                  Does function b() first not search locals() then globals() ?

                  非常令人費解.任何幫助表示贊賞.

                  Very puzzling. Any help appreciated.

                  t = '''
                  def b():
                  #   return (globals()['z']) #works
                      return z #fails
                  
                  b()
                  '''
                  
                  class MyDict(dict):
                      def __init__(self, g):
                          dict.__init__(self)
                          self.my_g = g
                  
                  
                      def __getitem__(self, key):
                          print("GET ", key)
                          try:
                              val = dict.__getitem__(self, key)
                          except:
                              print("GET exception1")
                              val = self.my_g[key]
                          return val
                  
                  
                  g = {'z':123}
                  
                  md = MyDict(g)
                  
                  #fails to find z
                  exec(t, md, md)
                  
                  #works
                  #exec(t, g, g)
                  

                  輸出

                  GET  b
                  Traceback (most recent call last):
                    File "/project1/text12", line 31, in <module>
                    File "<string>", line 6, in <module>
                    File "<string>", line 4, in b
                  NameError: global name 'z' is not defined
                  

                  推薦答案

                  在 python 3.3 之前,您不能globals 使用自定義 dict 子類 exec 語句的值.執行編譯代碼的底層 C 代碼直接訪問底層 C 結構,忽略您可能已經實現的任何自定義掛鉤.

                  Before python 3.3, you cannot use a custom dict subclass for the globals value of an exec statement. The underlying C code that executes the compiled code accesses the underlying C structures directly, ignoring any custom hooks you may have implemented.

                  換句話說,當代碼執行 LOAD_GLOBAL 操作時(就像函數 b 中的 z 一樣),C字節碼評估循環使用 globals 結構">C API,繞過任何 python 覆蓋.

                  In other words, when the code does a LOAD_GLOBAL operation (as is the case with z in your function b), the C bytecode evaluation loop accesses the globals structure in the current frame using the C API, bypassing any python overrides.

                  這記錄在 exec()功能文檔 as:

                  This is documented in the exec() function documentation as:

                  如果只提供了globals,它必須是一個字典,它將用于全局和局部變量.如果給出了 globalslocals,則它們分別用于全局變量和局部變量.如果提供,locals 可以是任何映射對象.

                  If only globals is provided, it must be a dictionary, which will be used for both the global and the local variables. If globals and locals are given, they are used for the global and local variables, respectively. If provided, locals can be any mapping object.

                  此限制已在 Python 3.3 中放寬,請參閱 issue 14385.文檔尚未更新,但字節碼評估循環已更新,以在回退到 C API 訪問之前測試自定義映射.如果使用自定義映射,則使用 PyObject_GetItem 函數,該函數將在自定義類上調用 __getitem__.

                  This restriction has been loosened in Python 3.3, see issue 14385. The documentation hasn't been updated yet, but the bytecode evaluation loop has been updated to test for custom mappings before falling back to the C API access. If a custom mapping is used, the PyObject_GetItem function is used, which will call __getitem__ on custom classes.

                  這篇關于exec() 方法中自定義命名空間的子類化 python 字典的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 包)
                    <tbody id='BWFcX'></tbody>
                      <bdo id='BWFcX'></bdo><ul id='BWFcX'></ul>

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

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

                        • <small id='BWFcX'></small><noframes id='BWFcX'>

                          1. 主站蜘蛛池模板: 久草成人 | 91免费小视频 | 日本午夜视频 | 国产视频一区在线 | 成人午夜免费视频 | 9191成人精品久久 | 日本久久网 | 久久久久一区 | 在线观看日本高清二区 | www视频在线观看 | 一本综合久久 | 亚洲黄色视屏 | 久久精品综合 | 成人精品国产免费网站 | 男人的天堂在线视频 | а天堂中文最新一区二区三区 | 中文字幕一区二区三区乱码在线 | 精品九九 | 91国内精精品久久久久久婷婷 | 久草福利| 理伦毛片 | 国产精品国产自产拍高清 | 国产精品视频网 | 欧美aⅴ | 免费看爱爱视频 | 欧洲精品在线观看 | 欧美电影网 | 日韩国产欧美视频 | 国产精品一区二区三区久久久 | 精品一区二区在线看 | 久久精品 | 91在线看| 老司机成人在线 | 韩日av片| 成人午夜看片 | av国产精品毛片一区二区小说 | 久久99精品久久久久久 | 波多野结衣一区二区三区在线观看 | 国产精品一区一区三区 | 成av人电影在线 | 午夜综合 |