Python为元组中的每个元素命名

Jackey Python 1,311 次浏览 , 没有评论
from mrjob.py2 import xrange

student = ('Jim', 16, 'male', 'jim8721@gmail.com')

print(student[0])

# NAME = 0
# AGE = 1
# GENDER = 2
# EMAIL = 3

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

print(student[NAME])

from collections import namedtuple

Student = namedtuple('Student', ['name', 'age', 'gender', 'email'])

s = Student('Jim', 16, 'male', 'jim8721@gmail.com')

print(s)
print(s.name)

# 关键字传参
s2 = Student(name='Jim', age=16, gender='male', email='jim8721@gmail.com')

print(s2)

print(s2.name)

print(isinstance(s, tuple))

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Go