- 第一种
student = ('张三','18','男','18202032222')
NAME,AGE,SEX,CELLPHONE = range(4) # 设置索引下标为常量值
# 姓名:张三 ,年龄:18 性别:男 联系方式:18202032222
print("姓名:%s ,年龄:%s 性别:%s 联系方式:%s" %(student[NAME],student[AGE],student[SEX],student[CELLPHONE]))
- 第二种(collections.namedtuple)
from collections import namedtuple
Student = namedtuple('Student',['name','age','sex','cellphone'])
student = Student('张三','18','男','18202032222')
# 姓名:张三 ,年龄:18 性别:男 联系方式:18202032222
print("姓名:%s ,年龄:%s 性别:%s 联系方式:%s" %(student.name,student.age,student.sex,student.cellphone))
print(isinstance(student,tuple)) # True,它是内置元组的子类