您的位置:首页 > 编程语言 > Python开发

python高效编程技巧2(如何为元组的每个元素命名,提高程序的可读性)

2017-10-16 19:09 781 查看
#!/usr/bin/env python
# -*- coding:utf-8 -*-

# 1、定义类似其他语言的枚举类型
NAME = 0
AGE = 1
student = ("jim", "16", "male")
print student[NAME]

# 2、使用标准库中的collections.nametuple代替内置的tuple
from collections import namedtuple

student = namedtuple("student", ["name", "age", "sex"])
first_student = student("tom", "12", "femal")
print first_student.name
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python