# coding:utf-8from collections import namedtuple
Student = namedtuple('Student', ['no', 'name', 'age', 'sex'])
student = Student("2016214338", u"张三", 19, u"男")print student.nameprint student.age
-----结果显示--------------------
张三
19
本文介绍了一种利用Python标准库中的namedtuple来快速创建具有一定意义字段的对象的方法。通过一个具体的例子——创建一个包含学号、姓名、年龄和性别的学生对象——展示了如何定义和使用这些对象。
# coding:utf-8from collections import namedtuple
Student = namedtuple('Student', ['no', 'name', 'age', 'sex'])
student = Student("2016214338", u"张三", 19, u"男")print student.nameprint student.age
-----结果显示--------------------
张三
19
750
4317
370

被折叠的 条评论
为什么被折叠?