Python namedtuple 用用更健康

Python的Collections模块提供了不少好用的数据容器类型,其中一个精品当属namedtuple

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

>>> from collections import namedtuple
>>> Animal = namedtuple('Animal', 'name age type')
>>> big_yellow = Animal(name="big_yellow", age=3, type="dog")
>>> big_yellow
Animal(name='big_yellow', age=3, type='dog')
>>> big_yellow.name
'big_yellow'

从上面的列子可以看到,有了namedtuple,通过属性访问数据能够让我们的代码更加的直观更好维护(相比索引访问的话),使用pycharm这样的IDE,编码的时候也会有相应的智能提示,进一步确保不犯低级错误。

类似于tuple,它的属性也是不可变的:

>>> big_yellow.age += 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: can't set attribute

能够方便的转换成OrderedDict:

>>> big_yellow._asdict()
OrderedDict([('name', 'big_yellow'), ('age', 3), ('type', 'dog')])

方法返回多个值得时候,其实更好的是返回namedtuple的结果,这样程序的逻辑会更加的清晰和好维护:

>>> from collections import namedtuple
>>> def get_name():
...     name = namedtuple("name", ["first", "middle", "last"])
...     return name("John", "You know nothing", "Snow")
...
>>> name = get_name()
>>> print name.first, name.middle, name.last
John You know nothing Snow

相比tupledictionarynamedtuple略微有点综合体的意味:直观、使用方便,墙裂建议大家在合适的时候多用用namedtuple。
更多相关的介绍可以查看官方文档。

参考资料:

https://docs.python.org/2/library/collections.html#collections.namedtuple

    原文作者:TypingQuietly
    原文地址: https://www.jianshu.com/p/e938a06a85f4
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞