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

来源:互联网 发布:ubuntu apache2 https 编辑:程序博客网 时间:2024/04/29 06:24
#!/usr/bin/env python# -*- coding:utf-8 -*-# 1、定义类似其他语言的枚举类型NAME = 0AGE = 1student = ("jim", "16", "male")print student[NAME]# 2、使用标准库中的collections.nametuple代替内置的tuplefrom collections import namedtuplestudent = namedtuple("student", ["name", "age", "sex"])first_student = student("tom", "12", "femal")print first_student.name
阅读全文
0 0
原创粉丝点击