python typing 笔记
from dataclasses import dataclass
from typing import List
@dataclass
class Player:
name: str
number: int
position: str
age: int
@dataclass
class Team:
name: str
players: List['Player'] # 使用字符串注解以避免循环引用
# 创建球员实例
james = Player(name='Lebron James', number=23, position='SF', age=25)
davis = Player(name='Anthony Davis', number=3, position='PF', age=21)
# 创建球队实例
lal = Player.Team(name='Los Angeles Lakers', players=[james, davis])
print(lal)