代码之家  ›  专栏  ›  技术社区  ›  Sprotty

Python类型提示和嵌套类[duplicate]

  •  0
  • Sprotty  · 技术社区  · 6 年前

    我在python 3中有以下代码:

    class Position:
    
        def __init__(self, x: int, y: int):
            self.x = x
            self.y = y
    
        def __add__(self, other: Position) -> Position:
            return Position(self.x + other.x, self.y + other.y)
    

    但是我的编辑(PyCharm)说参考位置无法解析(在 __add__ 方法)。我应该如何指定我期望返回类型是类型 Position ?

    编辑:我认为这实际上是一个PyCharm问题。它实际上使用警告和代码完成中的信息

    但是如果我错了,请纠正我,并且需要使用其他语法。

    0 回复  |  直到 4 年前
        1
  •  5
  •   MacFreek Konstantin Suvorov    4 年前

    太长,读不下去了 from __future__ import annotations )-对于Python3.6或更低版本,请使用字符串。

    我想你有个例外:

    NameError: name 'Position' is not defined
    

    这是因为 Position 除非使用的是python3.10或更高版本,否则必须先定义才能在注释中使用它。

    Python 3.7+:

    Python3.7引入 PEP 563: postponed evaluation of annotations . 使用future语句的模块 将自动将批注存储为字符串:

    from __future__ import annotations
    
    class Position:
        def __add__(self, other: Position) -> Position:
            ...
    

    这在Python3.10中计划成为默认值。由于Python仍然是一种动态类型化语言,因此在运行时不进行类型检查,所以类型化注释应该不会对性能产生影响,对吗?错了!在Python3.7之前,类型化模块曾经是 one of the slowest python modules in core 所以 如果你 import typing 你会看到的 up to 7 times increase in performance 7.3升级时。

    Python<3.7:使用字符串

    According to PEP 484 ,应使用字符串而不是类本身:

    class Position:
        ...
        def __add__(self, other: 'Position') -> 'Position':
           ...
    

    如果您使用Django框架,这可能是大家熟悉的,因为Django模型也使用字符串作为前向引用(外键定义是指外部模型所在的位置) self 或尚未申报)。这应该与Pycharm和其他工具一起使用。

    相关部分 PEP 484 PEP 563 ,为免您的旅行:

    转发参考

    当类型提示包含尚未定义的名称时,该定义可以表示为字符串文本,稍后再进行解析。

    这种情况通常发生在容器类的定义中,其中定义的类出现在某些方法的签名中。例如,以下代码(简单二叉树实现的开始)不起作用:

    class Tree:
        def __init__(self, left: Tree, right: Tree):
            self.left = left
            self.right = right
    

    为了解决这个问题,我们写下:

    class Tree:
        def __init__(self, left: 'Tree', right: 'Tree'):
            self.left = left
            self.right = right
    

    以及PEP 563:

    在Python3.10中,函数和变量注释将不再在定义时求值。相反,字符串形式将保留在 __annotations__ 字典。静态类型检查程序在行为上没有区别,而在运行时使用注释的工具将不得不执行延迟的求值。

    ...

    从Python 3.7开始,可以使用以下特殊导入启用上述功能:

    from __future__ import annotations
    

    你可能会被诱惑去做的事情

    A、 定义虚拟对象 职位

    在类定义之前,放置一个虚拟定义:

    class Position(object):
        pass
    
    
    class Position(object):
        ...
    

    这样可以消除 NameError 甚至看起来还不错:

    >>> Position.__add__.__annotations__
    {'other': __main__.Position, 'return': __main__.Position}
    

    但是是吗?

    >>> for k, v in Position.__add__.__annotations__.items():
    ...     print(k, 'is Position:', v is Position)                                                                                                                                                                                                                  
    return is Position: False
    other is Position: False
    

    您可能想尝试一些Python元编程魔法并编写一个decorator 要对类定义进行猴子修补以添加注释,请执行以下操作:

    class Position:
        ...
        def __add__(self, other):
            return self.__class__(self.x + other.x, self.y + other.y)
    

    Position.__add__.__annotations__['return'] = Position
    Position.__add__.__annotations__['other'] = Position
    

    至少看起来是对的:

    >>> for k, v in Position.__add__.__annotations__.items():
    ...     print(k, 'is Position:', v is Position)                                                                                                                                                                                                                  
    return is Position: True
    other is Position: True
    

    可能太麻烦了。

    结论

    如果使用的是3.6或更低版本,请使用包含类名的字符串文本,在3.7中使用 它会起作用的。

        2
  •  0
  •   sam-6174    4 年前

    将类型指定为string很好,但总让我有点恼火,因为我们基本上是在绕过解析器。所以你最好不要拼错以下任何一个字符串:

    def __add__(self, other: 'Position') -> 'Position':
        return Position(self.x + other.x, self.y + other.y)
    

    from typing import TypeVar
    
    T = TypeVar('T', bound='Position')
    
    class Position:
    
        def __init__(self, x: int, y: int):
            self.x = x
            self.y = y
    
        def __add__(self, other: T) -> T:
            return Position(self.x + other.x, self.y + other.y)