# here is a tuple ('David', 29, 'male', '23raff@163.com'), when we want to get its member
# we have to use index
e = ('David', 29, 'male', '23raff@163.com')
print(e[0])
print(e[1])
print(e[2])
print(e[3])
# it is not abvious when we want to the identity of each member
# now improve it using marco
NAME, AGE, GENDER, EMAIL = range(4)
print(e[NAME])
print(e[AGE])
print(e[GENDER])
print(e[EMAIL])
# we can use namedtuple to solve it perfect as following
from collections import namedtuple
Employee = namedtuple('Employee', ['name', 'age', 'gender', 'email'])
e = Employee('David', 29, 'male', '23raff@163.com')
print(e.name)
print(e.age)
print(e.gender)
print(e.email)
print(isinstance(e, tuple)) # s is a tuple
Python——为元组元素命名
最新推荐文章于 2024-11-11 09:32:23 发布