from collections import namedtuple
Student = namedtuple('Student', ['name', 'age', 'gender', 'email'])
s = Student('Tom', 16, 'male', 'xxx@gmail.com')
# s 为内置元组带子类
print(s.name)
# 输出为:'Tom'
print(s.age)
# 输出为:16
from collections import namedtuple
Student = namedtuple('Student', ['name', 'age', 'gender', 'email'])
s = Student('Tom', 16, 'male', 'xxx@gmail.com')
# s 为内置元组带子类
print(s.name)
# 输出为:'Tom'
print(s.age)
# 输出为:16
本文标题:python 如何为 tuple 中每个元素命名?
本文链接:https://www.haomeiwen.com/subject/lhswixtx.html
网友评论