Python-api: collections——High-performance container datatypes

来源:互联网 发布:b2b网络平台建设方案 编辑:程序博客网 时间:2024/06/06 17:48

Python-api: collections——High-performance container datatypes

  • Python-api collectionsHigh-performance container datatypes
    • namedtuple


来源:https://docs.python.org/2/library/collections.html
一点一点更新,用到一点更新一点

namedtuple

namedtuple主要是为了增加tuple的可读性,它可以用于一系列比较规整的tuple,并且namedtuple可以通过名称去索引而tuple只能通过下标去索引

Person = namedtuple('Person', 'name age gender')print(type(Person))Bob = Person(name = 'Bob', age = 29, gender = 'female')print(Bob.name)print(Bob)

namedtuple默认是不能用关键字的,如果出现类似class,def等关键字会报错,可以通过rename参数来解决这个问题:

#rename = true 可以考虑到Python关键字Person = namedtuple('Person', 'name class gender', rename = true)
原创粉丝点击