Python的namedtuple使用
2021-06-22 08:05
标签:field ati function gen func arguments tuple end import Python的namedtuple使用 标签:field ati function gen func arguments tuple end import 原文地址:https://www.cnblogs.com/Shinered/p/9678797.htmlfrom 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
上一篇:四则运算(二叉树实现) Java
文章标题:Python的namedtuple使用
文章链接:http://soscw.com/index.php/essay/97289.html