元组索引的命名

元组索引的命名
#!/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)

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

posted on 2017-09-23 18:30 Andy_963 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Andy963/p/6953323.html

你可能感兴趣的:(python)