【python】-学习记

提供程序可读性,减少使用素组和集合中的索引。

student = ('Tom', 25, 'male', 'tom123@gomail.com')

#name
#数组多了程序里面都是[]
print(student[0])

一、定义常量

NAME,AGE,SEX,EMAIL = xrange(4)
student = ('Tom', 25, 'male', 'tom123@gomail.com')
#提高程序可读性。便于后续的维护
print(student[NAME]) 
print(student[AGE])

二、标准库collections

from collections import namedtuple
Student  = namedtuple('Student', [ 'name', 'age', 'sex', 'email']
 s = Student('Tom', 25, 'male', 'tom123@gomail.com')
#访问元素不需要用索引了
#直接做对象访问
s.age
#25
s.name
#Tom
    原文作者:jiandanyaobai
    原文地址: https://www.jianshu.com/p/f4e3ff9d6370
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞