Python how to edit data in a namedtuple stored in

2019-05-31 15:33发布

import struct
from collections import namedtuple

StructPageNum = namedtuple('FDResult', ['DeviceID', 'PageNum','PicSize','PicData'])
PageNumList = []

Node = StructPageNum(DeviceID='NR09', PageNum=[],PicSize=100,PicData='')
PageNumList.append(Node)

PageNumList[0].PicData = 'hello' //how to do at here?

QUESTION

how to edit the value of PicData?

1条回答
Lonely孤独者°
2楼-- · 2019-05-31 16:07

It looks to me like you can use the _replace method of a namedtuple to do this pretty easily:

PageNumList[0] = PageNumList[0]._replace(PicData='hello')

This puts a new namedtuple in your PageNumList which looks pretty much like the old namedtuple except that we've changed the PicData "attribute". You can't edit the namedtuple that you have already since namedtuples are immutable (just like their unnamed counterparts: tuples)

查看更多
登录 后发表回答