您的位置:首页 > 其它

如何为每个元组中的每个元素命名,提高可读性

2019-02-13 13:39 363 查看

-> 解决方案一:定义类似与其他语言的枚举类型也就是定义一系列数值常量

NAME,AGE,SEX,EMAIL = range(4)
student = ('Jim','21','male','jim723tmail.com')
#name
print(student[NAME])

#age
if student[AGE] >= 18:
pass

#sex
if student[SEX] == 'mail':
pass

-> 解决方案二:使用标准库中collections.namedtuple替代内置tuple

from collections import  namedtuple
Student  = namedtuple('Student',['name','age','sex','email'])
s = Student('Jim',16,'male','jim32r3422@mail.com')
print(s)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: