tuple命名

定义枚举类型

NAME, AGE, GENDER, EMAIL = xrang(4)

用标准库中的collections.namedtuple替代内置tuple

from collections import namedtuple

# 定义tuple的名称
Student = namedtuple('Student',['name','age','gender','email'])

# 赋值
s = Student('Jim',16,'male','[email protected]')
# 也可以命名赋值
s2 = Student(name = 'jim', age = 16, gender = 'male', email = '[email protected]')

# 使用
s.name

# 判断类型
isinstance(s, tuple)

你可能感兴趣的:(tuple命名)