<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 id='Bs5y4'><tfoot id='Bs5y4'></tfoot><dl id='Bs5y4'><fieldset id='Bs5y4'></fieldset></dl></div>
    <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 中运行时根据联合类型检查变量

        时间:2023-09-14
          <tbody id='q3kCg'></tbody>
      2. <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 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 中运行时根据联合类型检查变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试编写一个函数装饰器,它使用 Python 3.6 类型提示来检查参数字典是否尊重类型提示,如果没有明确说明问题,则引发错误,以用于 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.

                  问题是当函数有一个使用 Union 类型的参数时,我无法在运行时检查变量.

                  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__().

                  是否有适当的方法来访问这些信息?还是故意让它在运行时不易访问?

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

                  推荐答案

                  你可以使用 Union__args__ 属性,它包含一个 tuple可能的内容:

                  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__ 参数没有记录,因此它可能被认为是弄乱了实现细节",但它似乎比解析 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.

                  这篇关于在 Python 3.6 中运行时根据联合类型检查变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在分组条形图上方显示百分比 下一篇:Seaborn Catplot 在条形图上设置值

                  相关文章

                  最新文章

                • <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 id='Qw82z'><tfoot id='Qw82z'></tfoot><dl id='Qw82z'><fieldset id='Qw82z'></fieldset></dl></div>

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

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