Python的namedtuple使用
Posted 卷积
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 ‘ Representation:‘, bob jane = Person(name=‘Jane‘, age=29, gender=‘female‘) print ‘ Field by name:‘, jane.name print ‘ Field by index:‘ for p in [bob, jane]: print ‘%s is a %d year old %s‘ % p
以上是关于Python的namedtuple使用的主要内容,如果未能解决你的问题,请参考以下文章
python typedtuple是一个带有类型强制的namedtuple,它还包含StrictTuple的原始代码