码迷,mamicode.com
首页 > 编程语言 > 详细

Python的namedtuple使用

时间:2018-09-20 11:25:08      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:field   ati   function   gen   func   arguments   tuple   end   import   

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使用

标签:field   ati   function   gen   func   arguments   tuple   end   import   

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

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!