Python namedtuple

2019-08-22  本文已影响0人  Joncc

namedtuple能够用来创建类似于元祖的数据类型,除了能够用索引来访问数据,能够迭代,更能够方便的通过属性名来访问数据:

In [1]: import collections

In [2]: MibObject = collections.namedtuple('MibObject', ['mibName', 'objectType', 'valueFunc'])

In [4]: obj = [MibObject('mymib','int',[1,2,3])]

In [5]: obj
Out[5]: [MibObject(mibName='mymib', objectType='int', valueFunc=[1, 2, 3])]

In [52]: for i in obj:
    ...:     print('name',i.mibName, 'type',i.objectType, 'value',i.valueFunc) 
    ...:     
('name', '1', 'type', '2', 'value', [1, 2, 3])


上一篇下一篇

猜你喜欢

热点阅读