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

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

    <tfoot id='q3kCg'></tfoot>

    1. <small id='q3kCg'></small><noframes id='q3kCg'>

    2. <legend id='q3kCg'><style id='q3kCg'><dir id='q3kCg'><q id='q3kCg'></q></dir></style></legend>

        在 Python 3.6 中運行時根據(jù)聯(lián)合類型檢查變量

        Check a variable against Union type at runtime in Python 3.6(在 Python 3.6 中運行時根據(jù)聯(lián)合類型檢查變量)
        <i id='Bs5y4'><tr id='Bs5y4'><dt id='Bs5y4'><q id='Bs5y4'><span id='Bs5y4'><b id='Bs5y4'><form id='Bs5y4'><ins id='Bs5y4'></ins><ul id='Bs5y4'></ul><sub id='Bs5y4'></sub></form><legend id='Bs5y4'></legend><bdo id='Bs5y4'><pre id='Bs5y4'><center id='Bs5y4'></center></pre></bdo></b><th id='Bs5y4'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Bs5y4'><tfoot id='Bs5y4'></tfoot><dl id='Bs5y4'><fieldset id='Bs5y4'></fieldset></dl></div>
            <tbody id='Bs5y4'></tbody>
            <bdo id='Bs5y4'></bdo><ul id='Bs5y4'></ul>
            • <legend id='Bs5y4'><style id='Bs5y4'><dir id='Bs5y4'><q id='Bs5y4'></q></dir></style></legend>

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

                <tfoot id='Bs5y4'></tfoot>

                1. 本文介紹了在 Python 3.6 中運行時根據(jù)聯(lián)合類型檢查變量的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在嘗試編寫一個函數(shù)裝飾器,它使用 Python 3.6 類型提示來檢查參數(shù)字典是否尊重類型提示,如果沒有明確說明問題,則引發(fā)錯誤,以用于 HTTP API.

                  I'm trying to write a function decorator that uses Python 3.6 type hints to check that a dictionary of arguments respects the type hints and if not raise an error with a clear description of the problem, to be used for HTTP APIs.

                  問題是當函數(shù)有一個使用 Union 類型的參數(shù)時,我無法在運行時檢查變量.

                  The problem is that when the function has a parameter using the Union type I can't check a variable against it at runtime.

                  比如我有這個功能

                  from typing import Union
                  def bark(myname: str, descr: Union[int, str], mynum: int = 3) -> str:
                      return descr + myname * mynum
                  

                  我能做到:

                  isinstance('Arnold', bark.__annotations__['myname'])
                  

                  但不是:

                  isinstance(3, bark.__annotations__['descr'])
                  

                  因為 Union 不能與 isinstanceissubclass 一起使用.

                  Because Union cannot be used with isinstance or issubclass.

                  我找不到使用類型對象檢查它的方法.我嘗試自己實施檢查,但是當 bark.__annotations__['descr'] 在 REPL 中顯示為 typing.Union[int, str] 我不能在運行時訪問類型列表,如果不使用檢查 bark.__annotations__['descr'].__repr__() 的丑陋技巧.

                  I couldn't find a way to check it using the type object. I tried to implement the check by myself but while bark.__annotations__['descr'] is shown as typing.Union[int, str] in the REPL I can't access the list of the types at runtime, if not using the ugly hack of examining bark.__annotations__['descr'].__repr__().

                  是否有適當?shù)姆椒▉碓L問這些信息?還是故意讓它在運行時不易訪問?

                  Is there a proper way to access this information? Or is it deliberately intended to not be easily accessible at runtime?

                  推薦答案

                  你可以使用 Union__args__ 屬性,它包含一個 tuple可能的內(nèi)容:

                  You could use the __args__ attribute of Union which holds a tuple of the "possible contents:

                  >>> from typing import Union
                  
                  >>> x = Union[int, str]
                  >>> x.__args__
                  (int, str)
                  >>> isinstance(3, x.__args__)
                  True
                  >>> isinstance('a', x.__args__)
                  True
                  

                  __args__ 參數(shù)沒有記錄,因此它可能被認為是弄亂了實現(xiàn)細節(jié)",但它似乎比解析 repr 更好.

                  The __args__ argument is not documented so it could be considered "messing with implementation details" but it seems like a better way than parsing the repr.

                  這篇關(guān)于在 Python 3.6 中運行時根據(jù)聯(lián)合類型檢查變量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

                  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(如何在不重復導入頂級名稱的情況下構(gòu)造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(分發(fā)帶有已編譯動態(tài)共享庫的 Python 包)
                    <tbody id='Qw82z'></tbody>
                  <tfoot id='Qw82z'></tfoot>
                  <legend id='Qw82z'><style id='Qw82z'><dir id='Qw82z'><q id='Qw82z'></q></dir></style></legend>
                  <i id='Qw82z'><tr id='Qw82z'><dt id='Qw82z'><q id='Qw82z'><span id='Qw82z'><b id='Qw82z'><form id='Qw82z'><ins id='Qw82z'></ins><ul id='Qw82z'></ul><sub id='Qw82z'></sub></form><legend id='Qw82z'></legend><bdo id='Qw82z'><pre id='Qw82z'><center id='Qw82z'></center></pre></bdo></b><th id='Qw82z'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Qw82z'><tfoot id='Qw82z'></tfoot><dl id='Qw82z'><fieldset id='Qw82z'></fieldset></dl></div>

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

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

                            主站蜘蛛池模板: 日韩中文一区二区三区 | 插插插干干干 | 五月婷婷丁香 | 国产日韩欧美另类 | 成年人在线观看 | 欧美成人精品在线观看 | 91久久网站 | 国产精品成人一区二区三区 | 成人毛片网站 | 亚洲v日韩v综合v精品v | 亚洲一区二区三区四区五区午夜 | www.日韩 | 日韩一区二区三区在线 | 黄色在线免费观看视频 | 日韩中文字幕 | 免费精品一区 | 亚洲成人一区二区在线 | 国产成人福利在线观看 | 精品二 | 国产精品精品视频一区二区三区 | 亚洲一区免费 | 精品综合久久久 | 成人亚洲网站 | 日韩久久久久久 | 久久久久久久电影 | 国产一区精品在线 | 中文在线一区二区 | 国产日韩欧美精品一区二区三区 | 欧美久久久久久 | 久久看看 | 亚洲国产成人精品女人久久久 | 午夜丁香视频在线观看 | 每日更新av | 免费视频成人国产精品网站 | 欧美精产国品一二三区 | 欧美激情a∨在线视频播放 成人免费共享视频 | 麻豆av网| 精品一区二区不卡 | 成人av一区 | 欧美精品在线免费观看 | 久久青|