相关疑难解决方法(0)

使用类作为其方法中的参数的类型提示

我在下面包含的代码会引发以下错误:

NameError: name 'Vector2' is not defined 
Run Code Online (Sandbox Code Playgroud)

在这一行:

def Translate (self, pos: Vector2):
Run Code Online (Sandbox Code Playgroud)

为什么Python不能Vector2Translate方法中识别我的类?

class Vector2:

    def __init__(self, x: float, y: float):

        self.x = x
        self.y = y

    def Translate(self, pos: Vector2):

        self.x += pos.x
        self.y += pos.y
Run Code Online (Sandbox Code Playgroud)

python oop type-hinting python-3.x

16
推荐指数
2
解决办法
3188
查看次数

标签 统计

oop ×1

python ×1

python-3.x ×1

type-hinting ×1