Type Hints, zaznaczenie, że metoda klasy zwraca obiekt tej klasy

0

Cześć!
Mam taką klasę:

class Position:
    def __init__(self, y: int, x: int):
        self.y: int = y
        self.x: int = x

    def changed(self, y_change: int, x_change: int) -> Position:
        return Position(self.y + y_change, self.x + x_change)

i Pycharm wywala mi Error:

    def change(self, y_change: int, x_change: int) -> Position:
NameError: name 'Position' is not defined

Jak to rozwiązać?

0

@Suchy702: Pokaż cały kod

3

Nie chcę kopiować tego co już ktoś napisał, ale https://stackoverflow.com/questions/33533148/how-do-i-type-hint-a-method-with-the-type-of-the-enclosing-class

TL;DR: daj from __future__ import annotations na początku i będzie działać, o ile masz Pythona >= 3.7

2

@Suchy702: A jak nie masz pythona >=3.7 to zamiast Position dodaj "Position" jako string:

    def changed(self, y_change: int, x_change: int) -> "Position":
        return Position(self.y + y_change, self.x + x_change)

1 użytkowników online, w tym zalogowanych: 0, gości: 1