<i id='1dQqh'><tr id='1dQqh'><dt id='1dQqh'><q id='1dQqh'><span id='1dQqh'><b id='1dQqh'><form id='1dQqh'><ins id='1dQqh'></ins><ul id='1dQqh'></ul><sub id='1dQqh'></sub></form><legend id='1dQqh'></legend><bdo id='1dQqh'><pre id='1dQqh'><center id='1dQqh'></center></pre></bdo></b><th id='1dQqh'></th></span></q></dt></tr></i><div id='1dQqh'><tfoot id='1dQqh'></tfoot><dl id='1dQqh'><fieldset id='1dQqh'></fieldset></dl></div>

    • <bdo id='1dQqh'></bdo><ul id='1dQqh'></ul>
    <tfoot id='1dQqh'></tfoot>

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

      <small id='1dQqh'></small><noframes id='1dQqh'>

      如何在 Python 3.5 中键入提示属性?

      时间:2023-09-13
      <legend id='f8tTE'><style id='f8tTE'><dir id='f8tTE'><q id='f8tTE'></q></dir></style></legend>

            <bdo id='f8tTE'></bdo><ul id='f8tTE'></ul>
          • <tfoot id='f8tTE'></tfoot>

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

                  <tbody id='f8tTE'></tbody>
              2. 本文介绍了如何在 Python 3.5 中键入提示属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个类,我希望属性的初始值为 None:

                I have a class where I want the initial value of an attribute to be None:

                class SomeClass:
                    def __init__(self):
                        self.some_attribute = None
                

                如何添加类型提示,以便 IDE 了解 some_attribute 通常属于 AnotherClass 类型?

                How can I add type hinting, so that the IDE understands that some_attribute is usually of the type AnotherClass?

                推荐答案

                在 Python 3.5 中,你必须写

                In Python 3.5, you have to write

                self.some_attribute = None  # type: AnotherClass
                

                从 Python 3.6 开始,为变量添加了新的类型提示语法(PEP 526):

                Since Python 3.6, new type hinting syntax was added for variables (PEP 526):

                self.some_attribute: AnotherClass = None
                

                这可能会让每个类型检查系统都抱怨,因为 None 实际上不是 AnotherClass 的实例.相反,您可以使用 typing.Union[None, AnotherClass],或简写:

                This will probably make every type-checking system complain, because None is in fact not an instance of AnotherClass. Instead, you can use typing.Union[None, AnotherClass], or the shorthand:

                from typing import Optional
                ...
                self.some_attribute: Optional[AnotherClass] = None
                

                这篇关于如何在 Python 3.5 中键入提示属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何使 Python/Sphinx 文档对象属性仅在 __init__ 中声 下一篇:Django 模型与Python 类属性

                相关文章

                最新文章

                    <legend id='35VhU'><style id='35VhU'><dir id='35VhU'><q id='35VhU'></q></dir></style></legend>

                      <bdo id='35VhU'></bdo><ul id='35VhU'></ul>

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