如何使用泛型 return class 键入 python?
How to return the class type in python using generics?
我正在使用泛型在 python 中创建列表 class(类似于 java 列表的列表)。 class 节点也是通用的,我正在为上一个节点和下一个节点创建 getter 和 setter 方法。我想知道如何 return 像 class 节点本身这样的类型?
这是我的进步:
from typing import Generic, TypeVar
T = TypeVar('T')
class Node(Generic[T]):
''' Generic class Node. It models a node for a linked list. '''
def __init__(self, element: T) -> None:
''' Constructor. It recives an element of type T.'''
self.element = element
self.next = None
self.prev = None
def get_item(self) -> T:
''' Returns the item in the node.'''
return self.element
def get_prev(self) -> #What is the type I should return here?:
''' Returns the previous node.'''
return self.prev
def get_next(self) -> #What is the type I should return here?:
''' Return the next node.'''
return self.next
def set_prev(self, prev) -> None:
''' Changes the previous element to the specified node.'''
self.prev = prev
def set_next(self, next) -> None:
''' Changes the next element to the specified node.'''
self.next = next
我试过
def get_prev(self) -> Node[T]:
''' Returns the previous node.'''
return self.prev
def get_next(self) -> Node[T]:
''' Return the next node.'''
return self.next
但它给了我错误
Traceback (most recent call last):
File "List.py", line 5, in <module>
class Node(Generic[T]):
File "List.py", line 18, in Node
def get_prev(self) -> Node[T]:
NameError: name 'Node' is not defined
我认为最新的 Python 版本在类型注释中具有 self-referencing 类 的功能(这里,get_prev
的 return 类型 Node
当前正在定义)。
较早的 Python 版本,低至 3.7,通过添加仍然支持它:
from __future__ import annotations
参见:https://peps.python.org/pep-0563/#enabling-the-future-behavior-in-python-3-7
我正在使用泛型在 python 中创建列表 class(类似于 java 列表的列表)。 class 节点也是通用的,我正在为上一个节点和下一个节点创建 getter 和 setter 方法。我想知道如何 return 像 class 节点本身这样的类型? 这是我的进步:
from typing import Generic, TypeVar
T = TypeVar('T')
class Node(Generic[T]):
''' Generic class Node. It models a node for a linked list. '''
def __init__(self, element: T) -> None:
''' Constructor. It recives an element of type T.'''
self.element = element
self.next = None
self.prev = None
def get_item(self) -> T:
''' Returns the item in the node.'''
return self.element
def get_prev(self) -> #What is the type I should return here?:
''' Returns the previous node.'''
return self.prev
def get_next(self) -> #What is the type I should return here?:
''' Return the next node.'''
return self.next
def set_prev(self, prev) -> None:
''' Changes the previous element to the specified node.'''
self.prev = prev
def set_next(self, next) -> None:
''' Changes the next element to the specified node.'''
self.next = next
我试过
def get_prev(self) -> Node[T]:
''' Returns the previous node.'''
return self.prev
def get_next(self) -> Node[T]:
''' Return the next node.'''
return self.next
但它给了我错误
Traceback (most recent call last):
File "List.py", line 5, in <module>
class Node(Generic[T]):
File "List.py", line 18, in Node
def get_prev(self) -> Node[T]:
NameError: name 'Node' is not defined
我认为最新的 Python 版本在类型注释中具有 self-referencing 类 的功能(这里,get_prev
的 return 类型 Node
当前正在定义)。
较早的 Python 版本,低至 3.7,通过添加仍然支持它:
from __future__ import annotations
参见:https://peps.python.org/pep-0563/#enabling-the-future-behavior-in-python-3-7