Why does Python not support record type? (i.e. mutable namedtuple)

后端 未结 11 1584
眼角桃花
眼角桃花 2020-12-12 22:10

Why does Python not support a record type natively? It\'s a matter of having a mutable version of namedtuple.

I could use namedtuple._replace. But I nee

11条回答
  •  醉话见心
    2020-12-12 22:31

    There's a library similar to namedtuple, but mutable, called recordtype.

    Package home: http://pypi.python.org/pypi/recordtype

    Simple example:

    from recordtype import recordtype
    
    Person = recordtype('Person', 'first_name last_name phone_number')
    person1 = Person('Trent', 'Steele', '637-3049')
    person1.last_name = 'Terrence';
    
    print person1
    # Person(first_name=Trent, last_name=Terrence, phone_number=637-3049)
    

    Simple default value example:

    Basis = recordtype('Basis', [('x', 1), ('y', 0)])
    

    Iterate through the fields of person1 in order:

    map(person1.__getattribute__, Person._fields)
    

提交回复
热议问题