from collections import namedtuple
from dataclasses import dataclass

# 以前简单的类可以使用namedtuple实现。
Car = namedtuple('Car', 'color mileage')

my_car = Car('red', 3812.4)
print(my_car.color)
print(my_car)

# 自Python3.7开始可以使用dataclass。
print("===========使用dataclass========")


@dataclass
class Car:
    color: str
    mileage: float


my_car = Car('red', 3812.4)
print(my_car.color)
print(my_car)

相关文章:

  • 2021-12-05
  • 2022-12-23
  • 2022-12-23
  • 2021-09-26
  • 2022-12-23
  • 2022-12-23
  • 2022-01-19
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2023-03-21
  • 2021-11-06
  • 2021-11-17
  • 2021-08-25
相关资源
相似解决方案