元组索引的命名

#!/usr/bin/env python
#coding: utf-8

#student's info

student = ('Jack', 16, 'Male', 'aaa.com')
# The normal way 
# print(student[0])

# this is better
NAME = 0
AGE = 1
GENDER = 2
EMAIL= 3

# this make the code friendly to human
print(student[NAME])

# method 2
NAME, AGE, GENDER, EMAIL = range(0,4)
print(student[NAME])


from collections import namedtuple

Student = namedtuple('student', ['name', 'age', 'gender', 'email'])
s1 = Student ('Jack', 16, 'Male', 'aaa.com')
print(s1.name)

 通常情况下,我们是直接用下标,但命名的下标来便于理解其意义,在命令后就容易理解了

原文地址:https://www.cnblogs.com/Andy963/p/6953323.html