Python的namedtuple使用

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

 

posted @ 2018-09-20 08:57  卷积  阅读(142)  评论(0)    收藏  举报