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))
posted @ 2018-08-05 21:26  此时相望不相闻  阅读(61)  评论(0编辑  收藏  举报