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

  • <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="bseyhiv" 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 中運(yùn)行時(shí)根據(jù)聯(lián)合類型檢查變量

        Check a variable against Union type at runtime in Python 3.6(在 Python 3.6 中運(yùn)行時(shí)根據(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="50qlkba" 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 中運(yùn)行時(shí)根據(jù)聯(lián)合類型檢查變量的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

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

                  問題是當(dāng)函數(shù)有一個(gè)使用 Union 類型的參數(shù)時(shí),我無法在運(yùn)行時(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.

                  比如我有這個(gè)功能

                  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'])
                  

                  因?yàn)?Union 不能與 isinstanceissubclass 一起使用.

                  Because Union cannot be used with isinstance or issubclass.

                  我找不到使用類型對(duì)象檢查它的方法.我嘗試自己實(shí)施檢查,但是當(dāng) bark.__annotations__['descr'] 在 REPL 中顯示為 typing.Union[int, str] 我不能在運(yùn)行時(shí)訪問類型列表,如果不使用檢查 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__().

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

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

                  推薦答案

                  你可以使用 Union__args__ 屬性,它包含一個(gè) 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ù)沒有記錄,因此它可能被認(rèn)為是弄亂了實(shí)現(xiàn)細(xì)節(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 中運(yùn)行時(shí)根據(jù)聯(lián)合類型檢查變量的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

                  python: Two modules and classes with the same name under different packages(python:不同包下同名的兩個(gè)模塊和類)
                  Configuring Python to use additional locations for site-packages(配置 Python 以使用站點(diǎn)包的其他位置)
                  How to structure python packages without repeating top level name for import(如何在不重復(fù)導(dǎo)入頂級(jí)名稱的情況下構(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ā)帶有已編譯動(dòng)態(tài)共享庫(kù)的 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="oa5hdup" 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'>

                            主站蜘蛛池模板: 无线遥控更衣吊篮_IC卡更衣吊篮_电动更衣吊篮配件_煤矿更衣吊篮-力得电子 | 地磅-电子地磅维修-电子吊秤-汽车衡-无人值守系统-公路治超-鹰牌衡器 | 山东信蓝建设有限公司官网| 中央空调维修、中央空调保养、螺杆压缩机维修-苏州东菱空调 | 气动调节阀,电动调节阀,自力式压力调节阀,切断阀「厂家」-浙江利沃夫自控阀门 | 洗瓶机厂家-酒瓶玻璃瓶冲瓶机-瓶子烘干机-封口旋盖压盖打塞机_青州惠联灌装机械 | BESWICK球阀,BESWICK接头,BURKERT膜片阀,美国SEL继电器-东莞市广联自动化科技有限公司 | 天津次氯酸钠酸钙溶液-天津氢氧化钠厂家-天津市辅仁化工有限公司 | 咖啡加盟-咖啡店加盟-咖啡西餐厅加盟-塞纳左岸咖啡西餐厅官网 | 智能风向风速仪,风速告警仪,数字温湿仪,综合气象仪(气象五要素)-上海风云气象仪器有限公司 | 插针变压器-家用电器变压器-工业空调变压器-CD型电抗器-余姚市中驰电器有限公司 | 氧氮氢联合测定仪-联测仪-氧氮氢元素分析仪-江苏品彦光电 | 铝合金风口-玻璃钢轴流风机-玻璃钢屋顶风机-德州东润空调设备有限公司 | 红立方品牌应急包/急救包加盟,小成本好项目代理_应急/消防/户外用品加盟_应急好项目加盟_新奇特项目招商 - 中红方宁(北京) 供应链有限公司 | 东莞精密模具加工,精密连接器模具零件,自動機零件,冶工具加工-益久精密 | 长城人品牌官网| 广州展览设计公司_展台设计搭建_展位设计装修公司-众派展览装饰 广州展览制作工厂—[优简]直营展台制作工厂_展会搭建资质齐全 | 润滑脂-高温润滑脂-轴承润滑脂-食品级润滑油-索科润滑油脂厂家 | 电脑知识|软件|系统|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网 | 铜镍-康铜-锰铜-电阻合金-NC003 - 杭州兴宇合金有限公司 | 浩方智通 - 防关联浏览器 - 跨境电商浏览器 - 云雀浏览器 | Type-c防水母座|贴片母座|耳机接口|Type-c插座-深圳市步步精科技有限公司 | 深圳高新投三江工业消防解决方案提供厂家_服务商_园区智慧消防_储能消防解决方案服务商_高新投三江 | 铝合金风口-玻璃钢轴流风机-玻璃钢屋顶风机-德州东润空调设备有限公司 | 长信科技产业园官网_西安厂房_陕西标准工业厂房| 中图网(原中国图书网):网上书店,尾货特色书店,30万种特价书低至2折! | 篮球地板厂家_舞台木地板品牌_体育运动地板厂家_凯洁地板 | 北京网站建设-企业网站建设-建站公司-做网站-北京良言多米网络公司 | 密封圈_泛塞封_格莱圈-[东莞市国昊密封圈科技有限公司]专注密封圈定制生产厂家 | 镀锌角钢_槽钢_扁钢_圆钢_方矩管厂家_镀锌花纹板-海邦钢铁(天津)有限公司 | 恒温恒湿试验箱_高低温试验箱_恒温恒湿箱-东莞市高天试验设备有限公司 | 金属波纹补偿器厂家_不锈钢膨胀节价格_非金属伸缩节定制-庆达补偿器 | 压砖机_电动螺旋压力机_粉末成型压力机_郑州华隆机械tel_0371-60121717 | 净化车间装修_合肥厂房无尘室设计_合肥工厂洁净工程装修公司-安徽盛世和居装饰 | 防锈油-助焊剂-光学玻璃清洗剂-贝塔防锈油生产厂家 | 北京森语科技有限公司-模型制作专家-展览展示-沙盘模型设计制作-多媒体模型软硬件开发-三维地理信息交互沙盘 | 农业四情_农业气象站_田间小型气象站_智慧农业气象站-山东风途物联网 | SF6环境监测系统-接地环流在线监测装置-瑟恩实业 | 汕头市盛大文化传播有限公司,www.11400.cc | 合肥制氮机_合肥空压机厂家_安徽真空泵-凯圣精机 | 硬质合金模具_硬质合金非标定制_硬面加工「生产厂家」-西迪技术股份有限公司 |