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

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

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

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

        <tfoot id='QT2tl'></tfoot>

        如何在python中修改本地命名空間

        How to modify the local namespace in python(如何在python中修改本地命名空間)
          <tbody id='eKj7i'></tbody>
        <tfoot id='eKj7i'></tfoot>
        <i id='eKj7i'><tr id='eKj7i'><dt id='eKj7i'><q id='eKj7i'><span id='eKj7i'><b id='eKj7i'><form id='eKj7i'><ins id='eKj7i'></ins><ul id='eKj7i'></ul><sub id='eKj7i'></sub></form><legend id='eKj7i'></legend><bdo id='eKj7i'><pre id='eKj7i'><center id='eKj7i'></center></pre></bdo></b><th id='eKj7i'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='eKj7i'><tfoot id='eKj7i'></tfoot><dl id='eKj7i'><fieldset id='eKj7i'></fieldset></dl></div>

          • <bdo id='eKj7i'></bdo><ul id='eKj7i'></ul>
            <legend id='eKj7i'><style id='eKj7i'><dir id='eKj7i'><q id='eKj7i'></q></dir></style></legend>

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

                • 本文介紹了如何在python中修改本地命名空間的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  如何在 python 中修改函數的本地命名空間?我知道 locals() 在函數內部調用時會返回函數的本地命名空間,但我想做這樣的事情(我有一個理由要在 f 無法訪問 g 的情況下執行此操作,但給出更快一個簡單的、愚蠢的例子來說明問題):

                  How can I modify the local namespace of a function in python? I know that locals() returns the local namespace of the function when called inside it, but I want to do something like this (I have a reason why I want to do this where g is not accessible to f, but it's quicker to give a trivial, stupid example to illustrate the problem):

                  def g():
                     pass
                  
                  def f():
                      g()
                  
                  f.add_to_locals({'g':g})
                  

                  推薦答案

                  你有幾個選擇.首先,請注意,您的示例中的 g 實際上并不是函數的局部變量(即未在其中分配),它是全局變量(即尚未分配給局部變量).這意味著它將在定義函數的模塊中查找它.這是幸運的,因為沒有辦法在外部更改本地變量(除了修補字節碼),因為它們是在函數運行時分配的,而不是之前.

                  You've a couple of options. First, note that g in your example isn't actually a local to the function (ie. not assigned within it), it's a global (ie hasn't been assigned to a local variable). This means that it will be looked up in the module the function is defined in. This is fortunate, as there's no way of altering locals externally (short of patching the bytecode), as they get assigned when the function runs, not before.

                  一種選擇就是將你的函數注入到函數模塊的命名空間中.這將起作用,但會影響該模塊中訪問變量的每個函數,而不僅僅是一個函數.

                  One option is simply to inject your function into the function's module's namespace. This will work, but will affect every function in that module that accesses the variable, rather than just the one function.

                  要只影響一個函數,您需要將 func_globals 指向其他地方.不幸的是,這是一個只讀屬性,但您可以通過使用相同的主體但不同的全局命名空間重新創建函數來做您想做的事情:

                  To affect just the one function, you need to instead point that func_globals somewhere else. Unfortunately, this is a read-only property, but you can do what you want by recreating the function with the same body, but a different global namespace:

                  import new
                  f = new.function(f.func_code, {'g': my_g_function}, f.func_name, f.func_defaults, f.func_closure)
                  

                  f 現在將是相同的,除了它將在提供的 dict 中查找全局變量.請注意,這會重新綁定整個全局名稱空間 - 如果那里有 f 確實 查找的變量,請確保您也提供它們.不過,這也相當 hacky,并且可能不適用于 cpython 以外的 python 版本.

                  f will now be indentical, except that it will look for globals in the provided dict. Note that this rebinds the whole global namespace - if there are variables there that f does look up, make sure you provide them too. This is also fairly hacky though, and may not work on versions of python other than cpython.

                  這篇關于如何在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='bAlDc'></tbody>
                        <tfoot id='bAlDc'></tfoot>
                      • <small id='bAlDc'></small><noframes id='bAlDc'>

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

                            <i id='bAlDc'><tr id='bAlDc'><dt id='bAlDc'><q id='bAlDc'><span id='bAlDc'><b id='bAlDc'><form id='bAlDc'><ins id='bAlDc'></ins><ul id='bAlDc'></ul><sub id='bAlDc'></sub></form><legend id='bAlDc'></legend><bdo id='bAlDc'><pre id='bAlDc'><center id='bAlDc'></center></pre></bdo></b><th id='bAlDc'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='bAlDc'><tfoot id='bAlDc'></tfoot><dl id='bAlDc'><fieldset id='bAlDc'></fieldset></dl></div>
                            <legend id='bAlDc'><style id='bAlDc'><dir id='bAlDc'><q id='bAlDc'></q></dir></style></legend>
                            主站蜘蛛池模板: 久久亚洲一区 | 精品久久久久国产免费第一页 | aa级毛片毛片免费观看久 | 极品国产视频 | 女人精96xxx免费网站p | 在线成人免费视频 | 在线精品亚洲欧美日韩国产 | 国产精品高潮呻吟 | 精品国产乱码久久久久久图片 | av国产在线观看 | 中文字幕三区 | 福利色导航 | 毛片免费在线 | 久草新视频| 日韩欧美国产一区二区 | 操操日| 日韩在线视频免费观看 | 国产精品一区二区日韩 | 国产午夜精品一区二区三区嫩草 | 作爱视频免费观看 | 欧美一级久久 | a在线视频 | 成人福利在线 | 亚洲一区精品在线 | 欧美极品在线视频 | 日韩在线视频观看 | 国产农村妇女毛片精品久久麻豆 | 色婷婷在线视频 | 91精品国产综合久久久久久丝袜 | 男女久久久 | 黄色一级大片在线观看 | 精品在线一区二区三区 | 欧美99久久精品乱码影视 | 欧美日日 | 不卡在线视频 | 日韩国产高清在线观看 | 久久精品二区亚洲w码 | 啪啪网页| 国产精品99久久久久久久久久久久 | 一区二区成人在线 | 大乳boobs巨大吃奶挤奶 |