元组索引的命名

Posted 安迪_963

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了元组索引的命名相关的知识,希望对你有一定的参考价值。

#!/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)

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

以上是关于元组索引的命名的主要内容,如果未能解决你的问题,请参考以下文章