- 普通做法:
student = ('mtianyan',18,'male','1147727180@qq.com')
# name
print student[0]
# age
print student[1] >= 10;
# sex
print student[2] == 'male'
C语言实现:
c: 采用宏定义枚举类型
python解决方案
使用标准库中collections.nametuple替代内置的tuple
from collections import namedtuple
# 类的工厂
Student = namedtuple('student',['name','age','sex','email'])
s1 = Student('mtianyan',18,'male','1147727180@qq.com')
print s1
s2 = Student(name ='mtianyan',age =18,sex = 'male',email = '1147727180@qq.com')
print s2
print s1.name
print s2.name
#任何使用普通元组的地方都可以使用这一替换
print isinstance(s1,tuple)
网友评论