Docs/Python/具名元组.txt
2022-10-18 16:59:37 +08:00

7 lines
289 B
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

collections.namedtuple是一个工厂函数可以用来构建一个带字段名的元组和一个有名字的类。
from collections import namedtuple
City = namedtuple('City', 'name country population coordinates')
beijing = City('Beijing', 'CN', 2700, (116.3, 39.9))
print(beijing)