首页 > 编程语言 > 详细

Python的namedtuple使用

时间:2018-09-20 10:05:08      阅读:143      评论:0      收藏:0      [点我收藏+]
from collections import namedtuple

# Each kind of nametuple is represented by its own class,
# created by using the nametuple() factory function
# The arguments are the name of the new class and a string 
# containing the names of the elements

Person = namedtuple(‘Person‘, ‘name age gender‘)

print ‘Type of Person:‘, type(Person)

bob = Person(name=‘Bob‘, age=30, gender=‘male‘)
print ‘\nRepresentation:‘, bob

jane = Person(name=‘Jane‘, age=29, gender=‘female‘)
print ‘\nField by name:‘, jane.name

print ‘\nField by index:‘
for p in [bob, jane]:
    print ‘%s is a %d year old %s‘ % p

 

Python的namedtuple使用

原文:https://www.cnblogs.com/Shinered/p/9678797.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!