抱歉,我真的不知道如何表达这个问题。 我对 def 之后括号中内容的用途(例如 (card: str) )以及 -> 符号的作用感到困惑。到底是怎么回事。为什么一个函数 -> 指向类名而其他函数则指向基本数据类型。
@dataclass
class Card:
suit: str
rank: int
def parse_card(card: str) -> Card:
return Card(card[0], int(card[1:]))
@dataclass
class Rectangle:
length: int
width: int
def area(rect: Rectangle) -> int:
return rect.length * rect.width
@dataclass
class Square:
color: str
width: int
@dataclass
class Circle:
radius: int
color: str
def square_to_circle(a_square: Square) -> Circle:
return Circle(a_square.width, a_square.color)
我不知道我在做什么,请帮忙
正如 Tim 和 Matthias 已经提到的,这些是类型提示。
您可以在此处阅读更多文档:https://peps.python.org/pep-0484/