python练手小题(二)

为元组的每个元素命名,提高程序的可读性

student = ('Tom',16,'man','132@qq.com')

# F1  类似于C里面定义常量,用常量代替索引
NAME,AGE,SEX,EMAIL = range(4)
print(student[NAME])

#F2  利用collections类
from collections import namedtuple

Student = namedtuple('Student',['name','age','sex','email']) 
s=Student('Tom2',16,'man','132@qq.com')
print(s)
print(isinstance(s,tuple))
原文地址:https://www.cnblogs.com/shuai06/p/12397519.html