Skip to content

Instantly share code, notes, and snippets.

@Everfighting
Created May 16, 2017 15:27
Show Gist options
  • Save Everfighting/49f5e92fce7ed35e61e4d2fbca5526f5 to your computer and use it in GitHub Desktop.
Save Everfighting/49f5e92fce7ed35e61e4d2fbca5526f5 to your computer and use it in GitHub Desktop.
元组元素命名,使得下标更有含义
from collections import namedtuple
Student = namedtuple('Student',['name','age','sex','email'])
# 位置传参
s = Student('Jim',16,'male','[email protected]')
# 关键字传参
s2 = Student(name ='Jim', age = 16, sex = 'male', email = '[email protected]')
# 类对象访问元组
s.name
# isinstance(s,tuple)
True
NAME,AGE,SEX,EMAIL = range(4)
student = ('jim',16,'male','[email protected]')
# name
print(student[NAME])
# age
if student[AGE] >= 18:
# sex
if student[SEX] == 'male':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment